How to filter a key using jq in bash

I have the following JSON:

  "a_key": "A",
  "key_to_filter": "B",
  "c_key": "C"

I want to get all the keys using jq as a compressed response (using jq -c "keys").

How can I get all the keys of this JSON without the key "key_to_filter"?

In other words, I need ["a_key", "c_key"]

>Solution :

You could just subtract that one from the array of keys:

jq -c 'keys - ["key_to_filter"]'


More verbosely but also more efficiently (if it matters), you could use a map to select what you want:

jq -c 'keys | map(select(. != "key_to_filter"))'


As @artild pointed out in a comment, you could also first delete the item in question, and just then get all keys:

jq -c 'del(.key_to_filter) | keys'




Note that keys returns a sorted array. If you want them unsorted, use keys_unsorted instead.

Leave a Reply