Kelly Norton
Kelly Norton

Reputation: 497

using jq to return multiple elements?

I'm trying to use jq to return multiple elements from a JSON object. The data is coming from an AWS cli lookup, this is just a tiny section of the output:

{
"ReservedInstancesOfferings": [{
        "OfferingClass": "convertible",
        "OfferingType": "No Upfront",
        "AvailabilityZone": "ap-southeast-2a",
        "InstanceTenancy": "default",
        "PricingDetails": [],
        "ProductDescription": "Linux/UNIX",
        "UsagePrice": 0.0,
        "RecurringCharges": [{
            "Amount": 0.167,
            "Frequency": "Hourly"
        }],
        "Marketplace": false,
        "CurrencyCode": "USD",
        "FixedPrice": 0.0,
        "Duration": 94608000,
        "Scope": "Availability Zone",
        "ReservedInstancesOfferingId": "1fc8c02b-bcc0-42b8-82ef-47c6f6d1c1b5",
        "InstanceType": "c4.xlarge"
    },
    {
        "OfferingClass": "convertible",
        "OfferingType": "No Upfront",
        "AvailabilityZone": "ap-southeast-2a",
        "InstanceTenancy": "dedicated",
        "PricingDetails": [],
        "ProductDescription": "Red Hat Enterprise Linux",
        "UsagePrice": 0.0,
        "RecurringCharges": [{
            "Amount": 0.243,
            "Frequency": "Hourly"
        }],
        "Marketplace": false,
        "CurrencyCode": "USD",
        "FixedPrice": 0.0,
        "Duration": 94608000,
        "Scope": "Availability Zone",
        "ReservedInstancesOfferingId": "24aaceee-f54e-4882-aba3-ce710a5036c9",
        "InstanceType": "c4.xlarge"
    }
]

}

I'm trying to extract the fields:

.ReservedInstancesOfferings[].ProductDescription
.ReservedInstancesOfferings[].InstanceType
.ReservedInstancesOfferings[].RecurringCharges[].Amount

I would like to output to appear like this:

"Red Hat Enterprise Linux"
"c4.xlarge"
0.183

As you can see above I understand how to extract all the elements individually, but I can't work out how to put them all together to attain the desired output. I've trued separating the queries with a comma, but that doesn't provide the desired output.

Any help would be very much appreciated.

Thank you!

EDIT:

Oh oh, I got it! Full answer is below. Boy oh boy jq is powerful!

Hopefully this helps someone :)

Upvotes: 3

Views: 2195

Answers (1)

Kelly Norton
Kelly Norton

Reputation: 497

OK, so I managed to get what I was after. I still have a little tidying up to do as some of the input fields aren't completely uniform, but it's close enough to move forward with:

aws ec2 describe-reserved-instances-offerings --availability-zone "ap-southeast-2a" --instance-type "c4.xlarge" |
jq '.ReservedInstancesOfferings[] | [.ProductDescription, .InstanceType, .RecurringCharges[].Amount]'

Output:

[
  "Linux/UNIX",
  "c4.xlarge",
  0.167
]
[
  "Red Hat Enterprise Linux",
  "c4.xlarge",
  0.243
]
[
  "Red Hat Enterprise Linux",
  "c4.xlarge",
  0.227
]
[
  "Linux/UNIX",
  "c4.xlarge",
  0.183
]
[
  "SUSE Linux",
  "c4.xlarge",
  0.2
]

Upvotes: 7

Related Questions