input -> output
single or multiple (e.g. all elements of array, not to confuse with one array)
default: a sequence of whitespace-separated JSON values
--null-input / -n : no input
--raw-input / -R : each line passed as a string
--slurp / -s : entire input stream as parge array (one input)
--arg name value
$arg would be available
value as string
--argjson name JSON-text
value would be json
e.g. "$argjson foo 123"
--slurpfile variable-name filename
read file
parse as JSON values
bind as an array to variable
--rawfile variable-name filename
whole content as string
--args
bind remaining args as positional string args -> $ARGS.positional[]
--jsonargs:
same as -args, as JSON values
... more ...
Passes through filters, one at a time
Default take filers from command line (mind the escaping)
-f filename / --from-file filename: filters from file
Default: output a sequence of newline-separated JSON data
--compact-output / -c : no pretty print
--raw-output / -r : output string as string, not JSON string (newline after each output)
--raw-output0: NUL after each output
--join-output / -j: like raw, no newline
... more...
input (1..n) -> output(filtered 1..n) looping
feed same input to both / multiple filters
combine the results (into one result)
e.g.
you can implement an averaging filter as "add / length" - feeding the input array both to the add filter and the length filter and then performing the division.
e.g. collect multiple results into array
always output the literal
..a DOES NOT WORK, use .. | .a
e.g. [[{"a":1}]] => '.. | .a?' => 1
useful snippet: '..|.permalink? | select(.)'
useful snippet: 'path(..)'
like JSON syntax
keys
quoted string
quote-omitted "identifier-like"
variable reference
Variable references as keys use the value of the variable as the key.
(shortcut syntax [TRICKY!!]) Without a value then the variable's name becomes the key and its value becomes the value
"f o o" as $foo | "b a r" as $bar | {$foo, $bar:$foo} => {"foo":"f o o","b a r":"f o o"}
paranthesized expressions
e.g., {("a"+"b"):59}.
e.g. { (.user) : .titles } /* use user value instead of "user" as key */
values
{} expression's input -> any expression (filters) -> value
IF MULTIPLE VALUES, MULTIPLE RESULTS, each for 1 value
e.g.
inpuit {"user":"stedolan","titles":["JQ Primer", "More JQ"]}
=> filter {user, title: .titles[]} /* note .title[] produces multiple results */
=> TWO RESULTS {"user":"stedolan", "title": "JQ Primer"} {"user":"stedolan", "title": "More JQ"}
shortcut syntax - select subset of properties: {user: .user, title: .title} equals {user, title}
(variable reference syntax [TRICKY!!]) Without a value then the variable's name becomes the key and its value becomes the value
".." is interesting.... it outputs both path expressions and the values??
Command jq 'pick(.a, .b.c, .x)'
Input {"a": 1, "b": {"c": 2, "d": 3}, "e": 4}
Output {"a":1,"b":{"c":2},"x":null}
Command jq 'path(.a[0].b)'
Input null
Output ["a",0,"b"]
Command jq '[path(..)]'
Input {"a":[{"b":1}]}
Output [[],["a"],["a",0],["a",0,"b"]]
inpit => .. => (1) object itself (2) .a (3) .a[0] (4) .a[0].b i.e. 1
path(..) => (1)