Harry Muscle
Harry Muscle

Reputation: 2387

Parse JSON Elements in Array with jq

I'm trying to figure out how to print just the attributes->preferences section in the following JSON example using jq. What's throwing me is that this is an array that contains multiple entries that are identified by an id key value pair and I need the attributes->preferences section just from the array element with id 1 without knowing the exact order of the array entries ahead of time.

[
  {
    "id": 1,
    "attributes": {
      "preferences": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3",
        "key4": "value4",
        ...
      }
    },
    ...
  },
  {
    "id": 2,
    ...
  },
  {
    "id": 4,
    ...
  },
  ...
]

My desired output would be:

{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3",
  "key4": "value4",
  ...
}

Upvotes: 0

Views: 71

Answers (1)

ikegami
ikegami

Reputation: 386541

jq '.[] | select( .id == 1 ) | .attributes.preferences'

Demo on jqplay

Upvotes: 1

Related Questions