11

I'm using the AWS CLI client to get the status of a snapshot but the output is in JSON. eg.

{
    "DBClusterSnapshots": [
        {
            "AvailabilityZones": [
                "us-east-2a",
                "us-east-2b",
                "us-east-2c"
            ],
            "DBClusterSnapshotIdentifier": "...",
            "DBClusterIdentifier": "...",
            "SnapshotCreateTime": "2021-12-23T05:59:41.658000+00:00",
            "Engine": "aurora",
            "AllocatedStorage": 517,
            "Status": "copying",
            "Port": 0,
            "ClusterCreateTime": "2020-01-17T18:59:19.045000+00:00",
            "MasterUsername": "...",
            "EngineVersion": "5.6.mysql_aurora.1.22.1",
            "LicenseModel": "aurora",
            "SnapshotType": "manual",
            "PercentProgress": 0,
            "StorageEncrypted": true,
            "KmsKeyId": "...",
            "DBClusterSnapshotArn": "...",
            "SourceDBClusterSnapshotArn": "...",
            "IAMDatabaseAuthenticationEnabled": false,
            "TagList": []
        }
    ]
}

I can use a combination of grep and sed (| grep Status | sed 's/.*"Status": "//' | sed 's/",//') to isolate the status of "copying" BUT I was wondering if there was an easier way to parse the JSON in bash. eg. var['DBClusterSnapshots'][0]['Status']

2 Answers 2

22

The AWS CLI tools have a built-in --query parameter that accepts a JMESPath expression to select a subset of the JSON output.

Your example would look something like this:

aws rds describe-db-cluster-snapshots --query "DBClusterSnapshots[0].Status"

The above command may produce quoted output like "copying" (with the quotes included), because the AWS CLI tools generate JSON literals by default.

If you want just the bare text copying (without quotes), add --output text to the above command line.

4
  • 1
    @IsmaelMiguel - the "best" answer is ultimately in the eye of the beholder BUT seeing as how grnch is a new user whereas user1686 isn't (and indeed, has a huuuuge amount of rep points) I've gone ahead and switched my answer. This'll get grnch a few badges that are completely new to him whereas user1686 has enough badges to last a lifetime lol
    – neubert
    Commented Dec 25, 2021 at 5:07
  • 2
    @neubert You should mark the answer that you think that helps you the most, and now to give reputation. For that, there's bounties. But, I agree with the outcome, as this answer uses the tool you were already using, to give you the data that you need. Commented Dec 25, 2021 at 5:11
  • 1
    @IsmaelMiguel - both answers have their uses. For AWS this answer is better and doesn't require any deps. jq and jshon are not installed by default on Ubuntu 20.04 and the scripting language approach works but isn't as succinct as others. BUT for general JSON parsing the other answer is better. tbh it's a tossup as to which is "better". Generally in situations such as these I just go with the first person I awarded the answer to but ultimately how I deal with it probably depends on my mood lol
    – neubert
    Commented Dec 25, 2021 at 5:15
  • 1
    Ha! Thanks @neubert, I appreciate it. I did wake up to a bunch of new badges this morning. :)
    – grnch
    Commented Dec 25, 2021 at 18:17
30

Yes, there are several different tools that have a full JSON parser and some form of query language (along the lines of XML having XPath).

  • jq -r .DBClusterSnapshots[0].Status

  • jshon -e DBClusterSnapshots -e 0 -e Status -u

But also nothing really stops you from writing a one-liner script in a language that does have a built-in JSON parser and outputs the wanted data:

  • python -c "import sys, json; data = json.load(sys.stdin); print(data['DBClusterSnapshots'][0]['Status'])"

  • perl -MJSON -E '$/=undef; $data=decode_json(<>); say $data->{DBClusterSnapshots}->[0]->{Status};'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .