Native AWS CLI solution
If you'd really like to get compact JSON without an additional program, you can use JMESPath's to_string
function in the an AWS command's --query
client-side filter arg to serialize into compact JSON, and then --output=text
to avoid double-quoting it.
Example
$ aws ec2 describe-regions --query='to_string(@)' --output=text
{"Regions":[{"Endpoint":"ec2.ap-south-1.amazonaws.com","RegionName":"ap-south-1","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.eu-north-1.amazonaws.com","RegionName":"eu-north-1","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.eu-west-3.amazonaws.com","RegionName":"eu-west-3","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.eu-west-2.amazonaws.com","RegionName":"eu-west-2","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.eu-west-1.amazonaws.com","RegionName":"eu-west-1","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.ap-northeast-3.amazonaws.com","RegionName":"ap-northeast-3","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.ap-northeast-2.amazonaws.com","RegionName":"ap-northeast-2","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.ap-northeast-1.amazonaws.com","RegionName":"ap-northeast-1","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.ca-central-1.amazonaws.com","RegionName":"ca-central-1","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.sa-east-1.amazonaws.com","RegionName":"sa-east-1","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.ap-southeast-1.amazonaws.com","RegionName":"ap-southeast-1","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.ap-southeast-2.amazonaws.com","RegionName":"ap-southeast-2","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.eu-central-1.amazonaws.com","RegionName":"eu-central-1","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.us-east-1.amazonaws.com","RegionName":"us-east-1","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.us-east-2.amazonaws.com","RegionName":"us-east-2","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.us-west-1.amazonaws.com","RegionName":"us-west-1","OptInStatus":"opt-in-not-required"},{"Endpoint":"ec2.us-west-2.amazonaws.com","RegionName":"us-west-2","OptInStatus":"opt-in-not-required"}]}
Important caveat from the AWS CLI docs
⚠️ Important
The output type you specify changes how the --query
option operates:
If you specify --output text
, the output is paginated before the --query
filter is applied, and the AWS CLI runs the query once on each page of the output. Due to this, the query includes the first matching element on each page which can result in unexpected extra output. To additionally filter the output, you can use other command line tools such as head
or tail
.
If you specify --output json
, the output is completely processed as a single, native structure before the --query
filter is applied. The AWS CLI runs the query only once against the entire structure, producing a filtered result that is then output.
This means that technically this approach will yield you one line of JSON per page of results from the server.
If you would only like to fetch one page to begin with, you can pass the --no-paginate
option to disable the AWS CLI's automatic fetch-all-pages feature.