jq snippets

0 minute read

Delete any object which contains a "keyname": "keyvalue" pair anywhere in the JSON:

cat x.json| jq 'del(..|select(objects and .keyname == "keyvalue"))'

If keyname contains a dash ("-"), use one of those notations:

cat x.json | jq 'del(..|select(objects and .["key-name"] == "keyvalue"))'
cat x.json | jq 'del(..|select(objects and ."key-name" == "keyvalue"))'

Delete all objects anywhere that contain a certain key:

cat x.json | jq 'del(..|select(objects and .key?)'

Delete a certain key from all objects everywhere:

cat x.json | jq 'del(..|.response?)'

Useful sources: