如何使用jq从多个嵌套数组中提取键

时间:2015-06-02 16:13:32

标签: json amazon-web-services jq

SETUP

我试图弄清楚jq过滤器是如何工作的,并且难以找出嵌套数组。使用下面的数据我无法做出平坦的5键输出。我可以获得1个键和4个空值或4个键和1个空值,但不是全部5个键。

1个键,4个空值:

.Reservations[] | {OwnerId, InstanceId, ImageId, PrivateIpAddress, Platform}

返回:

{
 "OwnerId": "000000000000",
 "InstanceId": null,
 "ImageId": null,
 "PrivateIpAddress": null,
 "Platform": null
}

4个键,1个空:

.Reservations[].Instances[] | {OwnerId, InstanceId, ImageId, PrivateIpAddress, Platform}

{
  "OwnerId": null,
  "InstanceId": "i-v33333333",
  "ImageId": "ami-44444444",
  "PrivateIpAddress": "10.0.0.0",
  "Platform": "windows"
}

由于种种原因,我无法使用" - 查询" AWS CLI中的选项确实返回了我正在寻找的格式:

aws ec2 describe-instances --region us-east-1 --profile temp --query 'Reservations[*].{InstanceId:Instances[0].InstanceId,IP:Instances[0].PrivateIpAddress,Platform:Instances[0].Platform,OwnerId:OwnerId}'

输出:

[
    {
        "InstanceId": "i-11111111",
        "IP": "10.9.9.3",
        "OwnerId": "111111111111",
        "Platform": windows
    },
    {
        "InstanceId": "i-22222222",
        "IP": "10.0.0.0",
        "OwnerId": "111111111111",
        "Platform": windows
    }
]

这是JSON输入:

   {
    "Reservations": [
        {
            "OwnerId": "000000000000",
            "ReservationId": "r-22222222",
            "Groups": [],
            "RequesterId": "111111111111",
            "Instances": [
                {
                    "Monitoring": {
                        "State": "enabled"
                    },
                    "PublicDnsName": null,
                    "State": {
                        "Code": 16,
                        "Name": "running"
                    },
                    "EbsOptimized": false,
                    "LaunchTime": "2015-04-10T00:02:02.000Z",
                    "Platform": "windows",
                    "PrivateIpAddress": "10.0.0.0",
                    "ProductCodes": [
                        {
                            "ProductCodeId": "0000000000000000000000000",
                            "ProductCodeType": "marketplace"
                        }
                    ],
                    "VpcId": "vpc-2222222",
                    "StateTransitionReason": null,
                    "InstanceId": "i-v33333333",
                    "ImageId": "ami-44444444",
                    "PrivateDnsName": "ip-10-0-0-0.aws.foobarcloud.com",
                    "KeyName": "bar-servicemesh",
                    "SecurityGroups": [
                        {
                            "GroupName": "bar-wildcard-dns-intranet-InstanceSecurityGroup-VN0DFQ13QCDY",
                            "GroupId": "sg-55555555"
                        }
                    ],
                    "ClientToken": "11111111-2222-3333-4444-555555555555_subnet-66666666_1",
                    "SubnetId": "subnet-66666666",
                    "InstanceType": "t2.medium",
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use",
                            "MacAddress": "00:00:00:00:00:77",
                            "SourceDestCheck": true,
                            "VpcId": "vpc-66666666",
                            "Description": null,
                            "NetworkInterfaceId": "eni-11111111"
                        }
                    ]
                }
            ]
        }
    ]
}

问题 使用上面的JSON输入,如何使用jq生成以下输出?:

{
  "OwnerId": "000000000000",
  "InstanceId": "i-v33333333",
  "ImageId": "ami-44444444",
  "PrivateIpAddress": "10.0.0.0",
  "Platform": "windows"
}

1 个答案:

答案 0 :(得分:10)

您可以使用变量来存储OwnerID,如下所示:

.Reservations[] | .OwnerId as $OwnerId | ( .Instances[] | { "OwnerId": $OwnerId, InstanceId, ImageId, PrivateIpAddress, Platform} )