Bash JSON Get Length of Array

Viewed 4593

AWS CLI returns an empty array on filter. I want to be able to count the number of elements in that array:

{ "Reservations": [] }

vs

{ "Reservations": [ { "OwnerId": "124531353552" ...... } ] }

So if there are no results I should get back 0 (the first example).

2 Answers

tl;dr: On mac, install jq first via brew. brew install jq. Run your query as you did before by adding the pipe jq command. Eg: aws batch list-jobs --job-queue MY-QUEUE-NAME --job-status RUNNABLE | jq '.jobSummaryList | length'

Explanation aws batch list-jobs --job-queue MY-QUEUE-NAME --job-status RUNNABLE when running this command the response is in the format of

{"jobSummaryList": []}

So adding the | jq '.jobSummaryList | length' gives me the count of object inside that list. And for you it will be 'Reservations'

Related