如果键名与“Name”匹配,则使用 jq 提取键值

eek*_*nky 2 select json key aws-cli jq

仅当键名称为 时,我才需要使用 jq 提取键值Name。下面是一个例子。我有一些没有密钥名称的 AMI Name,我想忽略它们。

aws ec2 describe-snapshots --snapshot-id snap-123 --region eu-west-1 --profile myprofile
{
    "Snapshots": [
        {
            "Description": "AMI upgrader",
            "Tags": [
                {
                    "Value": "AMI upgrader",
                    "Key": "Name"
                }
            ],
            "Encrypted": false,
            "VolumeId": "vol-9356e811",
            "State": "completed",
            "VolumeSize": 20,
            "StartTime": "2018-05-31T13:58:31.000Z",
            "Progress": "100%",
            "OwnerId": "1234",
            "SnapshotId": "snap-1234"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我努力了;

aws ec2 describe-snapshots --snapshot-id snap-123 --region eu-west-1 --profile myprofile | jq -r '.Snapshots[].Tags[]|.Name?.Value'
Run Code Online (Sandbox Code Playgroud)

但它返回null

ogu*_*ail 5

您可以使用select

jq -r '.Snapshots[].Tags[] | select(.Key == "Name").Value'
Run Code Online (Sandbox Code Playgroud)