pkaramol
pkaramol

Reputation: 19402

Filter out specific json array fields of elements using jq

I run a aws rds describe-instances and I got a json array of the instances more or less as follows:

"DBInstances": [
        {
            "DBInstanceIdentifier": "my-rds",
            "Endpoint": {
                "HostedZoneId": "XXXXXXXXXXXX"
            },
            "DBSecurityGroups": [],
            "VpcSecurityGroups": [
                {
                    "VpcSecurityGroupId": "sg-12345",
                    "Status": "active"
                },
                {
                    "VpcSecurityGroupId": "sg-12345",
                    "Status": "active"
                }
            ],
            "DBParameterGroups": [
                {
                    "DBParameterGroupName": "postgres-12",
                    "ParameterApplyStatus": "in-sync"
                }
            ],
            "AvailabilityZone": "us-west-1c",
            "DBSubnetGroup": {
                "VpcId": "vpc-123456",
                "SubnetGroupStatus": "Complete",
                "Subnets": [
                    {
                        "SubnetIdentifier": "subnet-123456",
                        "SubnetAvailabilityZone": {
                            "Name": "us-west-1b"
                        },
                        "SubnetOutpost": {},
                        "SubnetStatus": "Active"
                    },
                    {
                        "SubnetIdentifier": "subnet-12345",
                        "SubnetAvailabilityZone": {
                            "Name": "us-west-1c"
                        },
                        "SubnetOutpost": {},
                        "SubnetStatus": "Active"
                    },
                    {
                        "SubnetIdentifier": "subnet-12345",
                        "SubnetAvailabilityZone": {
                            "Name": "us-west-1e"
                        },
                        "SubnetOutpost": {},
                        "SubnetStatus": "Active"
                    }
                ]
            },
            "EngineVersion": "11.9",
            "AutoMinorVersionUpgrade": true,
            "ReadReplicaDBInstanceIdentifiers": [],
            "LicenseModel": "foobar",
            "OptionGroupMemberships": [
                {
                    "OptionGroupName": "randomname",
                    "Status": "in-sync"
                }
            ],
        },
    }

output truncated obviously.

Is there a way using (jq probably?) to print:

a) all elements of the DBInstances array

and for each element print only

b) the DBInstanceIdentifier and the EngineVersion?

Upvotes: 0

Views: 258

Answers (2)

0stone0
0stone0

Reputation: 44162

You can use the following JQ filter:

.DBInstances[] | [ .DBInstanceIdentifier, .EngineVersion ]

Where

  • .DBInstances[] Loops over the DBInstances array
  • [ .DBInstanceIdentifier, .EngineVersion ] creates an array containing only the DBInstanceIdentifier and EngineVersion keys
jq '.DBInstances[] | [ .DBInstanceIdentifier, .EngineVersion ]'
JqPlay Demo

As of your current JSON Example (with only 1 item in the DBInstances Array) the result is:

[
  "my-rds",
  "11.9"
]

Upvotes: 1

pkaramol
pkaramol

Reputation: 19402

Solved, not with .jq, but with an aws cli feature:

aws rds describe-db-instances --query 'DBInstances[*].[DBInstanceIdentifier, EngineVersion]' 

Upvotes: 1

Related Questions