mirror of
https://github.com/stedolan/jq.git
synced 2024-05-11 05:55:39 +00:00
Remove trailing whitespace from manual.yml
This commit is contained in:
@@ -9,7 +9,7 @@ body: |
|
|||||||
Filters can be combined in various ways - you can pipe the output of
|
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
|
one filter into another filter, or collect the output of a filter
|
||||||
into an array.
|
into an array.
|
||||||
|
|
||||||
Some filters produce multiple results, for instance there's one that
|
Some filters produce multiple results, for instance there's one that
|
||||||
produces all the elements of its input array. Piping that filter
|
produces all the elements of its input array. Piping that filter
|
||||||
into a second runs the second filter for each element of the
|
into a second runs the second filter for each element of the
|
||||||
@@ -43,7 +43,7 @@ manpage_intro: |
|
|||||||
By default, `jq` reads a stream of JSON objects (whitespace
|
By default, `jq` reads a stream of JSON objects (whitespace
|
||||||
separated) from `stdin`. One or more <files> may be specified, in
|
separated) from `stdin`. One or more <files> may be specified, in
|
||||||
which case `jq` will read input from those instead.
|
which case `jq` will read input from those instead.
|
||||||
|
|
||||||
The <options> are described in the [INVOKING JQ] section, they
|
The <options> are described in the [INVOKING JQ] section, they
|
||||||
mostly concern input and output formatting. The <filter> is written
|
mostly concern input and output formatting. The <filter> is written
|
||||||
in the jq language and specifies how to transform the input
|
in the jq language and specifies how to transform the input
|
||||||
@@ -61,11 +61,11 @@ manpage_epilogue: |
|
|||||||
## AUTHOR
|
## AUTHOR
|
||||||
|
|
||||||
Stephen Dolan `<mu@netsoc.tcd.ie>`
|
Stephen Dolan `<mu@netsoc.tcd.ie>`
|
||||||
|
|
||||||
sections:
|
sections:
|
||||||
- title: Invoking jq
|
- title: Invoking jq
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
jq filters run on a stream of JSON data. The input to jq is
|
jq filters run on a stream of JSON data. The input to jq is
|
||||||
parsed as a sequence of whitespace-separated JSON values which
|
parsed as a sequence of whitespace-separated JSON values which
|
||||||
are passed through the provided filter one at a time. The
|
are passed through the provided filter one at a time. The
|
||||||
@@ -91,44 +91,44 @@ sections:
|
|||||||
Output the jq version and exit with zero.
|
Output the jq version and exit with zero.
|
||||||
|
|
||||||
* `--slurp`/`-s`:
|
* `--slurp`/`-s`:
|
||||||
|
|
||||||
Instead of running the filter for each JSON object in the
|
Instead of running the filter for each JSON object in the
|
||||||
input, read the entire input stream into a large array and run
|
input, read the entire input stream into a large array and run
|
||||||
the filter just once.
|
the filter just once.
|
||||||
|
|
||||||
* `--online-input`/`-I`:
|
* `--online-input`/`-I`:
|
||||||
|
|
||||||
When the top-level input value is an array produce its elements
|
When the top-level input value is an array produce its elements
|
||||||
instead of the array. This allows on-line processing of
|
instead of the array. This allows on-line processing of
|
||||||
potentially very large top-level arrays' elements.
|
potentially very large top-level arrays' elements.
|
||||||
|
|
||||||
* `--raw-input`/`-R`:
|
* `--raw-input`/`-R`:
|
||||||
|
|
||||||
Don't parse the input as JSON. Instead, each line of text is
|
Don't parse the input as JSON. Instead, each line of text is
|
||||||
passed to the filter as a string. If combined with `--slurp`,
|
passed to the filter as a string. If combined with `--slurp`,
|
||||||
then the entire input is passed to the filter as a single long
|
then the entire input is passed to the filter as a single long
|
||||||
string.
|
string.
|
||||||
|
|
||||||
* `--null-input`/`-n`:
|
* `--null-input`/`-n`:
|
||||||
|
|
||||||
Don't read any input at all! Instead, the filter is run once
|
Don't read any input at all! Instead, the filter is run once
|
||||||
using `null` as the input. This is useful when using jq as a
|
using `null` as the input. This is useful when using jq as a
|
||||||
simple calculator or to construct JSON data from scratch.
|
simple calculator or to construct JSON data from scratch.
|
||||||
|
|
||||||
* `--compact-output` / `-c`:
|
* `--compact-output` / `-c`:
|
||||||
|
|
||||||
By default, jq pretty-prints JSON output. Using this option
|
By default, jq pretty-prints JSON output. Using this option
|
||||||
will result in more compact output by instead putting each
|
will result in more compact output by instead putting each
|
||||||
JSON object on a single line.
|
JSON object on a single line.
|
||||||
|
|
||||||
* `--colour-output` / `-C` and `--monochrome-output` / `-M`:
|
* `--colour-output` / `-C` and `--monochrome-output` / `-M`:
|
||||||
|
|
||||||
By default, jq outputs colored JSON if writing to a
|
By default, jq outputs colored JSON if writing to a
|
||||||
terminal. You can force it to produce color even if writing to
|
terminal. You can force it to produce color even if writing to
|
||||||
a pipe or a file using `-C`, and disable color with `-M`.
|
a pipe or a file using `-C`, and disable color with `-M`.
|
||||||
|
|
||||||
* `--ascii-output` / `-a`:
|
* `--ascii-output` / `-a`:
|
||||||
|
|
||||||
jq usually outputs non-ASCII Unicode codepoints as UTF-8, even
|
jq usually outputs non-ASCII Unicode codepoints as UTF-8, even
|
||||||
if the input specified them as escape sequences (like
|
if the input specified them as escape sequences (like
|
||||||
"\u03bc"). Using this option, you can force jq to produce pure
|
"\u03bc"). Using this option, you can force jq to produce pure
|
||||||
@@ -142,11 +142,11 @@ sections:
|
|||||||
output elsewhere).
|
output elsewhere).
|
||||||
|
|
||||||
* `--sort-keys` / `-S`:
|
* `--sort-keys` / `-S`:
|
||||||
|
|
||||||
Output the fields of each object with the keys in sorted order.
|
Output the fields of each object with the keys in sorted order.
|
||||||
|
|
||||||
* `--raw-output` / `-r`:
|
* `--raw-output` / `-r`:
|
||||||
|
|
||||||
With this option, if the filter's result is a string then it
|
With this option, if the filter's result is a string then it
|
||||||
will be written directly to standard output rather than being
|
will be written directly to standard output rather than being
|
||||||
formatted as a JSON string with quotes. This can be useful for
|
formatted as a JSON string with quotes. This can be useful for
|
||||||
@@ -184,7 +184,7 @@ sections:
|
|||||||
entries:
|
entries:
|
||||||
- title: "`.`"
|
- title: "`.`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
The absolute simplest (and least interesting) filter
|
The absolute simplest (and least interesting) filter
|
||||||
is `.`. This is a filter that takes its input and
|
is `.`. This is a filter that takes its input and
|
||||||
produces it unchanged as output.
|
produces it unchanged as output.
|
||||||
@@ -200,7 +200,7 @@ sections:
|
|||||||
|
|
||||||
- title: "`.foo`, `.foo.bar`"
|
- title: "`.foo`, `.foo.bar`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
The simplest *useful* filter is `.foo`. When given a
|
The simplest *useful* filter is `.foo`. When given a
|
||||||
JSON object (aka dictionary or hash) as input, it produces
|
JSON object (aka dictionary or hash) as input, it produces
|
||||||
the value at the key "foo", or null if there's none present.
|
the value at the key "foo", or null if there's none present.
|
||||||
@@ -209,7 +209,7 @@ sections:
|
|||||||
it with double quotes like this: `."foo$"`.
|
it with double quotes like this: `."foo$"`.
|
||||||
|
|
||||||
A filter of the form `.foo.bar` is equivalent to `.foo|.bar`.
|
A filter of the form `.foo.bar` is equivalent to `.foo|.bar`.
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
- program: '.foo'
|
- program: '.foo'
|
||||||
input: '{"foo": 42, "bar": "less interesting data"}'
|
input: '{"foo": 42, "bar": "less interesting data"}'
|
||||||
@@ -223,7 +223,7 @@ sections:
|
|||||||
|
|
||||||
- title: "`.foo?`"
|
- title: "`.foo?`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
Just like `.foo`, but does not output even an error when `.`
|
Just like `.foo`, but does not output even an error when `.`
|
||||||
is not an array or an object.
|
is not an array or an object.
|
||||||
|
|
||||||
@@ -243,7 +243,7 @@ sections:
|
|||||||
|
|
||||||
- title: "`.[<string>]`, `.[2]`, `.[10:15]`"
|
- title: "`.[<string>]`, `.[2]`, `.[10:15]`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
You can also look up fields of an object using syntax like
|
You can also look up fields of an object using syntax like
|
||||||
`.["foo"]` (.foo above is a shorthand version of this). This
|
`.["foo"]` (.foo above is a shorthand version of this). This
|
||||||
one works for arrays as well, if the key is an
|
one works for arrays as well, if the key is an
|
||||||
@@ -278,7 +278,7 @@ sections:
|
|||||||
- program: '.[2:4]'
|
- program: '.[2:4]'
|
||||||
input: '"abcdefghi"'
|
input: '"abcdefghi"'
|
||||||
output: ['"cd"']
|
output: ['"cd"']
|
||||||
|
|
||||||
- program: '.[:3]'
|
- program: '.[:3]'
|
||||||
input: '["a","b","c","d","e"]'
|
input: '["a","b","c","d","e"]'
|
||||||
output: ['["a", "b", "c"]']
|
output: ['["a", "b", "c"]']
|
||||||
@@ -289,7 +289,7 @@ sections:
|
|||||||
|
|
||||||
- title: "`.[]`"
|
- title: "`.[]`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
If you use the `.[index]` syntax, but omit the index
|
If you use the `.[index]` syntax, but omit the index
|
||||||
entirely, it will return *all* of the elements of an
|
entirely, it will return *all* of the elements of an
|
||||||
array. Running `.[]` with the input `[1,2,3]` will produce the
|
array. Running `.[]` with the input `[1,2,3]` will produce the
|
||||||
@@ -316,13 +316,13 @@ sections:
|
|||||||
|
|
||||||
- title: "`.[]?`"
|
- title: "`.[]?`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
Like `.[]`, but no errors will be output if . is not an array
|
Like `.[]`, but no errors will be output if . is not an array
|
||||||
or object.
|
or object.
|
||||||
|
|
||||||
- title: "`,`"
|
- title: "`,`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
If two filters are separated by a comma, then the
|
If two filters are separated by a comma, then the
|
||||||
input will be fed into both and there will be multiple
|
input will be fed into both and there will be multiple
|
||||||
outputs: first, all of the outputs produced by the left
|
outputs: first, all of the outputs produced by the left
|
||||||
@@ -337,18 +337,18 @@ sections:
|
|||||||
|
|
||||||
- program: ".user, .projects[]"
|
- program: ".user, .projects[]"
|
||||||
input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'
|
input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'
|
||||||
output: ['"stedolan"', '"jq"', '"wikiflow"']
|
output: ['"stedolan"', '"jq"', '"wikiflow"']
|
||||||
|
|
||||||
- program: '.[4,2]'
|
- program: '.[4,2]'
|
||||||
input: '["a","b","c","d","e"]'
|
input: '["a","b","c","d","e"]'
|
||||||
output: ['"e"', '"c"']
|
output: ['"e"', '"c"']
|
||||||
|
|
||||||
- title: "`|`"
|
- title: "`|`"
|
||||||
body: |
|
body: |
|
||||||
The | operator combines two filters by feeding the output(s) of
|
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. It's
|
the one on the left into the input of the one on the right. It's
|
||||||
pretty much the same as the Unix shell's pipe, if you're used to
|
pretty much the same as the Unix shell's pipe, if you're used to
|
||||||
that.
|
that.
|
||||||
|
|
||||||
If the one on the left produces multiple results, the one on
|
If the one on the left produces multiple results, the one on
|
||||||
the right will be run for each of those results. So, the
|
the right will be run for each of those results. So, the
|
||||||
@@ -362,7 +362,7 @@ sections:
|
|||||||
|
|
||||||
- title: Types and Values
|
- title: Types and Values
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
jq supports the same set of datatypes as JSON - numbers,
|
jq supports the same set of datatypes as JSON - numbers,
|
||||||
strings, booleans, arrays, objects (which in JSON-speak are
|
strings, booleans, arrays, objects (which in JSON-speak are
|
||||||
hashes with only string keys), and "null".
|
hashes with only string keys), and "null".
|
||||||
@@ -376,7 +376,7 @@ sections:
|
|||||||
entries:
|
entries:
|
||||||
- title: Array construction - `[]`
|
- title: Array construction - `[]`
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
As in JSON, `[]` is used to construct arrays, as in
|
As in JSON, `[]` is used to construct arrays, as in
|
||||||
`[1,2,3]`. The elements of the arrays can be any jq
|
`[1,2,3]`. The elements of the arrays can be any jq
|
||||||
expression. All of the results produced by all of the
|
expression. All of the results produced by all of the
|
||||||
@@ -384,7 +384,7 @@ sections:
|
|||||||
to construct an array out of a known quantity of values (as
|
to construct an array out of a known quantity of values (as
|
||||||
in `[.foo, .bar, .baz]`) or to "collect" all the results of a
|
in `[.foo, .bar, .baz]`) or to "collect" all the results of a
|
||||||
filter into an array (as in `[.items[].name]`)
|
filter into an array (as in `[.items[].name]`)
|
||||||
|
|
||||||
Once you understand the "," operator, you can look at jq's array
|
Once you understand the "," operator, you can look at jq's array
|
||||||
syntax in a different light: the expression `[1,2,3]` is not using a
|
syntax in a different light: the expression `[1,2,3]` is not using a
|
||||||
built-in syntax for comma-separated arrays, but is instead applying
|
built-in syntax for comma-separated arrays, but is instead applying
|
||||||
@@ -404,49 +404,49 @@ sections:
|
|||||||
|
|
||||||
Like JSON, `{}` is for constructing objects (aka
|
Like JSON, `{}` is for constructing objects (aka
|
||||||
dictionaries or hashes), as in: `{"a": 42, "b": 17}`.
|
dictionaries or hashes), as in: `{"a": 42, "b": 17}`.
|
||||||
|
|
||||||
If the keys are "sensible" (all alphabetic characters), then
|
If the keys are "sensible" (all alphabetic characters), then
|
||||||
the quotes can be left off. The value can be any expression
|
the quotes can be left off. The value can be any expression
|
||||||
(although you may need to wrap it in parentheses if it's a
|
(although you may need to wrap it in parentheses if it's a
|
||||||
complicated one), which gets applied to the {} expression's
|
complicated one), which gets applied to the {} expression's
|
||||||
input (remember, all filters have an input and an
|
input (remember, all filters have an input and an
|
||||||
output).
|
output).
|
||||||
|
|
||||||
{foo: .bar}
|
{foo: .bar}
|
||||||
|
|
||||||
will produce the JSON object `{"foo": 42}` if given the JSON
|
will produce the JSON object `{"foo": 42}` if given the JSON
|
||||||
object `{"bar":42, "baz":43}`. You can use this to select
|
object `{"bar":42, "baz":43}`. You can use this to select
|
||||||
particular fields of an object: if the input is an object
|
particular fields of an object: if the input is an object
|
||||||
with "user", "title", "id", and "content" fields and you
|
with "user", "title", "id", and "content" fields and you
|
||||||
just want "user" and "title", you can write
|
just want "user" and "title", you can write
|
||||||
|
|
||||||
{user: .user, title: .title}
|
{user: .user, title: .title}
|
||||||
|
|
||||||
Because that's so common, there's a shortcut syntax: `{user, title}`.
|
Because that's so common, there's a shortcut syntax: `{user, title}`.
|
||||||
|
|
||||||
If one of the expressions produces multiple results,
|
If one of the expressions produces multiple results,
|
||||||
multiple dictionaries will be produced. If the input's
|
multiple dictionaries will be produced. If the input's
|
||||||
|
|
||||||
{"user":"stedolan","titles":["JQ Primer", "More JQ"]}
|
{"user":"stedolan","titles":["JQ Primer", "More JQ"]}
|
||||||
|
|
||||||
then the expression
|
then the expression
|
||||||
|
|
||||||
{user, title: .titles[]}
|
{user, title: .titles[]}
|
||||||
|
|
||||||
will produce two outputs:
|
will produce two outputs:
|
||||||
|
|
||||||
{"user":"stedolan", "title": "JQ Primer"}
|
{"user":"stedolan", "title": "JQ Primer"}
|
||||||
{"user":"stedolan", "title": "More JQ"}
|
{"user":"stedolan", "title": "More JQ"}
|
||||||
|
|
||||||
Putting parentheses around the key means it will be evaluated as an
|
Putting parentheses around the key means it will be evaluated as an
|
||||||
expression. With the same input as above,
|
expression. With the same input as above,
|
||||||
|
|
||||||
{(.user): .titles}
|
{(.user): .titles}
|
||||||
|
|
||||||
produces
|
produces
|
||||||
|
|
||||||
{"stedolan": ["JQ Primer", "More JQ"]}
|
{"stedolan": ["JQ Primer", "More JQ"]}
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
- program: '{user, title: .titles[]}'
|
- program: '{user, title: .titles[]}'
|
||||||
input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
|
input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
|
||||||
@@ -465,7 +465,7 @@ sections:
|
|||||||
etc.). However, jq never does implicit type conversions. If you
|
etc.). However, jq never does implicit type conversions. If you
|
||||||
try to add a string to an object you'll get an error message and
|
try to add a string to an object you'll get an error message and
|
||||||
no result.
|
no result.
|
||||||
|
|
||||||
entries:
|
entries:
|
||||||
- title: Addition - `+`
|
- title: Addition - `+`
|
||||||
body: |
|
body: |
|
||||||
@@ -479,13 +479,13 @@ sections:
|
|||||||
- **Arrays** are added by being concatenated into a larger array.
|
- **Arrays** are added by being concatenated into a larger array.
|
||||||
|
|
||||||
- **Strings** are added by being joined into a larger string.
|
- **Strings** are added by being joined into a larger string.
|
||||||
|
|
||||||
- **Objects** are added by merging, that is, inserting all
|
- **Objects** are added by merging, that is, inserting all
|
||||||
the key-value pairs from both objects into a single
|
the key-value pairs from both objects into a single
|
||||||
combined object. If both objects contain a value for the
|
combined object. If both objects contain a value for the
|
||||||
same key, the object on the right of the `+` wins. (For
|
same key, the object on the right of the `+` wins. (For
|
||||||
recursive merge use the `*` operator.)
|
recursive merge use the `*` operator.)
|
||||||
|
|
||||||
`null` can be added to any value, and returns the other
|
`null` can be added to any value, and returns the other
|
||||||
value unchanged.
|
value unchanged.
|
||||||
|
|
||||||
@@ -571,7 +571,7 @@ sections:
|
|||||||
|
|
||||||
- title: "`keys`"
|
- title: "`keys`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
The builtin function `keys`, when given an object, returns
|
The builtin function `keys`, when given an object, returns
|
||||||
its keys in an array.
|
its keys in an array.
|
||||||
|
|
||||||
@@ -627,11 +627,11 @@ sections:
|
|||||||
|
|
||||||
- title: "`to_entries`, `from_entries`, `with_entries`"
|
- title: "`to_entries`, `from_entries`, `with_entries`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
These functions convert between an object and an array of
|
These functions convert between an object and an array of
|
||||||
key-value pairs. If `to_entries` is passed an object, then
|
key-value pairs. If `to_entries` is passed an object, then
|
||||||
for each `k: v` entry in the input, the output array
|
for each `k: v` entry in the input, the output array
|
||||||
includes `{"key": k, "value": v}`.
|
includes `{"key": k, "value": v}`.
|
||||||
|
|
||||||
`from_entries` does the opposite conversion, and
|
`from_entries` does the opposite conversion, and
|
||||||
`with_entries(foo)` is a shorthand for `to_entries |
|
`with_entries(foo)` is a shorthand for `to_entries |
|
||||||
@@ -668,7 +668,7 @@ sections:
|
|||||||
|
|
||||||
- title: "`arrays`, `objects`, `iterables`, `booleans`, `numbers`, `strings`, `nulls`, `values`, `scalars`"
|
- title: "`arrays`, `objects`, `iterables`, `booleans`, `numbers`, `strings`, `nulls`, `values`, `scalars`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
These built-ins select only inputs that are arrays, objects,
|
These built-ins select only inputs that are arrays, objects,
|
||||||
iterables (arrays or objects), booleans, numbers, strings,
|
iterables (arrays or objects), booleans, numbers, strings,
|
||||||
null, non-null values, and non-iterables, respectively.
|
null, non-null values, and non-iterables, respectively.
|
||||||
@@ -680,7 +680,7 @@ sections:
|
|||||||
|
|
||||||
- title: "`empty`"
|
- title: "`empty`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
`empty` returns no results. None at all. Not even `null`.
|
`empty` returns no results. None at all. Not even `null`.
|
||||||
|
|
||||||
It's useful on occasion. You'll know if you need it :)
|
It's useful on occasion. You'll know if you need it :)
|
||||||
@@ -714,14 +714,14 @@ sections:
|
|||||||
`paths` outputs the paths to all the elements in its input
|
`paths` outputs the paths to all the elements in its input
|
||||||
(except it does not output the empty list, representing .
|
(except it does not output the empty list, representing .
|
||||||
itself).
|
itself).
|
||||||
|
|
||||||
`paths(f)` outputs the paths to any values for which `f` is true.
|
`paths(f)` outputs the paths to any values for which `f` is true.
|
||||||
That is, `paths(numbers)` outputs the paths to all numeric
|
That is, `paths(numbers)` outputs the paths to all numeric
|
||||||
values.
|
values.
|
||||||
|
|
||||||
`leaf_paths` is an alias of `paths(scalars)`; `leaf_paths` is
|
`leaf_paths` is an alias of `paths(scalars)`; `leaf_paths` is
|
||||||
*deprecated* and will be removed in the next major release.
|
*deprecated* and will be removed in the next major release.
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
- program: '[paths]'
|
- program: '[paths]'
|
||||||
input: '[1,[[],{"a":2}]]'
|
input: '[1,[[],{"a":2}]]'
|
||||||
@@ -754,13 +754,13 @@ sections:
|
|||||||
|
|
||||||
- title: "`any`"
|
- title: "`any`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
The filter `any` takes as input an array of boolean values,
|
The filter `any` takes as input an array of boolean values,
|
||||||
and produces `true` as output if any of the the elements of
|
and produces `true` as output if any of the the elements of
|
||||||
the array is `true`.
|
the array is `true`.
|
||||||
|
|
||||||
If the input is an empty array, `any` returns `false`.
|
If the input is an empty array, `any` returns `false`.
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
- program: any
|
- program: any
|
||||||
input: '[true, false]'
|
input: '[true, false]'
|
||||||
@@ -774,13 +774,13 @@ sections:
|
|||||||
|
|
||||||
- title: "`all`"
|
- title: "`all`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
The filter `all` takes as input an array of boolean values,
|
The filter `all` takes as input an array of boolean values,
|
||||||
and produces `true` as output if all of the the elements of
|
and produces `true` as output if all of the the elements of
|
||||||
the array are `true`.
|
the array are `true`.
|
||||||
|
|
||||||
If the input is an empty array, `all` returns `true`.
|
If the input is an empty array, `all` returns `true`.
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
- program: all
|
- program: all
|
||||||
input: '[true, false]'
|
input: '[true, false]'
|
||||||
@@ -819,12 +819,12 @@ sections:
|
|||||||
|
|
||||||
- title: "`range(upto), `range(from;upto)`"
|
- title: "`range(upto), `range(from;upto)`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
The `range` function produces a range of numbers. `range(4;10)`
|
The `range` function produces a range of numbers. `range(4;10)`
|
||||||
produces 6 numbers, from 4 (inclusive) to 10 (exclusive). The numbers
|
produces 6 numbers, from 4 (inclusive) to 10 (exclusive). The numbers
|
||||||
are produced as separate outputs. Use `[range(4;10)]` to get a range as
|
are produced as separate outputs. Use `[range(4;10)]` to get a range as
|
||||||
an array.
|
an array.
|
||||||
|
|
||||||
Its first argument can be omitted; it defaults to zero.
|
Its first argument can be omitted; it defaults to zero.
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
@@ -837,7 +837,7 @@ sections:
|
|||||||
- program: '[range(4)]'
|
- program: '[range(4)]'
|
||||||
input: 'null'
|
input: 'null'
|
||||||
output: ['[0,1,2,3]']
|
output: ['[0,1,2,3]']
|
||||||
|
|
||||||
- title: "`floor`"
|
- title: "`floor`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
@@ -847,7 +847,7 @@ sections:
|
|||||||
- program: 'floor'
|
- program: 'floor'
|
||||||
input: '3.14159'
|
input: '3.14159'
|
||||||
output: ['3']
|
output: ['3']
|
||||||
|
|
||||||
- title: "`sqrt`"
|
- title: "`sqrt`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
@@ -857,7 +857,7 @@ sections:
|
|||||||
- program: 'sqrt'
|
- program: 'sqrt'
|
||||||
input: '9'
|
input: '9'
|
||||||
output: ['3']
|
output: ['3']
|
||||||
|
|
||||||
- title: "`tonumber`"
|
- title: "`tonumber`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
@@ -884,7 +884,7 @@ sections:
|
|||||||
|
|
||||||
- title: "`type`"
|
- title: "`type`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
The `type` function returns the type of its argument as a
|
The `type` function returns the type of its argument as a
|
||||||
string, which is one of null, boolean, number, string, array
|
string, which is one of null, boolean, number, string, array
|
||||||
or object.
|
or object.
|
||||||
@@ -896,7 +896,7 @@ sections:
|
|||||||
|
|
||||||
- title: "`sort, sort(path_expression), sort_by(path_expression)`"
|
- title: "`sort, sort(path_expression), sort_by(path_expression)`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
The `sort` functions sorts its input, which must be an
|
The `sort` functions sorts its input, which must be an
|
||||||
array. Values are sorted in the following order:
|
array. Values are sorted in the following order:
|
||||||
|
|
||||||
@@ -918,7 +918,7 @@ sections:
|
|||||||
|
|
||||||
`sort(foo)` compares two elements by comparing the result of
|
`sort(foo)` compares two elements by comparing the result of
|
||||||
`foo` on each element.
|
`foo` on each element.
|
||||||
|
|
||||||
`sort_by(foo)` is an alias of `sort(foo)`; `sort_by()` is
|
`sort_by(foo)` is an alias of `sort(foo)`; `sort_by()` is
|
||||||
*deprecated* and will be removed in the next major release.
|
*deprecated* and will be removed in the next major release.
|
||||||
|
|
||||||
@@ -932,7 +932,7 @@ sections:
|
|||||||
|
|
||||||
- title: "`group(path_expression)`, `group_by(path_expression)`"
|
- title: "`group(path_expression)`, `group_by(path_expression)`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
`group(.foo)` takes as input an array, groups the
|
`group(.foo)` takes as input an array, groups the
|
||||||
elements having the same `.foo` field into separate arrays,
|
elements having the same `.foo` field into separate arrays,
|
||||||
and produces all of these arrays as elements of a larger
|
and produces all of these arrays as elements of a larger
|
||||||
@@ -941,10 +941,10 @@ sections:
|
|||||||
Any jq expression, not just a field access, may be used in
|
Any jq expression, not just a field access, may be used in
|
||||||
place of `.foo`. The sorting order is the same as described
|
place of `.foo`. The sorting order is the same as described
|
||||||
in the `sort` function above.
|
in the `sort` function above.
|
||||||
|
|
||||||
`group_by(foo)` is an alias of `group(foo)`; `group_by()` is
|
`group_by(foo)` is an alias of `group(foo)`; `group_by()` is
|
||||||
*deprecated* and will be removed in the next major release.
|
*deprecated* and will be removed in the next major release.
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
- program: 'group(.foo)'
|
- program: 'group(.foo)'
|
||||||
input: '[{"foo":1, "bar":10}, {"foo":3, "bar":100}, {"foo":1, "bar":1}]'
|
input: '[{"foo":1, "bar":10}, {"foo":3, "bar":100}, {"foo":1, "bar":1}]'
|
||||||
@@ -952,13 +952,13 @@ sections:
|
|||||||
|
|
||||||
- title: "`min`, `max`, `min(path_exp)`, `max(path_exp)`, `min_by(path_exp)`, `max_by(path_exp)`"
|
- title: "`min`, `max`, `min(path_exp)`, `max(path_exp)`, `min_by(path_exp)`, `max_by(path_exp)`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
Find the minimum or maximum element of the input array.
|
Find the minimum or maximum element of the input array.
|
||||||
This filter accepts an optional argument that
|
This filter accepts an optional argument that
|
||||||
allows you to specify a particular field or
|
allows you to specify a particular field or
|
||||||
property to examine, e.g. `min(.foo)` finds the object
|
property to examine, e.g. `min(.foo)` finds the object
|
||||||
with the smallest `foo` field.
|
with the smallest `foo` field.
|
||||||
|
|
||||||
For legacy reasons, `min_by(.foo)` and `max_by(.foo)` exist as
|
For legacy reasons, `min_by(.foo)` and `max_by(.foo)` exist as
|
||||||
aliases for `min(.foo)` and `max(.foo)`. These aliases are
|
aliases for `min(.foo)` and `max(.foo)`. These aliases are
|
||||||
considered *deprecated* and will be removed in the next major
|
considered *deprecated* and will be removed in the next major
|
||||||
@@ -974,14 +974,14 @@ sections:
|
|||||||
|
|
||||||
- title: "`unique`, `unique(path_exp)`, `unique_by(path_exp)`"
|
- title: "`unique`, `unique(path_exp)`, `unique_by(path_exp)`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
The `unique` function takes as input an array and produces
|
The `unique` function takes as input an array and produces
|
||||||
an array of the same elements, in sorted order, with
|
an array of the same elements, in sorted order, with
|
||||||
duplicates removed. If an optional argument is passed, it
|
duplicates removed. If an optional argument is passed, it
|
||||||
will keep only one element for each value obtained by applying
|
will keep only one element for each value obtained by applying
|
||||||
the argument. Think of it as making an array by taking one
|
the argument. Think of it as making an array by taking one
|
||||||
element out of every group produced by `group`.
|
element out of every group produced by `group`.
|
||||||
|
|
||||||
For legacy reasons, `unique_by(.foo)` exists as an alias for
|
For legacy reasons, `unique_by(.foo)` exists as an alias for
|
||||||
`unique(.foo)`. This alias is considered *deprecated* and will
|
`unique(.foo)`. This alias is considered *deprecated* and will
|
||||||
be removed in the next major release.
|
be removed in the next major release.
|
||||||
@@ -999,7 +999,7 @@ sections:
|
|||||||
|
|
||||||
- title: "`reverse`"
|
- title: "`reverse`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
This function reverses an array.
|
This function reverses an array.
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
@@ -1158,7 +1158,7 @@ sections:
|
|||||||
|
|
||||||
- title: "`recurse(f)`, `recurse`, `recurse_down`"
|
- title: "`recurse(f)`, `recurse`, `recurse_down`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
The `recurse(f)` function allows you to search through a
|
The `recurse(f)` function allows you to search through a
|
||||||
recursive structure, and extract interesting data from all
|
recursive structure, and extract interesting data from all
|
||||||
levels. Suppose your input represents a filesystem:
|
levels. Suppose your input represents a filesystem:
|
||||||
@@ -1170,17 +1170,17 @@ sections:
|
|||||||
{"name": "/home", "children": [
|
{"name": "/home", "children": [
|
||||||
{"name": "/home/stephen", "children": [
|
{"name": "/home/stephen", "children": [
|
||||||
{"name": "/home/stephen/jq", "children": []}]}]}]}
|
{"name": "/home/stephen/jq", "children": []}]}]}]}
|
||||||
|
|
||||||
Now suppose you want to extract all of the filenames
|
Now suppose you want to extract all of the filenames
|
||||||
present. You need to retrieve `.name`, `.children[].name`,
|
present. You need to retrieve `.name`, `.children[].name`,
|
||||||
`.children[].children[].name`, and so on. You can do this
|
`.children[].children[].name`, and so on. You can do this
|
||||||
with:
|
with:
|
||||||
|
|
||||||
recurse(.children[]) | .name
|
recurse(.children[]) | .name
|
||||||
|
|
||||||
When called without an argument, `recurse` is equivalent to
|
When called without an argument, `recurse` is equivalent to
|
||||||
`recurse(.[]?)`.
|
`recurse(.[]?)`.
|
||||||
|
|
||||||
For legacy reasons, `recurse_down` exists as an alias to
|
For legacy reasons, `recurse_down` exists as an alias to
|
||||||
calling `recurse` without arguments. This alias is considered
|
calling `recurse` without arguments. This alias is considered
|
||||||
*deprecated* and will be removed in the next major release.
|
*deprecated* and will be removed in the next major release.
|
||||||
@@ -1188,21 +1188,21 @@ sections:
|
|||||||
examples:
|
examples:
|
||||||
- program: 'recurse(.foo[])'
|
- program: 'recurse(.foo[])'
|
||||||
input: '{"foo":[{"foo": []}, {"foo":[{"foo":[]}]}]}'
|
input: '{"foo":[{"foo": []}, {"foo":[{"foo":[]}]}]}'
|
||||||
output:
|
output:
|
||||||
- '{"foo":[{"foo":[]},{"foo":[{"foo":[]}]}]}'
|
- '{"foo":[{"foo":[]},{"foo":[{"foo":[]}]}]}'
|
||||||
- '{"foo":[]}'
|
- '{"foo":[]}'
|
||||||
- '{"foo":[{"foo":[]}]}'
|
- '{"foo":[{"foo":[]}]}'
|
||||||
- '{"foo":[]}'
|
- '{"foo":[]}'
|
||||||
- program: 'recurse'
|
- program: 'recurse'
|
||||||
input: '{"a":0,"b":[1]}'
|
input: '{"a":0,"b":[1]}'
|
||||||
output:
|
output:
|
||||||
- '0'
|
- '0'
|
||||||
- '[1]'
|
- '[1]'
|
||||||
- '1'
|
- '1'
|
||||||
|
|
||||||
- title: "`..`"
|
- title: "`..`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
Short-hand for `recurse` without arguments. This is intended
|
Short-hand for `recurse` without arguments. This is intended
|
||||||
to resemble the XPath `//` operator. Note that `..a` does not
|
to resemble the XPath `//` operator. Note that `..a` does not
|
||||||
work; use `..|a` instead. In the example below we use
|
work; use `..|a` instead. In the example below we use
|
||||||
@@ -1216,7 +1216,7 @@ sections:
|
|||||||
|
|
||||||
- title: "`env`"
|
- title: "`env`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
Outputs an object representing jq's environment.
|
Outputs an object representing jq's environment.
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
@@ -1235,7 +1235,7 @@ sections:
|
|||||||
- program: '"The input was \(.), which is one less than \(.+1)"'
|
- program: '"The input was \(.), which is one less than \(.+1)"'
|
||||||
input: '42'
|
input: '42'
|
||||||
output: ['"The input was 42, which is one less than 43"']
|
output: ['"The input was 42, which is one less than 43"']
|
||||||
|
|
||||||
- title: "Convert to/from JSON"
|
- title: "Convert to/from JSON"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
@@ -1283,17 +1283,17 @@ sections:
|
|||||||
characters to a `%xx` sequence.
|
characters to a `%xx` sequence.
|
||||||
|
|
||||||
* `@csv`:
|
* `@csv`:
|
||||||
|
|
||||||
The input must be an array, and it is rendered as CSV
|
The input must be an array, and it is rendered as CSV
|
||||||
with double quotes for strings, and quotes escaped by
|
with double quotes for strings, and quotes escaped by
|
||||||
repetition.
|
repetition.
|
||||||
|
|
||||||
* `@sh`:
|
* `@sh`:
|
||||||
|
|
||||||
The input is escaped suitable for use in a command-line
|
The input is escaped suitable for use in a command-line
|
||||||
for a POSIX shell. If the input is an array, the output
|
for a POSIX shell. If the input is an array, the output
|
||||||
will be a series of space-separated strings.
|
will be a series of space-separated strings.
|
||||||
|
|
||||||
* `@base64`:
|
* `@base64`:
|
||||||
|
|
||||||
The input is converted to base64 as specified by RFC 4648.
|
The input is converted to base64 as specified by RFC 4648.
|
||||||
@@ -1326,7 +1326,7 @@ sections:
|
|||||||
- program: '@sh "echo \(.)"'
|
- program: '@sh "echo \(.)"'
|
||||||
input: "\"O'Hara's Ale\""
|
input: "\"O'Hara's Ale\""
|
||||||
output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]
|
output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]
|
||||||
|
|
||||||
- title: Conditionals and Comparisons
|
- title: Conditionals and Comparisons
|
||||||
entries:
|
entries:
|
||||||
- title: "`==`, `!=`"
|
- title: "`==`, `!=`"
|
||||||
@@ -1338,7 +1338,7 @@ sections:
|
|||||||
to numbers. If you're coming from Javascript, jq's == is like
|
to numbers. If you're coming from Javascript, jq's == is like
|
||||||
Javascript's === - considering values equal only when they have the
|
Javascript's === - considering values equal only when they have the
|
||||||
same type as well as the same value.
|
same type as well as the same value.
|
||||||
|
|
||||||
!= is "not equal", and 'a != b' returns the opposite value of 'a == b'
|
!= is "not equal", and 'a != b' returns the opposite value of 'a == b'
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
@@ -1351,7 +1351,7 @@ sections:
|
|||||||
`if A then B else C end` will act the same as `B` if `A`
|
`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
|
produces a value other than false or null, but act the same
|
||||||
as `C` otherwise.
|
as `C` otherwise.
|
||||||
|
|
||||||
Checking for false or null is a simpler notion of
|
Checking for false or null is a simpler notion of
|
||||||
"truthiness" than is found in Javascript or Python, but it
|
"truthiness" than is found in Javascript or Python, but it
|
||||||
means that you'll sometimes have to be more explicit about
|
means that you'll sometimes have to be more explicit about
|
||||||
@@ -1359,11 +1359,11 @@ sections:
|
|||||||
string is empty using `if .name then A else B end`, you'll
|
string is empty using `if .name then A else B end`, you'll
|
||||||
need something more like `if (.name | length) > 0 then A else
|
need something more like `if (.name | length) > 0 then A else
|
||||||
B end` instead.
|
B end` instead.
|
||||||
|
|
||||||
If the condition A produces multiple results, it is
|
If the condition A produces multiple results, it is
|
||||||
considered "true" if any of those results is not false or
|
considered "true" if any of those results is not false or
|
||||||
null. If it produces zero results, it's considered false.
|
null. If it produces zero results, it's considered false.
|
||||||
|
|
||||||
More cases can be added to an if using `elif A then B` syntax.
|
More cases can be added to an if using `elif A then B` syntax.
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
@@ -1377,10 +1377,10 @@ sections:
|
|||||||
end
|
end
|
||||||
input: 2
|
input: 2
|
||||||
output: ['"many"']
|
output: ['"many"']
|
||||||
|
|
||||||
- title: "`>, >=, <=, <`"
|
- title: "`>, >=, <=, <`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
The comparison operators `>`, `>=`, `<=`, `<` return whether
|
The comparison operators `>`, `>=`, `<=`, `<` return whether
|
||||||
their left argument is greater than, greater than or equal
|
their left argument is greater than, greater than or equal
|
||||||
to, less than or equal to or less than their right argument
|
to, less than or equal to or less than their right argument
|
||||||
@@ -1407,14 +1407,14 @@ sections:
|
|||||||
so it is called as a filter to which things can be piped
|
so it is called as a filter to which things can be piped
|
||||||
rather than with special syntax, as in `.foo and .bar |
|
rather than with special syntax, as in `.foo and .bar |
|
||||||
not`.
|
not`.
|
||||||
|
|
||||||
These three only produce the values "true" and "false", and
|
These three only produce the values "true" and "false", and
|
||||||
so are only useful for genuine Boolean operations, rather
|
so are only useful for genuine Boolean operations, rather
|
||||||
than the common Perl/Python/Ruby idiom of
|
than the common Perl/Python/Ruby idiom of
|
||||||
"value_that_may_be_null or default". If you want to use this
|
"value_that_may_be_null or default". If you want to use this
|
||||||
form of "or", picking between two values rather than
|
form of "or", picking between two values rather than
|
||||||
evaluating a condition, see the "//" operator below.
|
evaluating a condition, see the "//" operator below.
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
- program: '42 and "a string"'
|
- program: '42 and "a string"'
|
||||||
input: 'null'
|
input: 'null'
|
||||||
@@ -1452,7 +1452,7 @@ sections:
|
|||||||
- program: '.foo // 42'
|
- program: '.foo // 42'
|
||||||
input: '{}'
|
input: '{}'
|
||||||
output: [42]
|
output: [42]
|
||||||
|
|
||||||
- title: Advanced features
|
- title: Advanced features
|
||||||
body: |
|
body: |
|
||||||
Variables are an absolute necessity in most programming languages, but
|
Variables are an absolute necessity in most programming languages, but
|
||||||
@@ -1477,67 +1477,67 @@ sections:
|
|||||||
entries:
|
entries:
|
||||||
- title: Variables
|
- title: Variables
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
In jq, all filters have an input and an output, so manual
|
In jq, all filters have an input and an output, so manual
|
||||||
plumbing is not necessary to pass a value from one part of a program
|
plumbing is not necessary to pass a value from one part of a program
|
||||||
to the next. Many expressions, for instance `a + b`, pass their input
|
to the next. Many expressions, for instance `a + b`, pass their input
|
||||||
to two distinct subexpressions (here `a` and `b` are both passed the
|
to two distinct subexpressions (here `a` and `b` are both passed the
|
||||||
same input), so variables aren't usually necessary in order to use a
|
same input), so variables aren't usually necessary in order to use a
|
||||||
value twice.
|
value twice.
|
||||||
|
|
||||||
For instance, calculating the average value of an array of numbers
|
For instance, calculating the average value of an array of numbers
|
||||||
requires a few variables in most languages - at least one to hold the
|
requires a few variables in most languages - at least one to hold the
|
||||||
array, perhaps one for each element or for a loop counter. In jq, it's
|
array, perhaps one for each element or for a loop counter. In jq, it's
|
||||||
simply `add / length` - the `add` expression is given the array and
|
simply `add / length` - the `add` expression is given the array and
|
||||||
produces its sum, and the `length` expression is given the array and
|
produces its sum, and the `length` expression is given the array and
|
||||||
produces its length.
|
produces its length.
|
||||||
|
|
||||||
So, there's generally a cleaner way to solve most problems in jq than
|
So, there's generally a cleaner way to solve most problems in jq than
|
||||||
defining variables. Still, sometimes they do make things easier, so jq
|
defining variables. Still, sometimes they do make things easier, so jq
|
||||||
lets you define variables using `expression as $variable`. All
|
lets you define variables using `expression as $variable`. All
|
||||||
variable names start with `$`. Here's a slightly uglier version of the
|
variable names start with `$`. Here's a slightly uglier version of the
|
||||||
array-averaging example:
|
array-averaging example:
|
||||||
|
|
||||||
length as $array_length | add / $array_length
|
length as $array_length | add / $array_length
|
||||||
|
|
||||||
We'll need a more complicated problem to find a situation where using
|
We'll need a more complicated problem to find a situation where using
|
||||||
variables actually makes our lives easier.
|
variables actually makes our lives easier.
|
||||||
|
|
||||||
|
|
||||||
Suppose we have an array of blog posts, with "author" and "title"
|
Suppose we have an array of blog posts, with "author" and "title"
|
||||||
fields, and another object which is used to map author usernames to
|
fields, and another object which is used to map author usernames to
|
||||||
real names. Our input looks like:
|
real names. Our input looks like:
|
||||||
|
|
||||||
{"posts": [{"title": "Frist psot", "author": "anon"},
|
{"posts": [{"title": "Frist psot", "author": "anon"},
|
||||||
{"title": "A well-written article", "author": "person1"}],
|
{"title": "A well-written article", "author": "person1"}],
|
||||||
"realnames": {"anon": "Anonymous Coward",
|
"realnames": {"anon": "Anonymous Coward",
|
||||||
"person1": "Person McPherson"}}
|
"person1": "Person McPherson"}}
|
||||||
|
|
||||||
We want to produce the posts with the author field containing a real
|
We want to produce the posts with the author field containing a real
|
||||||
name, as in:
|
name, as in:
|
||||||
|
|
||||||
{"title": "Frist psot", "author": "Anonymous Coward"}
|
{"title": "Frist psot", "author": "Anonymous Coward"}
|
||||||
{"title": "A well-written article", "author": "Person McPherson"}
|
{"title": "A well-written article", "author": "Person McPherson"}
|
||||||
|
|
||||||
We use a variable, $names, to store the realnames object, so that we
|
We use a variable, $names, to store the realnames object, so that we
|
||||||
can refer to it later when looking up author usernames:
|
can refer to it later when looking up author usernames:
|
||||||
|
|
||||||
.realnames as $names | .posts[] | {title, author: $names[.author]}
|
.realnames as $names | .posts[] | {title, author: $names[.author]}
|
||||||
|
|
||||||
The expression `exp as $x | ...` means: for each value of expression
|
The expression `exp as $x | ...` means: for each value of expression
|
||||||
`exp`, run the rest of the pipeline with the entire original input, and
|
`exp`, run the rest of the pipeline with the entire original input, and
|
||||||
with `$x` set to that value. Thus `as` functions as something of a
|
with `$x` set to that value. Thus `as` functions as something of a
|
||||||
foreach loop.
|
foreach loop.
|
||||||
|
|
||||||
Variables are scoped over the rest of the expression that defines
|
Variables are scoped over the rest of the expression that defines
|
||||||
them, so
|
them, so
|
||||||
|
|
||||||
.realnames as $names | (.posts[] | {title, author: $names[.author]})
|
.realnames as $names | (.posts[] | {title, author: $names[.author]})
|
||||||
|
|
||||||
will work, but
|
will work, but
|
||||||
|
|
||||||
(.realnames as $names | .posts[]) | {title, author: $names[.author]}
|
(.realnames as $names | .posts[]) | {title, author: $names[.author]}
|
||||||
|
|
||||||
won't.
|
won't.
|
||||||
|
|
||||||
For programming language theorists, it's more accurate to
|
For programming language theorists, it's more accurate to
|
||||||
@@ -1556,11 +1556,11 @@ sections:
|
|||||||
|
|
||||||
- title: 'Defining Functions'
|
- title: 'Defining Functions'
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
You can give a filter a name using "def" syntax:
|
You can give a filter a name using "def" syntax:
|
||||||
|
|
||||||
def increment: . + 1;
|
def increment: . + 1;
|
||||||
|
|
||||||
From then on, `increment` is usable as a filter just like a
|
From then on, `increment` is usable as a filter just like a
|
||||||
builtin function (in fact, this is how some of the builtins
|
builtin function (in fact, this is how some of the builtins
|
||||||
are defined). A function may take arguments:
|
are defined). A function may take arguments:
|
||||||
@@ -1606,19 +1606,19 @@ sections:
|
|||||||
|
|
||||||
- title: Reduce
|
- title: Reduce
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
The `reduce` syntax in jq allows you to combine all of the
|
The `reduce` syntax in jq allows you to combine all of the
|
||||||
results of an expression by accumulating them into a single
|
results of an expression by accumulating them into a single
|
||||||
answer. As an example, we'll pass `[3,2,1]` to this expression:
|
answer. As an example, we'll pass `[3,2,1]` to this expression:
|
||||||
|
|
||||||
reduce .[] as $item (0; . + $item)
|
reduce .[] as $item (0; . + $item)
|
||||||
|
|
||||||
For each result that `.[]` produces, `. + $item` is run to
|
For each result that `.[]` produces, `. + $item` is run to
|
||||||
accumulate a running total, starting from 0. In this
|
accumulate a running total, starting from 0. In this
|
||||||
example, `.[]` produces the results 3, 2, and 1, so the
|
example, `.[]` produces the results 3, 2, and 1, so the
|
||||||
effect is similar to running something like this:
|
effect is similar to running something like this:
|
||||||
|
|
||||||
0 | (3 as $item | . + $item) |
|
0 | (3 as $item | . + $item) |
|
||||||
(2 as $item | . + $item) |
|
(2 as $item | . + $item) |
|
||||||
(1 as $item | . + $item)
|
(1 as $item | . + $item)
|
||||||
|
|
||||||
@@ -1626,7 +1626,7 @@ sections:
|
|||||||
- program: 'reduce .[] as $item (0; . + $item)'
|
- program: 'reduce .[] as $item (0; . + $item)'
|
||||||
input: '[10,2,5,3]'
|
input: '[10,2,5,3]'
|
||||||
output: ['20']
|
output: ['20']
|
||||||
|
|
||||||
|
|
||||||
- title: Assignment
|
- title: Assignment
|
||||||
body: |
|
body: |
|
||||||
@@ -1648,14 +1648,14 @@ sections:
|
|||||||
entries:
|
entries:
|
||||||
- title: "`=`"
|
- title: "`=`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
The filter `.foo = 1` will take as input an object
|
The filter `.foo = 1` will take as input an object
|
||||||
and produce as output an object with the "foo" field set to
|
and produce as output an object with the "foo" field set to
|
||||||
1. There is no notion of "modifying" or "changing" something
|
1. There is no notion of "modifying" or "changing" something
|
||||||
in jq - all jq values are immutable. For instance,
|
in jq - all jq values are immutable. For instance,
|
||||||
|
|
||||||
.foo = .bar | .foo.baz = 1
|
.foo = .bar | .foo.baz = 1
|
||||||
|
|
||||||
will not have the side-effect of setting .bar.baz to be set
|
will not have the side-effect of setting .bar.baz to be set
|
||||||
to 1, as the similar-looking program in Javascript, Python,
|
to 1, as the similar-looking program in Javascript, Python,
|
||||||
Ruby or other languages would. Unlike these languages (but
|
Ruby or other languages would. Unlike these languages (but
|
||||||
@@ -1664,7 +1664,7 @@ sections:
|
|||||||
"the same object". They can be equal, or not equal, but if
|
"the same object". They can be equal, or not equal, but if
|
||||||
we change one of them in no circumstances will the other
|
we change one of them in no circumstances will the other
|
||||||
change behind our backs.
|
change behind our backs.
|
||||||
|
|
||||||
This means that it's impossible to build circular values in
|
This means that it's impossible to build circular values in
|
||||||
jq (such as an array whose first element is itself). This is
|
jq (such as an array whose first element is itself). This is
|
||||||
quite intentional, and ensures that anything a jq program
|
quite intentional, and ensures that anything a jq program
|
||||||
@@ -1677,14 +1677,14 @@ sections:
|
|||||||
works out the new value for the property being assigned to by running
|
works out the new value for the property being assigned to by running
|
||||||
the old value through this expression. For instance, .foo |= .+1 will
|
the old value through this expression. For instance, .foo |= .+1 will
|
||||||
build an object with the "foo" field set to the input's "foo" plus 1.
|
build an object with the "foo" field set to the input's "foo" plus 1.
|
||||||
|
|
||||||
This example should show the difference between '=' and '|=':
|
This example should show the difference between '=' and '|=':
|
||||||
|
|
||||||
Provide input '{"a": {"b": 10}, "b": 20}' to the programs:
|
Provide input '{"a": {"b": 10}, "b": 20}' to the programs:
|
||||||
|
|
||||||
.a = .b
|
.a = .b
|
||||||
.a |= .b
|
.a |= .b
|
||||||
|
|
||||||
The former will set the "a" field of the input to the "b" field of the
|
The former will set the "a" field of the input to the "b" field of the
|
||||||
input, and produce the output {"a": 20}. The latter will set the "a"
|
input, and produce the output {"a": 20}. The latter will set the "a"
|
||||||
field of the input to the "a" field's "b" field, producing {"a": 10}.
|
field of the input to the "a" field's "b" field, producing {"a": 10}.
|
||||||
@@ -1699,43 +1699,43 @@ sections:
|
|||||||
- program: .foo += 1
|
- program: .foo += 1
|
||||||
input: '{"foo": 42}'
|
input: '{"foo": 42}'
|
||||||
output: ['{"foo": 43}']
|
output: ['{"foo": 43}']
|
||||||
|
|
||||||
- title: Complex assignments
|
- title: Complex assignments
|
||||||
body: |
|
body: |
|
||||||
Lots more things are allowed on the left-hand side of a jq assignment
|
Lots more things are allowed on the left-hand side of a jq assignment
|
||||||
than in most langauges. We've already seen simple field accesses on
|
than in most langauges. We've already seen simple field accesses on
|
||||||
the left hand side, and it's no surprise that array accesses work just
|
the left hand side, and it's no surprise that array accesses work just
|
||||||
as well:
|
as well:
|
||||||
|
|
||||||
.posts[0].title = "JQ Manual"
|
.posts[0].title = "JQ Manual"
|
||||||
|
|
||||||
What may come as a surprise is that the expression on the left may
|
What may come as a surprise is that the expression on the left may
|
||||||
produce multiple results, referring to different points in the input
|
produce multiple results, referring to different points in the input
|
||||||
document:
|
document:
|
||||||
|
|
||||||
.posts[].comments |= . + ["this is great"]
|
.posts[].comments |= . + ["this is great"]
|
||||||
|
|
||||||
That example appends the string "this is great" to the "comments"
|
That example appends the string "this is great" to the "comments"
|
||||||
array of each post in the input (where the input is an object with a
|
array of each post in the input (where the input is an object with a
|
||||||
field "posts" which is an array of posts).
|
field "posts" which is an array of posts).
|
||||||
|
|
||||||
When jq encounters an assignment like 'a = b', it records the "path"
|
When jq encounters an assignment like 'a = b', it records the "path"
|
||||||
taken to select a part of the input document while executing a. This
|
taken to select a part of the input document while executing a. This
|
||||||
path is then used to find which part of the input to change while
|
path is then used to find which part of the input to change while
|
||||||
executing the assignment. Any filter may be used on the
|
executing the assignment. Any filter may be used on the
|
||||||
left-hand side of an equals - whichever paths it selects from the
|
left-hand side of an equals - whichever paths it selects from the
|
||||||
input will be where the assignment is performed.
|
input will be where the assignment is performed.
|
||||||
|
|
||||||
This is a very powerful operation. Suppose we wanted to add a comment
|
This is a very powerful operation. Suppose we wanted to add a comment
|
||||||
to blog posts, using the same "blog" input above. This time, we only
|
to blog posts, using the same "blog" input above. This time, we only
|
||||||
want to comment on the posts written by "stedolan". We can find those
|
want to comment on the posts written by "stedolan". We can find those
|
||||||
posts using the "select" function described earlier:
|
posts using the "select" function described earlier:
|
||||||
|
|
||||||
.posts[] | select(.author == "stedolan")
|
.posts[] | select(.author == "stedolan")
|
||||||
|
|
||||||
The paths provided by this operation point to each of the posts that
|
The paths provided by this operation point to each of the posts that
|
||||||
"stedolan" wrote, and we can comment on each of them in the same way
|
"stedolan" wrote, and we can comment on each of them in the same way
|
||||||
that we did before:
|
that we did before:
|
||||||
|
|
||||||
(.posts[] | select(.author == "stedolan") | .comments) |=
|
(.posts[] | select(.author == "stedolan") | .comments) |=
|
||||||
. + ["terrible."]
|
. + ["terrible."]
|
||||||
|
|||||||
Reference in New Issue
Block a user