jq — processing JSON on the command line
cheat sheet · jq 1.7 https://lab.dimsanych.ru/en/wiki/jq/Cheat sheet
-
jq .pretty-print -
jq '.field'a field's value -
jq '.a.b'a nested field's value -
jq '.arr[]'every array element -
jq -r '.[].name'raw output (for scripts) -
jq '.[] | select(.x=="y")'a filter -
jq -r '.[] | [.id,.name] | @tsv'a TSV table -
jq --arg u "$USER" '.[]|select(.name==$u)'a shell variable into the filter
Options
-r strips the quotes from a string result — without it, a script gets the
JSON literal "value" instead of value. --arg/--argjson are the only
safe way to pass a shell variable into a filter: pasting a variable straight
into the filter text breaks on quotes and special characters. -s (slurp)
is needed when the input is a stream of several JSON objects that must be
processed as a single array.