[Cialug] JSON Querying

Adam Shannon adamkshannon at gmail.com
Thu Jun 14 14:08:00 UTC 2018


You can select just instnaceId like so:

$ jq .Reservations[].Instances[] /tmp/aws.json | jq 'select(
.BlockDeviceMappings | length <= 2)' | jq -r .InstanceId
i-0fcf111d64bc97558

On Thu, Jun 14, 2018 at 9:06 AM, Adam Shannon <adamkshannon at gmail.com>
wrote:

> Does this work for you?
>
> $ jq .Reservations[].Instances[] /tmp/aws.json | jq 'select(
> .BlockDeviceMappings | length <= 2)'
> {
>   "Monitoring": {
>     "State": "enabled"
>   },
>   "PublicDnsName": "ec2-xx-xx-x-xx.name-1.amazonaws.com",
>   "State": {
>     "Code": 16,
>     "Name": "running"
>   },
>   "LaunchTime": "2018-06-13T12:00:20.000Z",
>   "PublicIpAddress": "xx.xx.x.xx",
>   "PrivateIpAddress": "xx.xx.x.xx",
>   "VpcId": "vpc-rjf271d9",
>   "CpuOptions": {
>     "CoreCount": 2,
>     "ThreadsPerCore": 2
>   },
>   "StateTransitionReason": "",
>   "InstanceId": "i-0fcf111d64bc97558",
>   "ImageId": "ami-60812a1d",
>   "PrivateDnsName": "ip-xx-xx-x-xx.ec2.internal",
>   "SubnetId": "subnet-xxxxxxxxxx",
>   "InstanceType": "m4.xlarge",
>   "SourceDestCheck": true,
>   "Hypervisor": "xen",
>   "BlockDeviceMappings": [
>     {
>       "DeviceName": "/dev/sda1",
>       "Ebs": {
>         "Status": "attached",
>         "DeleteOnTermination": true,
>         "VolumeId": "vol-xxxxxxxxxxx",
>         "AttachTime": "2018-04-09T19:41:10.000Z"
>       }
>     }
>   ],
>   "Architecture": "x86_64",
>   "RootDeviceType": "ebs",
>   "RootDeviceName": "/dev/sda1",
>   "VirtualizationType": "hvm",
>   "AmiLaunchIndex": 1
> }
>
>
>
> On Thu, Jun 14, 2018 at 8:34 AM, Todd Walton <tdwalton at gmail.com> wrote:
>
>> I'm having trouble figuring out this JSON query using jq. Maybe someone
>> here can help?
>>
>> I do a 'aws ec2 describe-instances' and I get back a JSON document (pasted
>> in below, truncated for clarity). So now I want to use jq to return the
>> InstanceId of any instance with less than 2 BlockDeviceMappings. You'll
>> notice that the first instance, i-0f7771a48c88ec8fc, has 3 block device
>> mappings: /dev/sda1, /dev/xvdcp, and /dev/xvdcc, while the second
>> instance,
>> i-0fcf111d64bc97558, only has 1, /dev/sda1. I want to pass all that
>> through
>> jq such that I return the InstanceId of the second one.
>>
>> Any suggestions? I'd take a non-jq suggestion if it were possible.
>>
>> References:
>> https://stedolan.github.io/jq/
>> https://jqplay.org/
>>
>> The doc:
>>
>> {
>>   "Reservations": [
>>     {
>>       "Instances": [
>>         {
>>           "Monitoring": {
>>             "State": "enabled"
>>           },
>>           "PublicDnsName": "ec2-xx-xx-x-xx.name-1.amazonaws.com",
>>           "State": {
>>             "Code": 16,
>>             "Name": "running"
>>           },
>>           "LaunchTime": "2018-06-13T12:00:20.000Z",
>>           "PublicIpAddress": "xx.xx.x.xx",
>>           "PrivateIpAddress": "xx.xx.x.xx",
>>           "VpcId": "vpc-rjf271d9",
>>           "CpuOptions": {
>>             "CoreCount": 2,
>>             "ThreadsPerCore": 2
>>           },
>>           "StateTransitionReason": "",
>>           "InstanceId": "i-0f7771a48c88ec8fc",
>>           "ImageId": "ami-60812a1d",
>>           "PrivateDnsName": "ip-xx-xx-x-xx.ec2.internal",
>>           "SubnetId": "subnet-xxxxxxxxxx",
>>           "InstanceType": "m4.xlarge",
>>           "SourceDestCheck": true,
>>           "Hypervisor": "xen",
>>           "BlockDeviceMappings": [
>>             {
>>               "DeviceName": "/dev/sda1",
>>               "Ebs": {
>>                 "Status": "attached",
>>                 "DeleteOnTermination": true,
>>                 "VolumeId": "vol-xxxxxxxxxxx",
>>                 "AttachTime": "2018-04-09T19:41:10.000Z"
>>               }
>>             },
>>             {
>>               "DeviceName": "/dev/xvdcp",
>>               "Ebs": {
>>                 "Status": "attached",
>>                 "DeleteOnTermination": false,
>>                 "VolumeId": "vol-xxxxxxxxxxx",
>>                 "AttachTime": "2018-05-17T12:03:23.000Z"
>>               }
>>             },
>>             {
>>               "DeviceName": "/dev/xvdcc",
>>               "Ebs": {
>>                 "Status": "attached",
>>                 "DeleteOnTermination": false,
>>                 "VolumeId": "vol-xxxxxxxxxxx",
>>                 "AttachTime": "2018-05-18T19:34:56.000Z"
>>               }
>>             }
>>           ],
>>           "Architecture": "x86_64",
>>           "RootDeviceType": "ebs",
>>           "RootDeviceName": "/dev/sda1",
>>           "VirtualizationType": "hvm",
>>           "AmiLaunchIndex": 0
>>         },
>>         {
>>           "Monitoring": {
>>             "State": "enabled"
>>           },
>>           "PublicDnsName": "ec2-xx-xx-x-xx.name-1.amazonaws.com",
>>           "State": {
>>             "Code": 16,
>>             "Name": "running"
>>           },
>>           "LaunchTime": "2018-06-13T12:00:20.000Z",
>>           "PublicIpAddress": "xx.xx.x.xx",
>>           "PrivateIpAddress": "xx.xx.x.xx",
>>           "VpcId": "vpc-rjf271d9",
>>           "CpuOptions": {
>>             "CoreCount": 2,
>>             "ThreadsPerCore": 2
>>           },
>>           "StateTransitionReason": "",
>>           "InstanceId": "i-0fcf111d64bc97558",
>>           "ImageId": "ami-60812a1d",
>>           "PrivateDnsName": "ip-xx-xx-x-xx.ec2.internal",
>>           "SubnetId": "subnet-xxxxxxxxxx",
>>           "InstanceType": "m4.xlarge",
>>           "SourceDestCheck": true,
>>           "Hypervisor": "xen",
>>           "BlockDeviceMappings": [
>>             {
>>               "DeviceName": "/dev/sda1",
>>               "Ebs": {
>>                 "Status": "attached",
>>                 "DeleteOnTermination": true,
>>                 "VolumeId": "vol-xxxxxxxxxxx",
>>                 "AttachTime": "2018-04-09T19:41:10.000Z"
>>               }
>>             }
>>           ],
>>           "Architecture": "x86_64",
>>           "RootDeviceType": "ebs",
>>           "RootDeviceName": "/dev/sda1",
>>           "VirtualizationType": "hvm",
>>           "AmiLaunchIndex": 1
>>         }
>>       ]
>>     }
>>   ]
>> }
>>
>> --
>> Todd
>> _______________________________________________
>> Cialug mailing list
>> Cialug at cialug.org
>> http://cialug.org/mailman/listinfo/cialug
>>
>
>


More information about the Cialug mailing list