A jq program is a "filter": it takes an input, and produces an output.
Filters can be combined in various ways - you can pipe the output of
one filter into another filter, or collect the output of a filter
into an array.
Some filters produce multiple results, for instance there's one that
produces all the elements of its input array.
Numbers in jq are internally represented by their IEEE754 double
precision approximation.
The simplest *useful* filter has the form `.foo`. When given a
JSON object (aka dictionary or hash) as input, `.foo` produces
the value at the key "foo" if the key is present, or null otherwise.
When the index value is an integer, `.[]` can index
arrays. Arrays are zero-based, so `.[2]` returns the third
element.
The `.[:]` syntax can be used to return a
subarray of an array or substring of a string.
A `#` character (not part of a string) starts a comment.
Inside a string, you can put an expression inside parens
after a backslash. Whatever the expression returns will be
interpolated into the string.
The | operator combines two filters by feeding the output(s) of
the one on the left into the input of the one on the right.
You can give a filter a name using "def" syntax:
Errors can be caught by using `try EXP catch EXP`.
The expression 'a == b' will produce 'true' if the results of evaluating
a and b are equal (that is, if they represent equivalent JSON values) and
'false' otherwise.
The operator `+` takes two filters, applies them both
to the same input, and adds the results together.
jq supports the normal Boolean operators `and`, `or`, `not`.
`if A then B else C end` will act the same as `B` if `A`
produces a value other than false or null, but act the same as
`C` otherwise.
jq has a syntax for named lexical labels to
break or go (back) to:
The `reduce` syntax allows you to combine all of the results of
an expression by accumulating them into a single answer.
The `recurse(f)` function allows you to search through a
recursive structure, and extract interesting data from all
levels.
jq lets you define variables using
`expression as $variable`.
Arguments are passed as _filters_ (functions with no
arguments), _not_ as values.
It is also possible to define functions in jq, although this
is a feature whose biggest use is defining jq's standard library
The destructuring alternative operator provides a concise mechanism
for destructuring an input that can take one of several forms.
`paths` outputs the paths to all the elements in its input
The builtin function `getpath` outputs the values in `.` found
at each path in `PATHS`.
Most users will want to use modification assignment operators,
such as `|=` or `+=`, rather than `=`.
Any filter may be used on the
left-hand side of an equals - whichever paths it selects from the
input will be where the assignment is performed.
For any filter `f`, `map(f)` and `map_values(f)` apply `f`
to each of the values in the input array or object, that is,
to the values of `.[]`.
The `sort` functions sorts its input, which must be an
array.
These functions convert between an object and an array of
key-value pairs.
The builtin function `has` returns whether the input object
has the given key, or the input array has an element
at the given index.
The `split` function splits an input string on the separator argument.
The `tojson` and `fromjson` builtins dump values as JSON texts
or parse JSON texts into values, respectively.
jq uses the
[Oniguruma regular expression library](https://github.com/kkos/oniguruma/blob/master/doc/RE),
jq provides some basic date handling functionality, with some
high-level and low-level builtins.
Two builtins functions
are provided for this, `input` and `inputs`, that read from
the same sources
The `debug` builtin can have as a side-effect the production of one or more messages on stderr.
With the `--stream` option jq can parse input texts in a streaming
fashion, allowing jq programs to start processing large JSON texts
immediately rather than after the parse completes.
The given `exit_code` (defaulting to `5`) will be jq's
exit status.
jq's output values are always
output as JSON texts on `stdout`.
jq supports the same set of datatypes as JSON - numbers,
strings, booleans, arrays, objects (which in JSON-speak are
hashes with only string keys), and "null".