Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get single-line json from aws cli

I have this command:

aws ec2 describe-regions --output=json

the output looks like:

{
    "Regions": [
        {
            "Endpoint": "ec2.eu-north-1.amazonaws.com", 
            "RegionName": "eu-north-1"
        }, 
        {
            "Endpoint": "ec2.ap-northeast-2.amazonaws.com", 
            "RegionName": "ap-northeast-2"
        }, 
        {
            "Endpoint": "ec2.us-west-2.amazonaws.com", 
            "RegionName": "us-west-2"
        }
    ]
}

that's not really usable by machine though, I am looking for output on a single-line like so:

{"Regions":[{"Endpoint":"ec2.eu-north-1.amazonaws.com","RegionName":"eu-north-1"},{"Endpoint":"ec2.ap-northeast-2.amazonaws.com","RegionName":"ap-northeast-2"},{"Endpoint":"ec2.us-west-2.amazonaws.com","RegionName":"us-west-2"}]}

is there some command with the aws-cli that gives me machine-readable JSON? Something like:

aws ec2 describe-regions --output='json-for-machines'

?

like image 765
Alexander Mills Avatar asked Dec 22 '22 23:12

Alexander Mills


1 Answers

Use jq to compact it like so.

aws ec2 describe-regions --output=json| jq -c

output becomes

{"Regions":[{"Endpoint":"ec2.eu-north-1.amazonaws.com","RegionName":"eu-north-1"},{"Endpoint":"ec2.ap-northeast-2.amazonaws.com","RegionName":"ap-northeast-2"},{"Endpoint":"ec2.us-west-2.amazonaws.com","RegionName":"us-west-2"}]}

https://stedolan.github.io/jq/

like image 84
al76 Avatar answered Jan 01 '23 18:01

al76