SETUP
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 key, 4 nulls:
.Reservations[] | {OwnerId, InstanceId, ImageId, PrivateIpAddress, Platform}
Returns:
{
"OwnerId": "000000000000",
"InstanceId": null,
"ImageId": null,
"PrivateIpAddress": null,
"Platform": null
}
4 keys, 1 null:
.Reservations[].Instances[] | {OwnerId, InstanceId, ImageId, PrivateIpAddress, Platform}
{
"OwnerId": null,
"InstanceId": "i-v33333333",
"ImageId": "ami-44444444",
"PrivateIpAddress": "10.0.0.0",
"Platform": "windows"
}
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}'
Output:
[
{
"InstanceId": "i-11111111",
"IP": "10.9.9.3",
"OwnerId": "111111111111",
"Platform": windows
},
{
"InstanceId": "i-22222222",
"IP": "10.0.0.0",
"OwnerId": "111111111111",
"Platform": windows
}
]
Here is the JSON input:
{
"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"
}
]
}
]
}
]
}
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"
}
You can use a variable to store the OwnerID
as in:
.Reservations[] | .OwnerId as $OwnerId | ( .Instances[] | { "OwnerId": $OwnerId, InstanceId, ImageId, PrivateIpAddress, Platform} )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With