Cheatsheet Pages / jq Cheatsheet
How to pass shell variables into jq safely — --arg — and slurp many documents with -s
A version string from your script must land in the JSON without injection risk — and a stream of separate JSON documents needs to become one array, or one merged object.
Every filter was executed against the cheatsheet's sample config.json with jq 1.7 before publishing; the sample file sits at the bottom of the cheatsheet so you can reproduce every output.
jq --arg v "2.4.1" '.version = $v' config.json | jq '.version'
# output: "2.4.1"
printf '%s\n' '{"a":1}' '{"b":2}' | jq -s '.'
# output: [ { "a": 1 }, { "b": 2 } ]
printf '%s\n' '{"a":1}' '{"b":2}' | jq -s 'add'
# output: { "a": 1, "b": 2 }
Good to know
Never interpolate $VAR into the filter string; --arg passes it as data. --slurp (-s) reads a stream of JSON values into an array; add then merges them.
