Remove a key:value from an JSON object using jq

Using jq-1.6, this deletes the key .maxHeight from the input (it doesn't even complain if it didn't exist before):

jq 'del(.maxHeight)' new.json

There are two approaches:

  • the targeted approach, illustrated in an answer to your previous question at Manipulating a JSON file with jq

  • the global approach, that ignores the specific context.

The following illustrates the global approach:

walk(if type == "object" and has("maxHeight") then del(.maxHeight) else . end)

This in effect "edits" the input by updating whichever objects have the specified key.

If your jq does not have walk/1 simply include its def (available e.g. from https://raw.githubusercontent.com/stedolan/jq/master/src/builtin.jq) before invoking it.

Tags:

Json

Jq