1

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[])'

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

So many jq experts now. Your answer is optimal. Bravo!
1

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"
}

Comments

1

You can use

jq '.Targets[] | .EcsParameters' rule-target.json

Demo

array wrappers seen in the current case remove the object wrappers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.