ca5p3r
ca5p3r

Reputation: 93

Iterate over JSON array using bash jq

I'd like to iterate over an array inside a JSON file by index using bash jq, and get for example only the name from each object inside that array:

{
  "items": [
    {
      "name": "item1",
      "year": "2021"
    },
    {
      "name": "item2",
      "year": "2020"
    }
  ]
}

Any ideas on how to achieve something like this?

item1name=`jq '.items'<???????> object.json | sed "s/\"//g"`

Upvotes: 0

Views: 1992

Answers (1)

Barbaros &#214;zhan
Barbaros &#214;zhan

Reputation: 65105

You can use

jq -r '.items[] | .name' object.json

where -r option is used to remove double quotes wrapping up the name values.

Demo

Upvotes: 1

Related Questions