Jq: Finding keys where values match

Created on 30 Dec 2012  路  1Comment  路  Source: stedolan/jq

I've got a JSON file that looks something like this:

{ "results" : { "groups" : {"alpha" : { "items": {
   "apple" : { "attributes": { "class": "fruit" } },
   "pear" : { "attributes" : { "class": "fruit" }},
   "dog" : {"attributes" : {"class": null }}
}},
"beta": {"items": {
  "banana" : { "attributes": { "class": "fruit" } }
}}}}}

I want to find keys of items where class is null. I cannot figure out how to do this using jq.

support

Most helpful comment

Something like

. as $object | keys[] | select($object[.].class == null)

should do the trick. The . as $object saves the input in $object, which you can refer to later to select specific keys.

>All comments

Something like

. as $object | keys[] | select($object[.].class == null)

should do the trick. The . as $object saves the input in $object, which you can refer to later to select specific keys.

Was this page helpful?
0 / 5 - 0 ratings