RagePwn
RagePwn

Reputation: 421

Using jq select to filter json not working

I'm trying to grab a property from a json document using jq. I want to get the latest snapshot with a specific name. Here is the jq statment in bash, which works fine until I add the select with filter

snapid=aws redshift describe-cluster-snapshots --region us-west-2 | jq'.[] | select(.ClusterIdentifier=="dev-cluster") | max_by(.SnapshotCreateTime) | .SnapshotIdentifier'

Here is the json doc I'm pulling from. There are more than one snapshot entries but this is the one I'm targeting.

{
    "Snapshots": [        
        {
                "EstimatedSecondsToCompletion": 0, 
                "OwnerAccount": "45645641155", 
                "CurrentBackupRateInMegaBytesPerSecond": 6.2857, 
                "ActualIncrementalBackupSizeInMegaBytes": 22.0, 
                "NumberOfNodes": 3, 
                "Status": "available", 
                "VpcId": "myvpc", 
                "ClusterVersion": "1.0", 
                "Tags": [], 
                "MasterUsername": "ayxbizops", 
                "TotalBackupSizeInMegaBytes": 192959.0, 
                "DBName": "dev", 
                "BackupProgressInMegaBytes": 22.0, 
                "ClusterCreateTime": "2016-09-06T15:56:08.170Z", 
                "RestorableNodeTypes": [
                    "dc1.large"
                ], 
                "EncryptedWithHSM": false, 
                "ClusterIdentifier": "dev-cluster", 
                "SnapshotCreateTime": "2016-09-06T16:00:25.595Z", 
                "AvailabilityZone": "us-west-2c", 
                "NodeType": "dc1.large", 
                "Encrypted": false, 
                "ElapsedTimeInSeconds": 3, 
                "SnapshotType": "manual", 
                "Port": 5439, 
                "SnapshotIdentifier": "thismorning"
        }
    ]
}

I get the error, Cannot index array with string "ClusterIdentifier".

Upvotes: 3

Views: 2359

Answers (1)

Thedward
Thedward

Reputation: 1472

How about this?

jq '.[]|map(select(.ClusterIdentifier=="dev-cluster"))
    |max_by(.SnapshotCreateTime)|.SnapshotIdentifier'

Upvotes: 1

Related Questions