Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse JSON to jq to get a list from AWS CLI

Tags:

aws-cli

jq

I would like to get a list of task definitions I can create a list from via AWS CLI. Here is a partial list from the command;

aws ecs list-task-definitions --region eu-west-1

{
    "taskDefinitionArns": [
        "arn:aws:ecs:eu-west-1:543573289192:task-definition/audience-view-dev-api:4", 
        "arn:aws:ecs:eu-west-1:543573289192:task-definition/audience-view-dev-groups:4", 
        "arn:aws:ecs:eu-west-1:543573289192:task-definition/audience-view-dev-groups:5", 
        "arn:aws:ecs:eu-west-1:543573289192:task-definition/audience-view-dev-mock-api:1", 
        "arn:aws:ecs:eu-west-1:543573289192:task-definition/audience-view-dev-mock-responder:1"
    ]
}

From this I would like to only show;

audience-view-dev-api
audience-view-dev-groups
audience-view-dev-groups
audience-view-dev-mock-api
audience-view-dev-mock-responder

I believe if I pipe this to jq then I can get what I'm after? I am trying various combinations of; aws ecs list-task-definitions --region eu-west-1 | jq -r '.taskDefinitionArns[].<something_here>' to no avail

like image 555
eekfonky Avatar asked Jan 30 '23 09:01

eekfonky


1 Answers

Try this:

$ aws ecs list-task-definitions --region eu-west-1 \
  | jq -Mr '.taskDefinitionArns[] | split(":")[5] | split("/")[1]'

Expanded:

  .taskDefinitionArns[]     # for each taskDefinitionArns
| split(":")[5]             # take entry [5] after splitting by :
| split("/")[1]             # take entry [1] after splitting by /
like image 198
jq170727 Avatar answered Mar 04 '23 06:03

jq170727