using jq I want to output json object within a json object including the keys not just the value
I have this stored as a file:
{
"Targets": [
{
"Id": "xxx_hourly_cron",
"Arn": "arn:aws:ecs:eu-west-2:000000000000:cluster/drupal-cluster",
"RoleArn": "arn:aws:iam::000000000000:role/ecsEventsRole",
"EcsParameters": {
"TaskDefinitionArn": "arn:aws:ecs:eu-west-2:000000000000:task-definition/xxx-cron:55",
"TaskCount": 1,
"EnableECSManagedTags": false,
"EnableExecuteCommand": false,
"PropagateTags": "TASK_DEFINITION"
}
}
]
}
Using bash and jq I want to trim it down this this:
{
"TaskDefinitionArn": "arn:aws:ecs:eu-west-2:000000000000:task-definition/xxx-cron:55",
"TaskCount": 1,
"EnableECSManagedTags": false,
"EnableExecuteCommand": false,
"PropagateTags": "TASK_DEFINITION"
}
I have tried this but it does not output the keys:
cat rule-target.json | jq '(.Targets[] .EcsParameters[])'
Solution 1:
You are so close:
$ jq '.Targets[].EcsParameters' rule-target.json
{
"TaskDefinitionArn": "arn:aws:ecs:eu-west-2:000000000000:task-definition/xxx-cron:55",
"TaskCount": 1,
"EnableECSManagedTags": false,
"EnableExecuteCommand": false,
"PropagateTags": "TASK_DEFINITION"
}
As EcsParameters
isn't an array, you shouldn't unwrap it ([]
).
Usually it's a useless use of cat
when doing cat file | cmd
, many commands takes files as arguments:
cmd -flag -flag2 file1.txt file2.txt
and if they don't then you can redirect stdin:
cmd -flag -flag2 < file1.txt
Solution 2:
I think you want:
jq '(.Targets[]|.EcsParameters)' rule-target.json
Which results in:
{
"TaskDefinitionArn": "arn:aws:ecs:eu-west-2:000000000000:task-definition/xxx-cron:55",
"TaskCount": 1,
"EnableECSManagedTags": false,
"EnableExecuteCommand": false,
"PropagateTags": "TASK_DEFINITION"
}