Skip to content

Querying complex json #53

@dre2004

Description

@dre2004

Hi,

I'm very new to Go and I stumbled across this module which I think is fantastic! I have a question more than issue which I'm hoping you can help with.

I have some very complicated and nested json (json output of aws describe instances) which I'm trying to query using gojsonq.

I've added an obfuscated version of the output below, essentially what I'm trying to do is query the Reservations.[].Instances[] to grab some details.

For example the payload below has details for two instances, I'm trying to get the InstanceIds as follows (without any luck).

instances := gojsonq.New().File("instances.json")
res := instances.From("Reservations.[].Instances.[]").Only("InstanceId")

What am I doing wrong? or is the complexity the issue?

{ "NextToken": null, "Reservations": [{ "Groups": null, "Instances": [{ "AmiLaunchIndex": 0, "Architecture": "x86_64", "BlockDeviceMappings": [{ "DeviceName": "/dev/sda1", "Ebs": { "AttachTime": "2017-01-10T12:59:09Z", "DeleteOnTermination": true, "Status": "attached", "VolumeId": "vol-085817e5af91e75ef" } }], "CapacityReservationId": null, "CapacityReservationSpecification": { "CapacityReservationPreference": "open", "CapacityReservationTarget": null }, "CpuOptions": { "CoreCount": 1, "ThreadsPerCore": 1 }, "EbsOptimized": false, "ElasticGpuAssociations": null, "ElasticInferenceAcceleratorAssociations": null, "EnaSupport": null, "HibernationOptions": { "Configured": false }, "Hypervisor": "xen", "ImageId": "ami-ae6259cd", "InstanceId": "i-abcdefg1234", "InstanceLifecycle": null, "InstanceType": "t2.small", "KernelId": null, "KeyName": "obfs", "LaunchTime": "2018-04-23T13:18:58Z", "Licenses": null, "Monitoring": { "State": "disabled" }, "NetworkInterfaces": [{ "Association": null, "Attachment": { "AttachTime": "2018-01-10T12:59:09Z", "AttachmentId": "eni-attach-e6303189", "DeleteOnTermination": true, "DeviceIndex": 0, "Status": "attached" }, "Description": "Primary network interface", "Groups": [{ "GroupId": "sg-obfs", "GroupName": "obfs" }], "InterfaceType": "interface", "Ipv6Addresses": null, "MacAddress": "02:1a:4b:29:c9:1f", "NetworkInterfaceId": "eni-abcdefg", "OwnerId": "0000000000", "PrivateDnsName": "ip-172-0-0-1.ap-southeast-1.compute.internal", "PrivateIpAddress": "172.0.0.1", "PrivateIpAddresses": [{ "Association": null, "Primary": true, "PrivateDnsName": "ip-172-0-0-1.ap-southeast-1.compute.internal", "PrivateIpAddress": "172.0.0.1" }], "SourceDestCheck": true, "Status": "in-use", "SubnetId": "subnet-obfs", "VpcId": "vpc-obfs" }], "Placement": { "Affinity": null, "AvailabilityZone": "ap-southeast-1a", "GroupName": "", "HostId": null, "PartitionNumber": null, "SpreadDomain": null, "Tenancy": "default" }, "Platform": null, "PrivateDnsName": "ip-172-0-0-1.ap-southeast-1.compute.internal", "PrivateIpAddress": "172.0.0.1", "ProductCodes": null, "PublicDnsName": "", "PublicIpAddress": null, "RamdiskId": null, "RootDeviceName": "/dev/sda1", "RootDeviceType": "ebs", "SecurityGroups": [{ "GroupId": "sg-obfs", "GroupName": "obfs" }], "SourceDestCheck": true, "SpotInstanceRequestId": null, "SriovNetSupport": null, "State": { "Code": 80, "Name": "stopped" }, "StateReason": { "Code": "Client.UserInitiatedShutdown", "Message": "Client.UserInitiatedShutdown: User initiated shutdown" }, "StateTransitionReason": "User initiated (2018-04-23 13:38:03 GMT)", "SubnetId": "subnet-b8a736dc", "Tags": [{ "Key": "new", "Value": "tag" }, { "Key": "Name", "Value": "obfs" }, { "Key": "environment", "Value": "DEV" }], "VirtualizationType": "hvm", "VpcId": "vpc-obfs" }], "OwnerId": "00000000000", "RequesterId": null, "ReservationId": "r-012456789" }, { "Groups": null, "Instances": [{ "AmiLaunchIndex": 0, "Architecture": "x86_64", "BlockDeviceMappings": [{ "DeviceName": "/dev/xvda", "Ebs": { "AttachTime": "2018-04-15T11:35:14Z", "DeleteOnTermination": true, "Status": "attached", "VolumeId": "vol-30234567890" } }], "CapacityReservationId": null, "CapacityReservationSpecification": { "CapacityReservationPreference": "open", "CapacityReservationTarget": null }, "ClientToken": "", "CpuOptions": { "CoreCount": 1, "ThreadsPerCore": 1 }, "EbsOptimized": false, "EnaSupport": true, "HibernationOptions": { "Configured": false }, "Hypervisor": "xen", "IamInstanceProfile": null, "ImageId": "ami-001122334455aa1bbb", "InstanceId": "i-9876543210", "InstanceLifecycle": null, "InstanceType": "t2.micro", "KernelId": null, "KeyName": "obfs", "LaunchTime": "2019-04-15T11:35:13Z", "Licenses": null, "Monitoring": { "State": "disabled" }, "NetworkInterfaces": [{ "Association": null, "Attachment": { "AttachTime": "2019-04-15T11:35:13Z", "AttachmentId": "eni-attach-01331224f2b57b180", "DeleteOnTermination": true, "DeviceIndex": 0, "Status": "attached" }, "Description": "", "Groups": [{ "GroupId": "sg-obfs", "GroupName": "obfs1" }], "InterfaceType": "interface", "Ipv6Addresses": null, "MacAddress": "02:8a:6f:31:7c:dc", "NetworkInterfaceId": "eni-obfs", "OwnerId": "0000000000", "PrivateDnsName": "ip-172-0-0-1.ap-southeast-1.compute.internal", "PrivateIpAddress": "172.0.0.1", "PrivateIpAddresses": [{ "Association": null, "Primary": true, "PrivateDnsName": "ip-172-0-0-1.ap-southeast-1.compute.internal", "PrivateIpAddress": "172.0.0.1" }], "SourceDestCheck": true, "Status": "in-use", "SubnetId": "subnet-obfs", "VpcId": "vpc-obfs" }], "Placement": { "Affinity": null, "AvailabilityZone": "ap-southeast-1a", "GroupName": "", "HostId": null, "PartitionNumber": null, "SpreadDomain": null, "Tenancy": "default" }, "Platform": null, "PrivateDnsName": "ip-172-0-0-1.ap-southeast-1.compute.internal", "PrivateIpAddress": "172.0.0.1", "ProductCodes": null, "PublicDnsName": "", "PublicIpAddress": null, "RamdiskId": null, "RootDeviceName": "/dev/xvda", "RootDeviceType": "ebs", "SecurityGroups": [{ "GroupId": "sg-obfs", "GroupName": "obfs1" }], "SourceDestCheck": true, "SpotInstanceRequestId": null, "SriovNetSupport": null, "State": { "Code": 80, "Name": "stopped" }, "StateReason": { "Code": "Client.UserInitiatedShutdown", "Message": "Client.UserInitiatedShutdown: User initiated shutdown" }, "StateTransitionReason": "User initiated (2019-04-15 20:29:38 GMT)", "SubnetId": "subnet-obfs", "Tags": [{ "Key": "purpose", "Value": "dev" }, { "Key": "created", "Value": "2019-04-15" }, { "Key": "Name", "Value": "obfs1" }], "VirtualizationType": "hvm", "VpcId": "vpc-obfs" }], "OwnerId": "00000000000", "RequesterId": null, "ReservationId": "r-obfs" }] }

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions