tset
tset

Reputation: 443

Shell command to return value in json output

How to return a particular value using shell command? In the following example I would like to query to return the value of "StackStatus" which is "CREATE_COMPLETE"

Here is the command:

aws cloudformation describe-stacks --stack-name stackname

Here is the output:

{
        "Stacks": [{
            "StackId": "arn:aws:cloudformation:ap-southeast-2:64560756805470:stack/stackname/8c8e3330-9f35-1er6-902e-50fae94f3fs42",
            "Description": "Creates base IAM roles and policies for platform management",
            "Parameters": [{
                "ParameterValue": "64560756805470",
                "ParameterKey": "PlatformManagementAccount"
            }],
            "Tags": [],

            "CreationTime": "2016-10-31T06:45:02.305Z",
            "Capabilities": [
                "CAPABILITY_IAM"
            ],
            "StackName": "stackname",
            "NotificationARNs": [],
            "StackStatus": "CREATE_COMPLETE",
            "DisableRollback": false
        }]
    }

Upvotes: 0

Views: 630

Answers (1)

strobelight
strobelight

Reputation: 267

The aws cli supports the --query option to get parts. In addition you could also pipe to another command line tool, jq to do similar query.

But in aws notation to get the 1st result:

aws cloudformation describe-stacks --stack-name stackname --query 'Stacks[0].StackStatus' --output text

Based on the above output, Stacks is an array of objects (a key/value), so hence need the [0] to get the 1st element of the array, and then .StackStatus is a key in this object containing a string as value. The --output text simply presents the output as simple text value instead of a json-looking object.

Edited per Charles comment.

Upvotes: 2

Related Questions