0

I am new to jq and I have a scenario to get the parent(at a particular level) of any matching value in the json file. Below is the sample data.

{
    "DevOps": {
        "us-east-1": {
            "i-049aa9a3bddb4****": {
                "AmiLaunchIndex": 0,
                "ImageId": "ami-00068cd7555f543d5",
                "InstanceId": "i-049aa9a3bddb4*****",
                "InstanceType": "t2.nano",
                "LaunchTime": "2019-11-24 18:39:25+00:00",
                "Monitoring": {
                    "State": "disabled"
                },
                "Placement": {
                    "AvailabilityZone": "us-east-1a",
                    "GroupName": "",
                    "Tenancy": "default"
                },
                "PrivateDnsName": "ip-10-250-128-77.ec2.internal",
                "PrivateIpAddress": "10.250.128.77",
                "ProductCodes": [],
                "PublicDnsName": "",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "Network-Test-Host"
                    }
                ],

            },
            "i-0368117434*****": {
                "AmiLaunchIndex": 0,
                "ImageId": "ami-078137ecf12552edf",
                "InstanceId": "i-0368117434******",
                "InstanceType": "c5.2xlarge",
                "KeyName": "infra-systems-prod-key",
                "LaunchTime": "2020-02-13 15:51:15+00:00",
                "Monitoring": {
                    "State": "disabled"
                },
                "Placement": {
                    "AvailabilityZone": "us-east-1a",
                    "GroupName": "",
                    "Tenancy": "default"
                },
                "PrivateDnsName": "ip-10-250-138-161.ec2.internal",
                "PrivateIpAddress": "10.250.138.161",
                "ProductCodes": [],
                "PublicDnsName": "",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "Network-Test-Host"
                    }
                ],

            },
        }
    }
}

For example, if my search value is "10.250.128.77", it should fetch the parent - "i-049aa9a3bddb4****". if the value is "Network-Test-Host", it should give me the list of 2 instance ids(i-049aa9a3bddb4****, i-0368117434*****). Then from this point, I would be able to traverse at any key and get the respective value like the hostname, ImageId, Instance Type

Any help on this is much appreciated

1 Answer 1

1

Your query using a particular private IP address:

jq '.DevOps."us-east-1" | map_values(select( .PrivateIpAddress == "10.250.128.77" ))' file

Parametrised to take the query IP address from the command line arguments:

jq --arg addr 10.250.128.77 \
    '.DevOps."us-east-1" | map_values(select( .PrivateIpAddress == $addr ))' file

Your query using a particular Key tag's Value:

jq '.DevOps."us-east-1" | map_values(select( IN(.Tags[]; {Key: "Name", Value: "Network-Test-Host"}) ))' file

Parametrised to take the Key and the Value from the command line arguments:

jq --arg Key Name --arg Value Network-Test-Host \
    '.DevOps."us-east-1" | map_values(select( IN(.Tags[]; $ARGS.named) ))' file

These commands would generate a JSON document with a single object containing the keys+values from your input document's .DevOps."us-east-1" section that matches the queries.

Common for each command is that they apply a select() statement to the values of the .DevOps."us-east-1" section. The boolean expression will be different depending on what subsection you want to test with the query. The sections where the boolean expression evaluates to true will be kept while all other bits are removed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .