使用jq选择过滤json不起作用

时间:2016-09-06 22:37:52

标签: json bash jq

我试图使用jq从json文档中获取属性。我想获得具有特定名称的最新快照。这是bash中的jq语句,在我添加select with filter

之前一直正常工作
snapid=aws redshift describe-cluster-snapshots --region us-west-2 | jq'.[] | select(.ClusterIdentifier=="dev-cluster") | max_by(.SnapshotCreateTime) | .SnapshotIdentifier'

这是我从中抽出的json doc。有多个快照条目,但这是我的目标。

{
    "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"
        }
    ]
}

我收到错误,无法使用字符串索引数组" ClusterIdentifier"。

1 个答案:

答案 0 :(得分:0)

这个怎么样?

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