jq snippets
Posted on August 21, 2021 (Last modified on July 11, 2024) • 1 min read • 86 wordsDelete 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: