如何使用jq从多个嵌套数组中提取键 [英] How to extract keys from multiple, nested arrays using jq
问题描述
设置
我试图弄清楚jq过滤器是如何工作的,并且在找出嵌套数组时遇到了麻烦.使用下面的数据,我无法输出固定的5键.我可以获得1个键和4个null或4个键和1个null,但不是全部5个键.
I'm trying to figure out how jq filters work and having trouble figuring out nested arrays. Using the data below I cannot make a flat 5 key output. I can get 1 key and 4 nulls or 4 keys and 1 null, but not all 5 keys.
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中使用"--query"选项,该选项的确会返回我要查找的格式:
For a various reasons I cannot use the "--query" option in AWS CLI which does return the format I'm looking for:
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产生以下输出?:
QUESTION Using the JSON input above, how do you use jq to produce the following output?:
{
"OwnerId": "000000000000",
"InstanceId": "i-v33333333",
"ImageId": "ami-44444444",
"PrivateIpAddress": "10.0.0.0",
"Platform": "windows"
}
推荐答案
您可以使用变量将OwnerID
存储为:
You can use a variable to store the OwnerID
as in:
.Reservations[] | .OwnerId as $OwnerId | ( .Instances[] | { "OwnerId": $OwnerId, InstanceId, ImageId, PrivateIpAddress, Platform} )
这篇关于如何使用jq从多个嵌套数组中提取键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!