Getting Started with JQ to Manipulating Outputs from the AWS CLI — Notes

I know jq is far more powerful than what is represented here. This is an attempt to have a place to refer back to since I always have to refamiliarize myself with the syntax and logic each time I revisit jq.

I hope that this helps someone else in getting started with AWS or jq.

AWS has a query command built into the CLI and can be used in conjunction with jq. This is not addressed below as this is intended to be a point from which to get started.

The base request from AWS CLI to get the EBS volumes in an account.

aws ec2 describe-volumes

The base request for all EBS volumes piped to jq. The “.” designates the root of the data being piped into it. No change in the data, just the addition of visual cues with color. The elements are in an array named Volumes.

aws ec2 describe-volumes | jq .

The specification of the array to get a list of the elements in the Volumes array. This still shows the JSON elements designating an array.

aws ec2 describe-volumes | jq .Volumes

Pulling the [] into the query removes them from the output and returns the list of elements without being included in an array.

aws ec2 describe-volumes | jq .Volumes[]

Return values associated with a specific key in the results at the root level of each object.

aws ec2 describe-volumes | jq .Volumes[].VolumeId

Identifying elements that match a specific value available in the State key shared across all items in the list.

aws ec2 describe-volumes | jq ‘.Volumes[] | select(.State==”available”)’

Specifying further to get specific values of keys from within a select statement.

aws ec2 describe-volumes | jq ‘.Volumes[] | select(.State==”available”)’.VolumeId

Remove extra characters using the tr command for easy consumption into variables for scripts.

aws ec2 describe-volumes | jq ‘.Volumes[] | select(.State==”available”)’.VolumeId | tr -d \”

Returning elements nested within other elements

aws ec2 describe-volumes | jq .Volumes[].Attachments

Remove extra characters using the tr command for easy consumption into variables for scripts.

aws ec2 describe-volumes | jq ‘.Volumes[].Attachments’ | tr -d \][

Feel free to provide more and I’ll include it in the next. Cheers.

Modernizing companies’ AWS security and governance programs at scale.