Search code examples
jsonamazon-web-servicesaws-cli

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'

?


Solution

  • 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/