jq
The jq
manual is good but long and omits some nuances. The wiki has more in-depth information but is even bigger and less structured (the jq
Language Description might be interesting though). This document tries to be concise and exhaustive.
TODO:
awk
jq
is for JSON what awk
is for text, and fits well into a Unix-like environment.
Similarities:
stdin
, produces data on stdout
.-f
option.Differences:
awk
operates on newline-separated lines of text, jq
operates on whitespace-separated JSON values.awk
is mostly imperative, jq
is mostly functional.null
.true
, false
.1.23
."hello"
.[null, 1.23, true, "hello"]
.{ "a": 1.23, "b": true, "c": "hello" }
.null 1 true "hello"
.f
takes one(!) input stream and produces one(!) output stream.|
takes two filters f1
and f2
and produces a filter f1 | f2
. The input stream of the pipe filter is fed to f1
, the output stream of f1
is fed to the input stream of f2
, and the output stream of f2
is the output stream of the filter pipe.jq
executable takes a filter as its only argument, the input stream of which is stdin
and which output stream goes to stdout
(with values pretty printed and separated by a single newline).TODO: As seen above, a pipe is a filter and should be listed here instead of above.
One way to categorize filters is now many outputs they produce per input.
Passes the values in the input stream to the output stream unchanged.
.
. Does nothing else.f as $var
. Passes the input stream to f
and stores its output stream in the variable $var
.def func: f;
. Defines func
as an alias to filter f
(which is usually a pipe).Value literals. E.g. for every value in the input stream the filter 1
discards it and produces the number 1
.
The implicit filter in jq
is the .
identity filter
$ printf '1 "hello" true' | jq
1
"hello"
true
$ echo '1 2 3' | jq '.'
2
3
4
$ echo '1 2 3' | jq '. + 1 | . * .'
4
9
16
jq
has two basic components:
If it seems like a filter takes two input streams, it is really copying the one input to the two places it’s used.