greyhoundforty
greyhoundforty

Reputation: 239

Using jq to print out multiple elements in json

I am trying to get jq to return both the keyname as well as the isAvailable value. This offering is currently not available in every region that is being returned by the API so I would ideally like to be able to return something like the following:

[
  "AMSTERDAM03,
  "1"
]

Here is the json being returned by the API call.

[
    {
        "description": "AMS03 - Amsterdam",
        "keyname": "AMSTERDAM03",
        "location": {
            "location": {
                "id": 814994,
                "longName": "Amsterdam 3",
                "name": "ams03",
                "statusId": 2
            },
            "locationPackageDetails": [
                {
                    "isAvailable": 1,
                    "locationId": 814994,
                    "packageId": 737
                }
            ]
        },
        "sortOrder": 2
    }
]

If I run jq '.[] | .keyname' I get back the expected value, but I cannot seem to figure out the syntax for digging in to the lower layers.

Upvotes: 1

Views: 491

Answers (1)

Inian
Inian

Reputation: 85875

A simple jq filter like below would be sufficient,

command-producing-json | jq '.[] | [ .keyname, .location.locationPackageDetails[].isAvailable ]'

would produce below as expected.

[
  "AMSTERDAM03",
  1
]

You can lose the double-quotes by using the -r, --raw-output option.

Upvotes: 1

Related Questions