设置
我正在尝试理解 jq 过滤器的工作原理,但是在处理嵌套数组时遇到了麻烦。使用下面的数据,我无法生成一个扁平的包含 5 个键值的输出。我可以得到一个键和四个空值,或者四个键和一个空值,但不是所有的五个键。
一个键和四个空值:
.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”选项来返回我要查找的格式:
出于各种原因,我不能使用 AWS CLI 中的“--query”选项,该选项可以返回我所需的格式:
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"
}
{ OwnerId } + (.Instances[] | { InstanceId,ImageId,PrivateIpAddress,Platform })
。其中包含了实例ID、镜像ID、私有IP地址和平台等信息。 - Jeff Mercado