2

I have an array of objects similar to the following:

[
  {
    "id": "one",
    "tags": {
      "my.key": "true"
    }
  },
  {
    "id": "two",
  }
]

How can I select all "id" values for each object containing a tag where "my.key" is "true"?

GaZ
  • 2,346
  • 23
  • 46

1 Answers1

3

You can use a select with .tags["my.key"] == "true" and get only the id field :

jq '.[] | select(.tags["my.key"] == "true") | .id' data.json
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
  • Thanks for the speedy response! I also got an alternative solution based on [another answer](http://stackoverflow.com/a/26701851/62667): `map(select(.tags["my.key"] == "true")) | .[] .id` – GaZ Dec 28 '16 at 21:48
  • which is pretty much the same thing. I had a typo in my "select" syntax which you helped me to find, thanks :) – GaZ Dec 28 '16 at 21:50