Improve manual in various ways (inputs, sort_by, foreach sections, etc.) (#2744)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
- Add error/0 and mentions null input behavior (close #2231) - Explain value iterator suffix syntax .foo[] (close #1047) - Mention array slicing is also zero-based (close #2094) - Add examples of input and inputs filters (close #2216, close #2470) - Improve sort_by about multiple values (close #2103, close #2467, close #2474) - Improve foreach section and simplify examples (close #1148, close #2169) - Fix recurse/1 document on how it is identical using recurse/2 (close #2036, close #2412) - Add non-string examples of index/1, rindex/1 (close #1422) - Simplify the example of truncate_stream/1 (close #1736)
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+698 added−302 removed
- tests:+89 added−12 removed
- other:+609 added−290 removed
Provenance
- Commit
ed334b5- Tree
a71465f- Author
itchyny <itchyny@cybozu.co.jp>- Committer
GitHub <noreply@github.com>- Parent
1cf6515
9 files changed+698 added−302 removed
modified:docs/content/download/default.yml+1 added-1 removed
153 unmodified lines154 155 You can build it using the usual `./configure && make && sudo156 make install` rigmarole.157 158- If you're interested in using the lastest development version, try:158+ If you're interested in using the latest development version, try:159 160 git clone --recursive https://github.com/jqlang/jq.git161 cd jq162 autoreconf -i45 unmodified linesmodified:docs/content/manual/v1.3/manual.yml+20 added-21 removed
74 unmodified lines75 76 jq filters run on a stream of JSON data. The input to jq is77 parsed as a sequence of whitespace-separated JSON values which78 are passed through the provided filter one at a time. The79- output(s) of the filter are written to standard out, again as a80- sequence of whitespace-separated JSON data.79+ output(s) of the filter are written to standard output, as a80+ sequence of newline-separated JSON data.81 82 You can affect how jq reads and writes its input and output83 using some command-line options:84 86 unmodified lines171 body: |172 173 You can also look up fields of an object using syntax like174 `.["foo"]` (`.foo` above is a shorthand version of this). This175- one works for arrays as well, if the key is an176- integer. Arrays are zero-based (like javascript), so `.[2]`177- returns the third element of the array.175+ one works for arrays as well, if the key is an integer. Arrays176+ are zero-based, so `.[2]` returns the third element of the array.177 178 The `.[10:15]` syntax can be used to return a subarray of an179 array. The array returned by `.[10:15]` will be of length 5,180 containing the elements from index 10 (inclusive) to index96 unmodified lines277 strings, booleans, arrays, objects (which in JSON-speak are278 hashes with only string keys), and "null".279 280 Booleans, null, strings and numbers are written the same way as282- in javascript. Just like everything else in jq, these simple281+ in JSON. Just like everything else in jq, these simple282 values take an input and produce an output - `42` is a valid jq283 expression that takes an input, ignores it, and returns 42284 instead.285 619 unmodified lines905 "truthiness" than is found in JavaScript or Python, but it906 means that you'll sometimes have to be more explicit about907 the condition you want: you can't test whether, e.g. a908 string is empty using `if .name then A else B end`, you'll910- need something more like `if (.name | count) > 0 then A else909+ need something more like `if (.name | length) > 0 then A else910 B end` instead.911 912 If the condition `A` produces multiple results, then `B` is evaluated913 once for each result that is not false or null, and `C` is evaluated124 unmodified lines1038 simply `add / length` - the `add` expression is given the array and1039 produces its sum, and the `length` expression is given the array and1040 produces its length.1041 1043- So, there's generally a cleaner way to solve most problems in jq that1042+ So, there's generally a cleaner way to solve most problems in jq than1043 defining variables. Still, sometimes they do make things easier, so jq1044 lets you define variables using `expression as $variable`. All1045 variable names start with `$`. Here's a slightly uglier version of the1046 array-averaging example:79 unmodified lines1126 - program: 'def addvalue(f): f as $x | map(. + $x); addvalue(.[0])'1127 input: '[[1,2],[10,20]]'1128 output: ['[[1,2,1,2], [10,20,1,2]]']1129 1131- - title: Reduce1130+ - title: "`reduce`"1131 body: |1132 1134- The `reduce` syntax in jq allows you to combine all of the1135- results of an expression by accumulating them into a single1136- answer. As an example, we'll pass `[3,2,1]` to this expression:1133+ The `reduce` syntax allows you to combine all of the results of1134+ an expression by accumulating them into a single answer.1135+ The form is `reduce EXP as $var (INIT; UPDATE)`.1136+ As an example, we'll pass `[1,2,3]` to this expression:1137 1138 reduce .[] as $item (0; . + $item)1139 1140 For each result that `.[]` produces, `. + $item` is run to1141- accumulate a running total, starting from 0. In this1142- example, `.[]` produces the results 3, 2, and 1, so the1143- effect is similar to running something like this:1141+ accumulate a running total, starting from 0 as the input value.1142+ In this example, `.[]` produces the results `1`, `2`, and `3`,1143+ so the effect is similar to running something like this:1144 1145- 0 | (3 as $item | . + $item) |1146- (2 as $item | . + $item) |1147- (1 as $item | . + $item)1145+ 0 | 1 as $item | . + $item |1146+ 2 as $item | . + $item |1147+ 3 as $item | . + $item1148 1149 examples:1150 - program: 'reduce .[] as $item (0; . + $item)'1151- input: '[10,2,5,3]'1152- output: ['20']1151+ input: '[1,2,3,4,5]'1152+ output: ['15']1153 1154-1154 - title: Assignment1155 body: |1156 1157 Assignment works a little differently in jq than in most107 unmodified linesmodified:docs/content/manual/v1.4/manual.yml+19 added-20 removed
74 unmodified lines75 76 jq filters run on a stream of JSON data. The input to jq is77 parsed as a sequence of whitespace-separated JSON values which78 are passed through the provided filter one at a time. The79- output(s) of the filter are written to standard out, again as a80- sequence of whitespace-separated JSON data.79+ output(s) of the filter are written to standard output, as a80+ sequence of newline-separated JSON data.81 82 Note: it is important to mind the shell's quoting rules. As a83 general rule it's best to always quote (with single-quote84 characters) the jq program, as too many characters with special167 unmodified lines252 body: |253 254 You can also look up fields of an object using syntax like255 `.["foo"]` (`.foo` above is a shorthand version of this). This256- one works for arrays as well, if the key is an257- integer. Arrays are zero-based (like javascript), so `.[2]`258- returns the third element of the array.256+ one works for arrays as well, if the key is an integer. Arrays257+ are zero-based, so `.[2]` returns the third element of the array.258 259 The `.[10:15]` syntax can be used to return a subarray of an260 array or substring of a string. The array returned by261 `.[10:15]` will be of length 5, containing the elements from262 index 10 (inclusive) to index 15 (exclusive). Either index may263 be negative (in which case it counts backwards from the end of264 the array), or omitted (in which case it refers to the start266- or end of the array).265+ or end of the array). Indices are zero-based.266 267 The `?` "operator" can also be used with the slice operator,268 as in `.[10:15]?`, which outputs values where the inputs are269 slice-able.103 unmodified lines373 strings, booleans, arrays, objects (which in JSON-speak are374 hashes with only string keys), and "null".375 376 Booleans, null, strings and numbers are written the same way as378- in javascript. Just like everything else in jq, these simple377+ in JSON. Just like everything else in jq, these simple378 values take an input and produce an output - `42` is a valid jq379 expression that takes an input, ignores it, and returns 42380 instead.381 1146 unmodified lines1528 - program: 'def addvalue(f): f as $x | map(. + $x); addvalue(.[0])'1529 input: '[[1,2],[10,20]]'1530 output: ['[[1,2,1,2], [10,20,1,2]]']1531 1533- - title: Reduce1532+ - title: "`reduce`"1533 body: |1534 1536- The `reduce` syntax in jq allows you to combine all of the1537- results of an expression by accumulating them into a single1538- answer. As an example, we'll pass `[3,2,1]` to this expression:1535+ The `reduce` syntax allows you to combine all of the results of1536+ an expression by accumulating them into a single answer.1537+ The form is `reduce EXP as $var (INIT; UPDATE)`.1538+ As an example, we'll pass `[1,2,3]` to this expression:1539 1540 reduce .[] as $item (0; . + $item)1541 1542 For each result that `.[]` produces, `. + $item` is run to1543- accumulate a running total, starting from 0. In this1544- example, `.[]` produces the results 3, 2, and 1, so the1545- effect is similar to running something like this:1543+ accumulate a running total, starting from 0 as the input value.1544+ In this example, `.[]` produces the results `1`, `2`, and `3`,1545+ so the effect is similar to running something like this:1546 1547- 0 | (3 as $item | . + $item) |1548- (2 as $item | . + $item) |1549- (1 as $item | . + $item)1547+ 0 | 1 as $item | . + $item |1548+ 2 as $item | . + $item |1549+ 3 as $item | . + $item1550 1551 examples:1552 - program: 'reduce .[] as $item (0; . + $item)'1553- input: '[10,2,5,3]'1554- output: ['20']1553+ input: '[1,2,3,4,5]'1554+ output: ['15']1555 1556-1556 - title: Assignment1557 body: |1558 1559 Assignment works a little differently in jq than in most107 unmodified linesmodified:docs/content/manual/v1.5/manual.yml+126 added-62 removed
76 unmodified lines77 78 jq filters run on a stream of JSON data. The input to jq is79 parsed as a sequence of whitespace-separated JSON values which80 are passed through the provided filter one at a time. The81- output(s) of the filter are written to standard out, again as a82- sequence of whitespace-separated JSON data.81+ output(s) of the filter are written to standard output, as a82+ sequence of newline-separated JSON data.83 84 Note: it is important to mind the shell's quoting rules. As a85 general rule it's best to always quote (with single-quote86 characters) the jq program, as too many characters with special231 unmodified lines318 body: |319 320 You can also look up fields of an object using syntax like321 `.["foo"]` (`.foo` above is a shorthand version of this). This322- one works for arrays as well, if the key is an323- integer. Arrays are zero-based (like javascript), so `.[2]`324- returns the third element of the array.322+ one works for arrays as well, if the key is an integer. Arrays323+ are zero-based, so `.[2]` returns the third element of the array.324 325 The `.[10:15]` syntax can be used to return a subarray of an326 array or substring of a string. The array returned by327 `.[10:15]` will be of length 5, containing the elements from328 index 10 (inclusive) to index 15 (exclusive). Either index may329 be negative (in which case it counts backwards from the end of330 the array), or omitted (in which case it refers to the start332- or end of the array).331+ or end of the array). Indices are zero-based.332 333 The `.[2]` syntax can be used to return the element at the334 given index. Negative indices are allowed, with -1 referring335 to the last element, -2 referring to the next to last element,44 unmodified lines380 If you use the `.[index]` syntax, but omit the index381 entirely, it will return *all* of the elements of an382 array. Running `.[]` with the input `[1,2,3]` will produce the383 numbers as three separate results, rather than as a single385- array.384+ array. A filter of the form `.foo[]` is equivalent to385+ `.foo | .[]`.386 387 You can also use this on an object, and it will return all388 the values of the object.389 7 unmodified lines397 - program: '.[]'398 input: '[]'399 output: []400 401+ - program: '.foo[]'402+ input: '{"foo":[1,2,3]}'403+ output: ['1','2','3']404+405 - program: '.[]'406 input: '{"a": 1, "b": 1}'407 output: ['1', '1']408 409 - title: "`.[]?`"410 body: |411 412 Like `.[]`, but no errors will be output if . is not an array409- or object.413+ or object. A filter of the form `.foo[]?` is equivalent to414+ `.foo | .[]?`.415 416 - title: "`,`"417 body: |418 41 unmodified lines460 strings, booleans, arrays, objects (which in JSON-speak are461 hashes with only string keys), and "null".462 463 Booleans, null, strings and numbers are written the same way as459- in javascript. Just like everything else in jq, these simple464+ in JSON. Just like everything else in jq, these simple465 values take an input and produce an output - `42` is a valid jq466 expression that takes an input, ignores it, and returns 42467 instead.468 378 unmodified lines847 - program: '[1,2,empty,3]'848 input: 'null'849 output: ['[1,2,3]']850 846- - title: "`error(message)`"851+ - title: "`error`, `error(message)`"852 body: |853 849- Produces an error, just like `.a` applied to values other than850- null and objects would, but with the given message as the851- error's value.854+ Produces an error with the input value, or with the message855+ given as the argument. Errors can be caught with try/catch;856+ see below.857 858+ When the error value is `null`, it produces nothing and works859+ just like `empty`. So `[null | error]` and `[error(null)]` both860+ emit `[]`.861+862+ examples:863+ - program: 'try error catch .'864+ input: '"error message"'865+ output: ['"error message"']866+867+ - program: 'try error("invalid value: \(.)") catch .'868+ input: '42'869+ output: ['"invalid value: 42"']870+871 - title: "`$__loc__`"872 body: |873 874 Produces an object with a "file" key and a "line" key, with147 unmodified lines1022 - program: flatten1023 input: '[{"foo": "bar"}, [{"foo": "baz"}]]'1024 output: ['[{"foo": "bar"}, {"foo": "baz"}]']1025 1008- - title: "`range(upto)`, `range(from; upto)` `range(from; upto; by)`"1026+ - title: "`range(upto)`, `range(from; upto)`, `range(from; upto; by)`"1027 body: |1028 1029 The `range` function produces a range of numbers. `range(4; 10)`1030 produces 6 numbers, from 4 (inclusive) to 10 (exclusive). The numbers127 unmodified lines1158 compared by comparing their sets of keys (as arrays in1159 sorted order), and if their keys are equal then the values1160 are compared key by key.1161 1144- `sort` may be used to sort by a particular field of an1145- object, or by applying any jq filter.1162+ `sort_by` may be used to sort by a particular field of an1163+ object, or by applying any jq filter. `sort_by(f)` compares1164+ two elements by comparing the result of `f` on each element.1165+ When `f` produces multiple values, it firstly compares the1166+ first values, and the second values if the first values are1167+ equal, and so on.1168 1147- `sort_by(f)` compares two elements by comparing the result of1148- `f` on each element.1149-1169 examples:1170 - program: 'sort'1171 input: '[8,3,null,6]'1172 output: ['[null,3,6,8]']1173+1174 - program: 'sort_by(.foo)'1155- input: '[{"foo":4, "bar":10}, {"foo":3, "bar":100}, {"foo":2, "bar":1}]'1156- output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":100}, {"foo":4, "bar":10}]']1175+ input: '[{"foo":4, "bar":10}, {"foo":3, "bar":10}, {"foo":2, "bar":1}]'1176+ output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":4, "bar":10}]']1177 1178+ - program: 'sort_by(.foo, .bar)'1179+ input: '[{"foo":4, "bar":10}, {"foo":3, "bar":20}, {"foo":2, "bar":1}, {"foo":3, "bar":10}]'1180+ output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":3, "bar":20}, {"foo":4, "bar":10}]']1181+1182 - title: "`group_by(path_expression)`"1183 body: |1184 1185 `group_by(.foo)` takes as input an array, groups the117 unmodified lines1303 examples:1304 - program: 'index(", ")'1305 input: '"a,b, cd, efg, hijk"'1306 output: ['3']1307+ - program: 'index(1)'1308+ input: '[0,1,2,1,3,1,4]'1309+ output: ['1']1310+ - program: 'index([1,2])'1311+ input: '[0,1,2,3,1,4,2,5,1,2,6,7]'1312+ output: ['1']1313 - program: 'rindex(", ")'1314 input: '"a,b, cd, efg, hijk"'1315 output: ['12']1316+ - program: 'rindex(1)'1317+ input: '[0,1,2,1,3,1,4]'1318+ output: ['5']1319+ - program: 'rindex([1,2])'1320+ input: '[0,1,2,3,1,4,2,5,1,2,6,7]'1321+ output: ['8']1322 1323 - title: "`inside`"1324 body: |1325 1017 unmodified lines2343 - program: 'def addvalue(f): f as $x | map(. + $x); addvalue(.[0])'2344 input: '[[1,2],[10,20]]'2345 output: ['[[1,2,1,2], [10,20,1,2]]']2346 2311- - title: Reduce2312- body: |2313-2314- The `reduce` syntax in jq allows you to combine all of the2315- results of an expression by accumulating them into a single2316- answer. As an example, we'll pass `[3,2,1]` to this expression:2317-2318- reduce .[] as $item (0; . + $item)2319-2320- For each result that `.[]` produces, `. + $item` is run to2321- accumulate a running total, starting from 0. In this2322- example, `.[]` produces the results 3, 2, and 1, so the2323- effect is similar to running something like this:2324-2325- 0 | (3 as $item | . + $item) |2326- (2 as $item | . + $item) |2327- (1 as $item | . + $item)2328-2329- examples:2330- - program: 'reduce .[] as $item (0; . + $item)'2331- input: '[10,2,5,3]'2332- output: ['20']2333-2347 - title: "`limit(n; exp)`"2348 body: |2349 2350 The `limit` function extracts up to `n` outputs from `exp`.31 unmodified lines2382 - program: '[range(.)]|[first, last, nth(5)]'2383 input: '10'2384 output: ['[0,9,5]']2385 2386+ - title: "`reduce`"2387+ body: |2388+2389+ The `reduce` syntax allows you to combine all of the results of2390+ an expression by accumulating them into a single answer.2391+ The form is `reduce EXP as $var (INIT; UPDATE)`.2392+ As an example, we'll pass `[1,2,3]` to this expression:2393+2394+ reduce .[] as $item (0; . + $item)2395+2396+ For each result that `.[]` produces, `. + $item` is run to2397+ accumulate a running total, starting from 0 as the input value.2398+ In this example, `.[]` produces the results `1`, `2`, and `3`,2399+ so the effect is similar to running something like this:2400+2401+ 0 | 1 as $item | . + $item |2402+ 2 as $item | . + $item |2403+ 3 as $item | . + $item2404+2405+ examples:2406+ - program: 'reduce .[] as $item (0; . + $item)'2407+ input: '[1,2,3,4,5]'2408+ output: ['15']2409+2410+ - program: 'reduce .[] as [$i,$j] (0; . + $i * $j)'2411+ input: '[[1,2],[3,4],[5,6]]'2412+ output: ['44']2413+2414+ - program: 'reduce .[] as {$x,$y} (null; .x += $x | .y += [$y])'2415+ input: '[{"x":"a","y":1},{"x":"b","y":2},{"x":"c","y":3}]'2416+ output: ['{"x":"abc","y":[1,2,3]}']2417+2418 - title: "`foreach`"2419 body: |2420 2421 The `foreach` syntax is similar to `reduce`, but intended to2422 allow the construction of `limit` and reducers that produce2378- intermediate results (see example).2423+ intermediate results.2424 2425 The form is `foreach EXP as $var (INIT; UPDATE; EXTRACT)`.2381- Like `reduce`, `INIT` is evaluated once to produce a state2382- value, then each output of `EXP` is bound to `$var`, `UPDATE`2383- is evaluated for each output of `EXP` with the current state2384- and with `$var` visible. Each value output by `UPDATE`2385- replaces the previous state. Finally, `EXTRACT` is evaluated2386- for each new state to extract an output of `foreach`.2426+ As an example, we'll pass `[1,2,3]` to this expression:2427 2388- This is mostly useful only for constructing `reduce`- and2389- `limit`-like functions. But it is much more general, as it2390- allows for partial reductions (see the example below).2428+ foreach .[] as $item (0; . + $item; [$item, . * 2])2429 2430+ Like the `reduce` syntax, `. + $item` is run for each result2431+ that `.[]` produces, but `[$item, . * 2]` is run for each2432+ intermediate values. In this example, since the intermediate2433+ values are `1`, `3`, and `6`, the `foreach` expression produces2434+ `[1,2]`, `[2,6]`, and `[3,12]`. So the effect is similar2435+ to running something like this:2436+2437+ 0 | 1 as $item | . + $item | [$item, . * 2],2438+ 2 as $item | . + $item | [$item, . * 2],2439+ 3 as $item | . + $item | [$item, . * 2]2440+2441+ When `EXTRACT` is omitted, the identity filter is used.2442+ That is, it outputs the intermediate values as they are.2443+2444 examples:2393- - program: '[foreach .[] as $item2394- ([[],[]];2395- if $item == null then [[],.[0]] else [(.[0] + [$item]),[]] end;2396- if $item == null then .[1] else empty end)]'2397- input: '[1,2,3,4,null,"a","b",null]'2398- output: ['[[1,2,3,4],["a","b"]]']2445+ - program: 'foreach .[] as $item (0; . + $item)'2446+ input: '[1,2,3,4,5]'2447+ output: ['1','3','6','10','15']2448 2449+ - program: 'foreach .[] as $item (0; . + $item; [$item, . * 2])'2450+ input: '[1,2,3,4,5]'2451+ output: ['[1,2]','[2,6]','[3,12]','[4,20]','[5,30]']2452+2453+ - program: 'foreach .[] as $item (0; . + 1; {index: ., $item})'2454+ input: '["foo", "bar", "baz"]'2455+ output:2456+ - '{"index":1,"item":"foo"}'2457+ - '{"index":2,"item":"bar"}'2458+ - '{"index":3,"item":"baz"}'2459+2460 - title: Recursion2461 body: |2462 2463 As described above, `recurse` uses recursion, and any jq106 unmodified lines2570 body: |2571 2572 Outputs one new input.2573 2574+ echo 1 2 3 4 | jq '[., input]' # [1,2] [3,4]2575+2576 - title: "`inputs`"2577 body: |2578 2579 Outputs all remaining inputs, one by one.2580 2581 This is primarily useful for reductions over a program's2582 inputs.2583 2584+ echo 1 2 3 | jq -n 'reduce inputs as $i (0; . + $i)' # 62585+2586 - title: "`debug`"2587 body: |2588 2589 Causes a debug message based on the input value to be44 unmodified lines2634 number of path elements from the left of the outputs of the2635 given streaming expression.2636 2637 examples:2574- - program: '[1|truncate_stream([[0],1],[[1,0],2],[[1,0]],[[1]])]'2638+ - program: 'truncate_stream([[0],1],[[1,0],2],[[1,0]],[[1]])'2639 input: '1'2576- output: ['[[[0],2],[[0]]]']2640+ output: ['[[0],2]', '[[0]]']2641 2642 - title: "`fromstream(stream_expression)`"2643 body: |2644 286 unmodified linesmodified:docs/content/manual/v1.6/manual.yml+135 added-61 removed
76 unmodified lines77 78 jq filters run on a stream of JSON data. The input to jq is79 parsed as a sequence of whitespace-separated JSON values which80 are passed through the provided filter one at a time. The81- output(s) of the filter are written to standard out, again as a82- sequence of whitespace-separated JSON data.81+ output(s) of the filter are written to standard output, as a82+ sequence of newline-separated JSON data.83 84 Note: it is important to mind the shell's quoting rules. As a85 general rule it's best to always quote (with single-quote86 characters) the jq program, as too many characters with special309 unmodified lines396 elements from index 10 (inclusive) to index 15 (exclusive).397 Either index may be negative (in which case it counts398 backwards from the end of the array), or omitted (in which399 case it refers to the start or end of the array).400+ Indices are zero-based.401 402 examples:403 - program: '.[2:4]'404 input: '["a","b","c","d","e"]'17 unmodified lines422 If you use the `.[index]` syntax, but omit the index423 entirely, it will return *all* of the elements of an424 array. Running `.[]` with the input `[1,2,3]` will produce the425 numbers as three separate results, rather than as a single425- array.426+ array. A filter of the form `.foo[]` is equivalent to427+ `.foo | .[]`.428 429 You can also use this on an object, and it will return all430 the values of the object.431 7 unmodified lines439 - program: '.[]'440 input: '[]'441 output: []442 443+ - program: '.foo[]'444+ input: '{"foo":[1,2,3]}'445+ output: ['1','2','3']446+447 - program: '.[]'448 input: '{"a": 1, "b": 1}'449 output: ['1', '1']450 451 - title: "`.[]?`"452 body: |453 454 Like `.[]`, but no errors will be output if . is not an array449- or object.455+ or object. A filter of the form `.foo[]?` is equivalent to456+ `.foo | .[]?`.457 458 - title: "Comma: `,`"459 body: |460 61 unmodified lines522 strings, booleans, arrays, objects (which in JSON-speak are523 hashes with only string keys), and "null".524 525 Booleans, null, strings and numbers are written the same way as519- in javascript. Just like everything else in jq, these simple526+ in JSON. Just like everything else in jq, these simple527 values take an input and produce an output - `42` is a valid jq528 expression that takes an input, ignores it, and returns 42529 instead.530 93 unmodified lines624 625 Recursively descends `.`, producing every value. This is the626 same as the zero-argument `recurse` builtin (see below). This627 is intended to resemble the XPath `//` operator. Note that621- `..a` does not work; use `..|.a` instead. In the example622- below we use `..|.a?` to find all the values of object keys628+ `..a` does not work; use `.. | .a` instead. In the example629+ below we use `.. | .a?` to find all the values of object keys630 "a" in any object found "below" `.`.631 632 This is particularly useful in conjunction with `path(EXP)`633 (also see below) and the `?` operator.634 635 examples:629- - program: '..|.a?'636+ - program: '.. | .a?'637 input: '[[{"a":1}]]'638 output: ['1']639 640 - title: Builtin operators and functions373 unmodified lines1014 - program: '[1,2,empty,3]'1015 input: 'null'1016 output: ['[1,2,3]']1017 1011- - title: "`error(message)`"1018+ - title: "`error`, `error(message)`"1019 body: |1020 1014- Produces an error, just like `.a` applied to values other than1015- null and objects would, but with the given message as the1016- error's value. Errors can be caught with try/catch; see below.1021+ Produces an error with the input value, or with the message1022+ given as the argument. Errors can be caught with try/catch;1023+ see below.1024 1025+ When the error value is `null`, it produces nothing and works1026+ just like `empty`. So `[null | error]` and `[error(null)]` both1027+ emit `[]`.1028+1029+ examples:1030+ - program: 'try error catch .'1031+ input: '"error message"'1032+ output: ['"error message"']1033+1034+ - program: 'try error("invalid value: \(.)") catch .'1035+ input: '42'1036+ output: ['"invalid value: 42"']1037+1038 - title: "`halt`"1039 body: |1040 1041 Stops the jq program with no further outputs. jq will exit279 unmodified lines1321 compared by comparing their sets of keys (as arrays in1322 sorted order), and if their keys are equal then the values1323 are compared key by key.1324 1305- `sort` may be used to sort by a particular field of an1306- object, or by applying any jq filter.1325+ `sort_by` may be used to sort by a particular field of an1326+ object, or by applying any jq filter. `sort_by(f)` compares1327+ two elements by comparing the result of `f` on each element.1328+ When `f` produces multiple values, it firstly compares the1329+ first values, and the second values if the first values are1330+ equal, and so on.1331 1308- `sort_by(f)` compares two elements by comparing the result of1309- `f` on each element.1310-1332 examples:1333 - program: 'sort'1334 input: '[8,3,null,6]'1335 output: ['[null,3,6,8]']1336+1337 - program: 'sort_by(.foo)'1316- input: '[{"foo":4, "bar":10}, {"foo":3, "bar":100}, {"foo":2, "bar":1}]'1317- output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":100}, {"foo":4, "bar":10}]']1338+ input: '[{"foo":4, "bar":10}, {"foo":3, "bar":10}, {"foo":2, "bar":1}]'1339+ output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":4, "bar":10}]']1340 1341+ - program: 'sort_by(.foo, .bar)'1342+ input: '[{"foo":4, "bar":10}, {"foo":3, "bar":20}, {"foo":2, "bar":1}, {"foo":3, "bar":10}]'1343+ output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":3, "bar":20}, {"foo":4, "bar":10}]']1344+1345 - title: "`group_by(path_expression)`"1346 body: |1347 1348 `group_by(.foo)` takes as input an array, groups the117 unmodified lines1466 examples:1467 - program: 'index(", ")'1468 input: '"a,b, cd, efg, hijk"'1469 output: ['3']1470+ - program: 'index(1)'1471+ input: '[0,1,2,1,3,1,4]'1472+ output: ['1']1473+ - program: 'index([1,2])'1474+ input: '[0,1,2,3,1,4,2,5,1,2,6,7]'1475+ output: ['1']1476 - program: 'rindex(", ")'1477 input: '"a,b, cd, efg, hijk"'1478 output: ['12']1479+ - program: 'rindex(1)'1480+ input: '[0,1,2,1,3,1,4]'1481+ output: ['5']1482+ - program: 'rindex([1,2])'1483+ input: '[0,1,2,3,1,4,2,5,1,2,6,7]'1484+ output: ['8']1485 1486 - title: "`inside`"1487 body: |1488 196 unmodified lines1685 1686 When called without an argument, `recurse` is equivalent to1687 `recurse(.[]?)`.1688 1651- `recurse(f)` is identical to `recurse(f; . != null)` and can be1689+ `recurse(f)` is identical to `recurse(f; true)` and can be1690 used without concerns about recursion depth.1691 1692 `recurse(f; condition)` is a generator which begins by1693 emitting . and then emits in turn .|f, .|f|f, .|f|f|f, ... so long977 unmodified lines2671 left". Consider this expression now, `... | (.*3 as2672 $times_three | [. + $times_three]) | ...`: here the binding2673 `$times_three` is _not_ visible past the closing parenthesis.2674 2637- - title: Reduce2638- body: |2639-2640- The `reduce` syntax in jq allows you to combine all of the2641- results of an expression by accumulating them into a single2642- answer. As an example, we'll pass `[3,2,1]` to this expression:2643-2644- reduce .[] as $item (0; . + $item)2645-2646- For each result that `.[]` produces, `. + $item` is run to2647- accumulate a running total, starting from 0. In this2648- example, `.[]` produces the results 3, 2, and 1, so the2649- effect is similar to running something like this:2650-2651- 0 | (3 as $item | . + $item) |2652- (2 as $item | . + $item) |2653- (1 as $item | . + $item)2654-2655- examples:2656- - program: 'reduce .[] as $item (0; . + $item)'2657- input: '[10,2,5,3]'2658- output: ['20']2659-2675 - title: "`isempty(exp)`"2676 body: |2677 2678 Returns true if `exp` produces no outputs, false otherwise.2 unmodified lines2681 - program: 'isempty(empty)'2682 input: 'null'2683 output: ['true']2684 2685+ - program: 'isempty(.[])'2686+ input: '[]'2687+ output: ['true']2688+2689+ - program: 'isempty(.[])'2690+ input: '[1,2,3]'2691+ output: ['false']2692+2693 - title: "`limit(n; exp)`"2694 body: |2695 2696 The `limit` function extracts up to `n` outputs from `exp`.31 unmodified lines2728 - program: '[range(.)]|[first, last, nth(5)]'2729 input: '10'2730 output: ['[0,9,5]']2731 2732+ - title: "`reduce`"2733+ body: |2734+2735+ The `reduce` syntax allows you to combine all of the results of2736+ an expression by accumulating them into a single answer.2737+ The form is `reduce EXP as $var (INIT; UPDATE)`.2738+ As an example, we'll pass `[1,2,3]` to this expression:2739+2740+ reduce .[] as $item (0; . + $item)2741+2742+ For each result that `.[]` produces, `. + $item` is run to2743+ accumulate a running total, starting from 0 as the input value.2744+ In this example, `.[]` produces the results `1`, `2`, and `3`,2745+ so the effect is similar to running something like this:2746+2747+ 0 | 1 as $item | . + $item |2748+ 2 as $item | . + $item |2749+ 3 as $item | . + $item2750+2751+ examples:2752+ - program: 'reduce .[] as $item (0; . + $item)'2753+ input: '[1,2,3,4,5]'2754+ output: ['15']2755+2756+ - program: 'reduce .[] as [$i,$j] (0; . + $i * $j)'2757+ input: '[[1,2],[3,4],[5,6]]'2758+ output: ['44']2759+2760+ - program: 'reduce .[] as {$x,$y} (null; .x += $x | .y += [$y])'2761+ input: '[{"x":"a","y":1},{"x":"b","y":2},{"x":"c","y":3}]'2762+ output: ['{"x":"abc","y":[1,2,3]}']2763+2764 - title: "`foreach`"2765 body: |2766 2767 The `foreach` syntax is similar to `reduce`, but intended to2768 allow the construction of `limit` and reducers that produce2714- intermediate results (see example).2769+ intermediate results.2770 2771 The form is `foreach EXP as $var (INIT; UPDATE; EXTRACT)`.2717- Like `reduce`, `INIT` is evaluated once to produce a state2718- value, then each output of `EXP` is bound to `$var`, `UPDATE`2719- is evaluated for each output of `EXP` with the current state2720- and with `$var` visible. Each value output by `UPDATE`2721- replaces the previous state. Finally, `EXTRACT` is evaluated2722- for each new state to extract an output of `foreach`.2772+ As an example, we'll pass `[1,2,3]` to this expression:2773 2724- This is mostly useful only for constructing `reduce`- and2725- `limit`-like functions. But it is much more general, as it2726- allows for partial reductions (see the example below).2774+ foreach .[] as $item (0; . + $item; [$item, . * 2])2775 2776+ Like the `reduce` syntax, `. + $item` is run for each result2777+ that `.[]` produces, but `[$item, . * 2]` is run for each2778+ intermediate values. In this example, since the intermediate2779+ values are `1`, `3`, and `6`, the `foreach` expression produces2780+ `[1,2]`, `[2,6]`, and `[3,12]`. So the effect is similar2781+ to running something like this:2782+2783+ 0 | 1 as $item | . + $item | [$item, . * 2],2784+ 2 as $item | . + $item | [$item, . * 2],2785+ 3 as $item | . + $item | [$item, . * 2]2786+2787+ When `EXTRACT` is omitted, the identity filter is used.2788+ That is, it outputs the intermediate values as they are.2789+2790 examples:2729- - program: '[foreach .[] as $item2730- ([[],[]];2731- if $item == null then [[],.[0]] else [(.[0] + [$item]),[]] end;2732- if $item == null then .[1] else empty end)]'2733- input: '[1,2,3,4,null,"a","b",null]'2734- output: ['[[1,2,3,4],["a","b"]]']2791+ - program: 'foreach .[] as $item (0; . + $item)'2792+ input: '[1,2,3,4,5]'2793+ output: ['1','3','6','10','15']2794 2795+ - program: 'foreach .[] as $item (0; . + $item; [$item, . * 2])'2796+ input: '[1,2,3,4,5]'2797+ output: ['[1,2]','[2,6]','[3,12]','[4,20]','[5,30]']2798+2799+ - program: 'foreach .[] as $item (0; . + 1; {index: ., $item})'2800+ input: '["foo", "bar", "baz"]'2801+ output:2802+ - '{"index":1,"item":"foo"}'2803+ - '{"index":2,"item":"bar"}'2804+ - '{"index":3,"item":"baz"}'2805+2806 - title: Recursion2807 body: |2808 2809 As described above, `recurse` uses recursion, and any jq129 unmodified lines2939 body: |2940 2941 Outputs one new input.2942 2943+ echo 1 2 3 4 | jq '[., input]' # [1,2] [3,4]2944+2945 - title: "`inputs`"2946 body: |2947 2948 Outputs all remaining inputs, one by one.2949 2950 This is primarily useful for reductions over a program's2951 inputs.2952 2953+ echo 1 2 3 | jq -n 'reduce inputs as $i (0; . + $i)' # 62954+2955 - title: "`debug`"2956 body: |2957 2958 Causes a debug message based on the input value to be50 unmodified lines3009 number of path elements from the left of the outputs of the3010 given streaming expression.3011 3012 examples:2939- - program: '[1|truncate_stream([[0],1],[[1,0],2],[[1,0]],[[1]])]'3013+ - program: 'truncate_stream([[0],1],[[1,0],2],[[1,0]],[[1]])'3014 input: '1'2941- output: ['[[[0],2],[[0]]]']3015+ output: ['[[0],2]', '[[0]]']3016 3017 - title: "`fromstream(stream_expression)`"3018 body: |3019 345 unmodified linesmodified:docs/content/manual/manual.yml+140 added-66 removed
77 unmodified lines78 79 jq filters run on a stream of JSON data. The input to jq is80 parsed as a sequence of whitespace-separated JSON values which81 are passed through the provided filter one at a time. The82- output(s) of the filter are written to standard out, again as a83- sequence of whitespace-separated JSON data.82+ output(s) of the filter are written to standard output, as a83+ sequence of newline-separated JSON data.84 85 Note: it is important to mind the shell's quoting rules. As a86 general rule it's best to always quote (with single-quote87 characters) the jq program, as too many characters with special43 unmodified lines131 to reduce large inputs incrementally.132 133 * `--stream-errors`:134 135- Like `--stream`, but invalid JSON inputs yield array calues135+ Like `--stream`, but invalid JSON inputs yield array values136 where the first element is the error and the second is a path.137 For example, `["a",n]` produces ["Invalid literal at line 1,138 column 9",[1]]`.139 193 unmodified lines333 Although the identity filter never modifies the value of its334 input, jq processing can sometimes make it appear as though335 it does. For example, using the current implementation of336 jq, we would see that the expression:337- 337+338 1E1234567890 | .339 340 produces `1.7976931348623157e+308` on at least one platform.341 This is because, in the process of parsing the number, this29 unmodified lines371 input: '"Hello, world!"'372 output: ['"Hello, world!"']373 374 - program: '.'375- input: '0.12345678901234567890123456789'375+ input: '0.12345678901234567890123456789'376 output: ['0.12345678901234567890123456789']377 378 - program: '[., tojson]'379 input: '12345678909876543212345'17 unmodified lines397 The simplest *useful* filter has the form `.foo`. When given a398 JSON object (aka dictionary or hash) as input, `.foo` produces399 the value at the key "foo" if the key is present, or null otherwise.400 401- A filter of the form `.foo.bar` is equivalent to `.foo|.bar`.401+ A filter of the form `.foo.bar` is equivalent to `.foo | .bar`.402 403 The `.foo` syntax only works for simple, identifier-like keys, that404 is, keys that are all made of alphanumeric characters and405 underscore, and which do not start with a digit.77 unmodified lines483 elements from index 10 (inclusive) to index 15 (exclusive).484 Either index may be negative (in which case it counts485 backwards from the end of the array), or omitted (in which486 case it refers to the start or end of the array).487+ Indices are zero-based.488 489 examples:490 - program: '.[2:4]'491 input: '["a","b","c","d","e"]'17 unmodified lines509 If you use the `.[index]` syntax, but omit the index510 entirely, it will return *all* of the elements of an511 array. Running `.[]` with the input `[1,2,3]` will produce the512 numbers as three separate results, rather than as a single512- array.513+ array. A filter of the form `.foo[]` is equivalent to514+ `.foo | .[]`.515 516 You can also use this on an object, and it will return all517 the values of the object.518 7 unmodified lines526 - program: '.[]'527 input: '[]'528 output: []529 530+ - program: '.foo[]'531+ input: '{"foo":[1,2,3]}'532+ output: ['1','2','3']533+534 - program: '.[]'535 input: '{"a": 1, "b": 1}'536 output: ['1', '1']537 538 - title: "`.[]?`"539 body: |540 541 Like `.[]`, but no errors will be output if . is not an array536- or object.542+ or object. A filter of the form `.foo[]?` is equivalent to543+ `.foo | .[]?`.544 545 - title: "Comma: `,`"546 body: |547 186 unmodified lines734 735 Recursively descends `.`, producing every value. This is the736 same as the zero-argument `recurse` builtin (see below). This737 is intended to resemble the XPath `//` operator. Note that731- `..a` does not work; use `..|.a` instead. In the example732- below we use `..|.a?` to find all the values of object keys738+ `..a` does not work; use `.. | .a` instead. In the example739+ below we use `.. | .a?` to find all the values of object keys740 "a" in any object found "below" `.`.741 742 This is particularly useful in conjunction with `path(EXP)`743 (also see below) and the `?` operator.744 745 examples:739- - program: '..|.a?'746+ - program: '.. | .a?'747 input: '[[{"a":1}]]'748 output: ['1']749 750 - title: Builtin operators and functions445 unmodified lines1196 - program: '[1,2,empty,3]'1197 input: 'null'1198 output: ['[1,2,3]']1199 1193- - title: "`error(message)`"1200+ - title: "`error`, `error(message)`"1201 body: |1202 1196- Produces an error, just like `.a` applied to values other than1197- null and objects would, but with the given message as the1198- error's value. Errors can be caught with try/catch; see below.1203+ Produces an error with the input value, or with the message1204+ given as the argument. Errors can be caught with try/catch;1205+ see below.1206 1207+ When the error value is `null`, it produces nothing and works1208+ just like `empty`. So `[null | error]` and `[error(null)]` both1209+ emit `[]`.1210+1211+ examples:1212+ - program: 'try error catch .'1213+ input: '"error message"'1214+ output: ['"error message"']1215+1216+ - program: 'try error("invalid value: \(.)") catch .'1217+ input: '42'1218+ output: ['"invalid value: 42"']1219+1220 - title: "`halt`"1221 body: |1222 1223 Stops the jq program with no further outputs. jq will exit276 unmodified lines1500 compared by comparing their sets of keys (as arrays in1501 sorted order), and if their keys are equal then the values1502 are compared key by key.1503 1484- `sort` may be used to sort by a particular field of an1485- object, or by applying any jq filter.1504+ `sort_by` may be used to sort by a particular field of an1505+ object, or by applying any jq filter. `sort_by(f)` compares1506+ two elements by comparing the result of `f` on each element.1507+ When `f` produces multiple values, it firstly compares the1508+ first values, and the second values if the first values are1509+ equal, and so on.1510 1487- `sort_by(f)` compares two elements by comparing the result of1488- `f` on each element.1489-1511 examples:1512 - program: 'sort'1513 input: '[8,3,null,6]'1514 output: ['[null,3,6,8]']1515+1516 - program: 'sort_by(.foo)'1495- input: '[{"foo":4, "bar":10}, {"foo":3, "bar":100}, {"foo":2, "bar":1}]'1496- output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":100}, {"foo":4, "bar":10}]']1517+ input: '[{"foo":4, "bar":10}, {"foo":3, "bar":10}, {"foo":2, "bar":1}]'1518+ output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":4, "bar":10}]']1519 1520+ - program: 'sort_by(.foo, .bar)'1521+ input: '[{"foo":4, "bar":10}, {"foo":3, "bar":20}, {"foo":2, "bar":1}, {"foo":3, "bar":10}]'1522+ output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":3, "bar":20}, {"foo":4, "bar":10}]']1523+1524 - title: "`group_by(path_expression)`"1525 body: |1526 1527 `group_by(.foo)` takes as input an array, groups the117 unmodified lines1645 examples:1646 - program: 'index(", ")'1647 input: '"a,b, cd, efg, hijk"'1648 output: ['3']1649+ - program: 'index(1)'1650+ input: '[0,1,2,1,3,1,4]'1651+ output: ['1']1652+ - program: 'index([1,2])'1653+ input: '[0,1,2,3,1,4,2,5,1,2,6,7]'1654+ output: ['1']1655 - program: 'rindex(", ")'1656 input: '"a,b, cd, efg, hijk"'1657 output: ['12']1658+ - program: 'rindex(1)'1659+ input: '[0,1,2,1,3,1,4]'1660+ output: ['5']1661+ - program: 'rindex([1,2])'1662+ input: '[0,1,2,3,1,4,2,5,1,2,6,7]'1663+ output: ['8']1664 1665 - title: "`inside`"1666 body: |1667 215 unmodified lines1883 1884 When called without an argument, `recurse` is equivalent to1885 `recurse(.[]?)`.1886 1849- `recurse(f)` is identical to `recurse(f; . != null)` and can be1887+ `recurse(f)` is identical to `recurse(f; true)` and can be1888 used without concerns about recursion depth.1889 1890 `recurse(f; condition)` is a generator which begins by1891 emitting . and then emits in turn .|f, .|f|f, .|f|f|f, ... so long1006 unmodified lines2898 left". Consider this expression now, `... | (.*3 as2899 $times_three | [. + $times_three]) | ...`: here the binding2900 `$times_three` is _not_ visible past the closing parenthesis.2901 2864- - title: Reduce2865- body: |2866-2867- The `reduce` syntax in jq allows you to combine all of the2868- results of an expression by accumulating them into a single2869- answer. As an example, we'll pass `[3,2,1]` to this expression:2870-2871- reduce .[] as $item (0; . + $item)2872-2873- For each result that `.[]` produces, `. + $item` is run to2874- accumulate a running total, starting from 0. In this2875- example, `.[]` produces the results 3, 2, and 1, so the2876- effect is similar to running something like this:2877-2878- 0 | (3 as $item | . + $item) |2879- (2 as $item | . + $item) |2880- (1 as $item | . + $item)2881-2882- examples:2883- - program: 'reduce .[] as $item (0; . + $item)'2884- input: '[10,2,5,3]'2885- output: ['20']2886-2902 - title: "`isempty(exp)`"2903 body: |2904 2905 Returns true if `exp` produces no outputs, false otherwise.2 unmodified lines2908 - program: 'isempty(empty)'2909 input: 'null'2910 output: ['true']2911 2912+ - program: 'isempty(.[])'2913+ input: '[]'2914+ output: ['true']2915+2916+ - program: 'isempty(.[])'2917+ input: '[1,2,3]'2918+ output: ['false']2919+2920 - title: "`limit(n; exp)`"2921 body: |2922 2923 The `limit` function extracts up to `n` outputs from `exp`.29 unmodified lines2953 - program: '[range(.)]|[first, last, nth(5)]'2954 input: '10'2955 output: ['[0,9,5]']2956 2957+ - title: "`reduce`"2958+ body: |2959+2960+ The `reduce` syntax allows you to combine all of the results of2961+ an expression by accumulating them into a single answer.2962+ The form is `reduce EXP as $var (INIT; UPDATE)`.2963+ As an example, we'll pass `[1,2,3]` to this expression:2964+2965+ reduce .[] as $item (0; . + $item)2966+2967+ For each result that `.[]` produces, `. + $item` is run to2968+ accumulate a running total, starting from 0 as the input value.2969+ In this example, `.[]` produces the results `1`, `2`, and `3`,2970+ so the effect is similar to running something like this:2971+2972+ 0 | 1 as $item | . + $item |2973+ 2 as $item | . + $item |2974+ 3 as $item | . + $item2975+2976+ examples:2977+ - program: 'reduce .[] as $item (0; . + $item)'2978+ input: '[1,2,3,4,5]'2979+ output: ['15']2980+2981+ - program: 'reduce .[] as [$i,$j] (0; . + $i * $j)'2982+ input: '[[1,2],[3,4],[5,6]]'2983+ output: ['44']2984+2985+ - program: 'reduce .[] as {$x,$y} (null; .x += $x | .y += [$y])'2986+ input: '[{"x":"a","y":1},{"x":"b","y":2},{"x":"c","y":3}]'2987+ output: ['{"x":"abc","y":[1,2,3]}']2988+2989 - title: "`foreach`"2990 body: |2991 2992 The `foreach` syntax is similar to `reduce`, but intended to2993 allow the construction of `limit` and reducers that produce2939- intermediate results (see example).2994+ intermediate results.2995 2996 The form is `foreach EXP as $var (INIT; UPDATE; EXTRACT)`.2942- Like `reduce`, `INIT` is evaluated once to produce a state2943- value, then each output of `EXP` is bound to `$var`, `UPDATE`2944- is evaluated for each output of `EXP` with the current state2945- and with `$var` visible. Each value output by `UPDATE`2946- replaces the previous state. Finally, `EXTRACT` is evaluated2947- for each new state to extract an output of `foreach`.2997+ As an example, we'll pass `[1,2,3]` to this expression:2998 2949- This is mostly useful only for constructing `reduce`- and2950- `limit`-like functions. But it is much more general, as it2951- allows for partial reductions (see the example below).2999+ foreach .[] as $item (0; . + $item; [$item, . * 2])3000 3001+ Like the `reduce` syntax, `. + $item` is run for each result3002+ that `.[]` produces, but `[$item, . * 2]` is run for each3003+ intermediate values. In this example, since the intermediate3004+ values are `1`, `3`, and `6`, the `foreach` expression produces3005+ `[1,2]`, `[2,6]`, and `[3,12]`. So the effect is similar3006+ to running something like this:3007+3008+ 0 | 1 as $item | . + $item | [$item, . * 2],3009+ 2 as $item | . + $item | [$item, . * 2],3010+ 3 as $item | . + $item | [$item, . * 2]3011+3012+ When `EXTRACT` is omitted, the identity filter is used.3013+ That is, it outputs the intermediate values as they are.3014+3015 examples:2954- - program: '[foreach .[] as $item2955- ([[],[]];2956- if $item == null then [[],.[0]] else [(.[0] + [$item]),[]] end;2957- if $item == null then .[1] else empty end)]'2958- input: '[1,2,3,4,null,"a","b",null]'2959- output: ['[[1,2,3,4],["a","b"]]']3016+ - program: 'foreach .[] as $item (0; . + $item)'3017+ input: '[1,2,3,4,5]'3018+ output: ['1','3','6','10','15']3019 3020+ - program: 'foreach .[] as $item (0; . + $item; [$item, . * 2])'3021+ input: '[1,2,3,4,5]'3022+ output: ['[1,2]','[2,6]','[3,12]','[4,20]','[5,30]']3023+3024+ - program: 'foreach .[] as $item (0; . + 1; {index: ., $item})'3025+ input: '["foo", "bar", "baz"]'3026+ output:3027+ - '{"index":1,"item":"foo"}'3028+ - '{"index":2,"item":"bar"}'3029+ - '{"index":3,"item":"baz"}'3030+3031 - title: Recursion3032 body: |3033 3034 As described above, `recurse` uses recursion, and any jq132 unmodified lines3167 3168 Outputs one new input.3169 3170 Note that when using `input` it is generally be necessary to3101- invoke jq with the -n command-line option, otherwise3171+ invoke jq with the `-n` command-line option, otherwise3172 the first entity will be lost.3173 3174+ echo 1 2 3 4 | jq '[., input]' # [1,2] [3,4]3175+3176 - title: "`inputs`"3177 body: |3178 3179 Outputs all remaining inputs, one by one.3180 3181 This is primarily useful for reductions over a program's3182 inputs. Note that when using `inputs` it is generally necessary3111- to invoke jq with the -n command-line option, otherwise3183+ to invoke jq with the `-n` command-line option, otherwise3184 the first entity will be lost.3185 3186+ echo 1 2 3 | jq -n 'reduce inputs as $i (0; . + $i)' # 63187+3188 - title: "`debug`, `debug(msgs)`"3189 body: |3190 3191 These two filters are like `.` but have as a side-effect the69 unmodified lines3261 number of path elements from the left of the outputs of the3262 given streaming expression.3263 3264 examples:3191- - program: '[1|truncate_stream([[0],1],[[1,0],2],[[1,0]],[[1]])]'3265+ - program: 'truncate_stream([[0],1],[[1,0],2],[[1,0]],[[1]])'3266 input: '1'3193- output: ['[[[0],2],[[0]]]']3267+ output: ['[[0],2]', '[[0]]']3268 3269 - title: "`fromstream(stream_expression)`"3270 body: |3271 357 unmodified linesmodified:tests/jq.test+8 added
1230 unmodified lines1231 [[.[]|[.a,.a]]?]1232 [null,true,{"a":1}]1233 []1234 1235+[1,error,2]1236+null1237+[1,2]1238+1239+[1,error(null),2]1240+01241+[1,2]1242+1243 try error("\($__loc__)") catch .1244 null1245 "{\"file\":\"<top-level>\",\"line\":1}"1246 653 unmodified linesmodified:tests/man.test+81 added-12 removed
84 unmodified lines85 86 .[]87 []88 89+.foo[]90+{"foo":[1,2,3]}91+192+293+394+95 .[]96 {"a": 1, "b": 1}97 198 139 unmodified lines138 {(.user): .titles}139 {"user":"stedolan","titles":["JQ Primer", "More JQ"]}140 {"stedolan": ["JQ Primer", "More JQ"]}141 136-..|.a?142+.. | .a?143 [[{"a":1}]]144 1145 146 .a + 1174 unmodified lines321 [1,2,empty,3]322 null323 [1,2,3]324 325+try error catch .326+"error message"327+"error message"328+329+try error("invalid value: \(.)") catch .330+42331+"invalid value: 42"332+333 try error("\($__loc__)") catch .334 null335 "{\"file\":\"<top-level>\",\"line\":1}"336 119 unmodified lines456 [8,3,null,6]457 [null,3,6,8]458 459 sort_by(.foo)446-[{"foo":4, "bar":10}, {"foo":3, "bar":100}, {"foo":2, "bar":1}]447-[{"foo":2, "bar":1}, {"foo":3, "bar":100}, {"foo":4, "bar":10}]460+[{"foo":4, "bar":10}, {"foo":3, "bar":10}, {"foo":2, "bar":1}]461+[{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":4, "bar":10}]462 463+sort_by(.foo, .bar)464+[{"foo":4, "bar":10}, {"foo":3, "bar":20}, {"foo":2, "bar":1}, {"foo":3, "bar":10}]465+[{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":3, "bar":20}, {"foo":4, "bar":10}]466+467 group_by(.foo)468 [{"foo":1, "bar":10}, {"foo":3, "bar":100}, {"foo":1, "bar":1}]469 [[{"foo":1, "bar":10}, {"foo":1, "bar":1}], [{"foo":3, "bar":100}]]470 56 unmodified lines527 index(", ")528 "a,b, cd, efg, hijk"529 3530 531+index(1)532+[0,1,2,1,3,1,4]533+1534+535+index([1,2])536+[0,1,2,3,1,4,2,5,1,2,6,7]537+1538+539 rindex(", ")540 "a,b, cd, efg, hijk"541 12542 543+rindex(1)544+[0,1,2,1,3,1,4]545+5546+547+rindex([1,2])548+[0,1,2,3,1,4,2,5,1,2,6,7]549+8550+551 inside("foobar")552 "bar"553 true554 277 unmodified lines832 def addvalue(f): f as $x | map(. + $x); addvalue(.[0])833 [[1,2],[10,20]]834 [[1,2,1,2], [10,20,1,2]]835 802-reduce .[] as $item (0; . + $item)803-[10,2,5,3]804-20805-836 isempty(empty)837 null838 true839 840+isempty(.[])841+[]842+true843+844+isempty(.[])845+[1,2,3]846+false847+848 [limit(3;.[])]849 [0,1,2,3,4,5,6,7,8,9]850 [0,1,2]851 4 unmodified lines856 [range(.)]|[first, last, nth(5)]857 10858 [0,9,5]859 822-[foreach .[] as $item ([[],[]]; if $item == null then [[],.[0]] else [(.[0] + [$item]),[]] end; if $item == null then .[1] else empty end)]823-[1,2,3,4,null,"a","b",null]824-[[1,2,3,4],["a","b"]]860+reduce .[] as $item (0; . + $item)861+[1,2,3,4,5]862+15863 864+reduce .[] as [$i,$j] (0; . + $i * $j)865+[[1,2],[3,4],[5,6]]866+44867+868+reduce .[] as {$x,$y} (null; .x += $x | .y += [$y])869+[{"x":"a","y":1},{"x":"b","y":2},{"x":"c","y":3}]870+{"x":"abc","y":[1,2,3]}871+872+foreach .[] as $item (0; . + $item)873+[1,2,3,4,5]874+1875+3876+6877+10878+15879+880+foreach .[] as $item (0; . + $item; [$item, . * 2])881+[1,2,3,4,5]882+[1,2]883+[2,6]884+[3,12]885+[4,20]886+[5,30]887+888+foreach .[] as $item (0; . + 1; {index: ., $item})889+["foo", "bar", "baz"]890+{"index":1,"item":"foo"}891+{"index":2,"item":"bar"}892+{"index":3,"item":"baz"}893+894 def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else . end; if by == 0 then init else init|_range end | select((by > 0 and . < upto) or (by < 0 and . > upto)); range(0; 10; 3)895 null896 0897 33 unmodified lines901 def while(cond; update): def _while: if cond then ., (update | _while) else empty end; _while; [while(.<100; .*2)]902 1903 [1,2,4,8,16,32,64]904 837-[1|truncate_stream([[0],1],[[1,0],2],[[1,0]],[[1]])]905+truncate_stream([[0],1],[[1,0],2],[[1,0]],[[1]])906 1839-[[[0],2],[[0]]]907+[[0],2]908+[[0]]909 910 fromstream(1|truncate_stream([[0],1],[[1,0],2],[[1,0]],[[1]]))911 null912 [2]31 unmodified linesmodified:jq.1.prebuilt+168 added-59 removed
30 unmodified lines31 .P32 But that\'s getting ahead of ourselves\. :) Let\'s start with something simpler:33 .34 .SH "INVOKING JQ"35-jq filters run on a stream of JSON data\. The input to jq is parsed as a sequence of whitespace\-separated JSON values which are passed through the provided filter one at a time\. The output(s) of the filter are written to standard out, again as a sequence of whitespace\-separated JSON data\.35+jq filters run on a stream of JSON data\. The input to jq is parsed as a sequence of whitespace\-separated JSON values which are passed through the provided filter one at a time\. The output(s) of the filter are written to standard output, as a sequence of newline\-separated JSON data\.36 .37 .P38 Note: it is important to mind the shell\'s quoting rules\. As a general rule it\'s best to always quote (with single\-quote characters) the jq program, as too many characters with special meaning to jq are also shell meta\-characters\. For example, \fBjq "foo"\fR will fail on most Unix shells because that will be the same as \fBjq foo\fR, which will generally fail because \fBfoo is not defined\fR\. When using the Windows command shell (cmd\.exe) it\'s best to use double quotes around your jq program when given on the command\-line (instead of the \fB\-f program\-file\fR option), but then double\-quotes in the jq program need backslash escaping\. When using the Powershell (\fBpowershell\.exe\fR) or the Powershell Core (\fBpwsh\fR/\fBpwsh\.exe\fR), use single\-quote characters around the jq program and backslash\-escaped double\-quotes (\fB\e"\fR) inside the jq program\.39 .35 unmodified lines75 .TP76 \fB\-\-stream\-errors\fR:77 .78 .IP79-Like \fB\-\-stream\fR, but invalid JSON inputs yield array calues where the first element is the error and the second is a path\. For example, \fB["a",n]\fR produces ["Invalid literal at line 1, column 9",[1]]`\.79+Like \fB\-\-stream\fR, but invalid JSON inputs yield array values where the first element is the error and the second is a path\. For example, \fB["a",n]\fR produces ["Invalid literal at line 1, column 9",[1]]`\.80 .81 .IP82 Implies \fB\-\-stream\fR\. Invalid JSON inputs produce no error values when \fB\-\-stream\fR without \fB\-\-stream\-errors\fR\.83 .242 unmodified lines326 .SS "Object Identifier\-Index: \.foo, \.foo\.bar"327 The simplest \fIuseful\fR filter has the form \fB\.foo\fR\. When given a JSON object (aka dictionary or hash) as input, \fB\.foo\fR produces the value at the key "foo" if the key is present, or null otherwise\.328 .329 .P330-A filter of the form \fB\.foo\.bar\fR is equivalent to \fB\.foo|\.bar\fR\.330+A filter of the form \fB\.foo\.bar\fR is equivalent to \fB\.foo | \.bar\fR\.331 .332 .P333 The \fB\.foo\fR syntax only works for simple, identifier\-like keys, that is, keys that are all made of alphanumeric characters and underscore, and which do not start with a digit\.334 .79 unmodified lines414 .415 .IP "" 0416 .417 .SS "Array/String Slice: \.[<number>:<number>]"418-The \fB\.[<number>:<number>]\fR syntax can be used to return a subarray of an array or substring of a string\. The array returned by \fB\.[10:15]\fR will be of length 5, containing the elements from index 10 (inclusive) to index 15 (exclusive)\. Either index may be negative (in which case it counts backwards from the end of the array), or omitted (in which case it refers to the start or end of the array)\.418+The \fB\.[<number>:<number>]\fR syntax can be used to return a subarray of an array or substring of a string\. The array returned by \fB\.[10:15]\fR will be of length 5, containing the elements from index 10 (inclusive) to index 15 (exclusive)\. Either index may be negative (in which case it counts backwards from the end of the array), or omitted (in which case it refers to the start or end of the array)\. Indices are zero\-based\.419 .420 .IP "" 4421 .422 .nf18 unmodified lines441 .442 .IP "" 0443 .444 .SS "Array/Object Value Iterator: \.[]"445-If you use the \fB\.[index]\fR syntax, but omit the index entirely, it will return \fIall\fR of the elements of an array\. Running \fB\.[]\fR with the input \fB[1,2,3]\fR will produce the numbers as three separate results, rather than as a single array\.445+If you use the \fB\.[index]\fR syntax, but omit the index entirely, it will return \fIall\fR of the elements of an array\. Running \fB\.[]\fR with the input \fB[1,2,3]\fR will produce the numbers as three separate results, rather than as a single array\. A filter of the form \fB\.foo[]\fR is equivalent to \fB\.foo | \.[]\fR\.446 .447 .P448 You can also use this on an object, and it will return all the values of the object\.449 .8 unmodified lines458 jq \'\.[]\'459 []460 => 461 462+jq \'\.foo[]\'463+ {"foo":[1,2,3]}464+=> 1, 2, 3465+466 jq \'\.[]\'467 {"a": 1, "b": 1}468 => 1, 1469 .1 unmodified line471 .472 .IP "" 0473 .474 .SS "\.[]?"471-Like \fB\.[]\fR, but no errors will be output if \. is not an array or object\.475+Like \fB\.[]\fR, but no errors will be output if \. is not an array or object\. A filter of the form \fB\.foo[]?\fR is equivalent to \fB\.foo | \.[]?\fR\.476 .477 .SS "Comma: ,"478 If two filters are separated by a comma, then the same input will be fed into both and the two filters\' output value streams will be concatenated in order: first, all of the outputs produced by the left expression, and then all of the outputs produced by the right\. For instance, filter \fB\.foo, \.bar\fR, produces both the "foo" fields and "bar" fields as separate outputs\.479 .228 unmodified lines708 .709 .IP "" 0710 .711 .SS "Recursive Descent: \.\."708-Recursively descends \fB\.\fR, producing every value\. This is the same as the zero\-argument \fBrecurse\fR builtin (see below)\. This is intended to resemble the XPath \fB//\fR operator\. Note that \fB\.\.a\fR does not work; use \fB\.\.|\.a\fR instead\. In the example below we use \fB\.\.|\.a?\fR to find all the values of object keys "a" in any object found "below" \fB\.\fR\.712+Recursively descends \fB\.\fR, producing every value\. This is the same as the zero\-argument \fBrecurse\fR builtin (see below)\. This is intended to resemble the XPath \fB//\fR operator\. Note that \fB\.\.a\fR does not work; use \fB\.\. | \.a\fR instead\. In the example below we use \fB\.\. | \.a?\fR to find all the values of object keys "a" in any object found "below" \fB\.\fR\.713 .714 .P715 This is particularly useful in conjunction with \fBpath(EXP)\fR (also see below) and the \fB?\fR operator\.716 .717 .IP "" 4718 .719 .nf720 717-jq \'\.\.|\.a?\'721+jq \'\.\. | \.a?\'722 [[{"a":1}]]723 => 1724 .725 .fi503 unmodified lines1229 .fi1230 .1231 .IP "" 01232 .1229-.SS "error(message)"1230-Produces an error, just like \fB\.a\fR applied to values other than null and objects would, but with the given message as the error\'s value\. Errors can be caught with try/catch; see below\.1233+.SS "error, error(message)"1234+Produces an error with the input value, or with the message given as the argument\. Errors can be caught with try/catch; see below\.1235 .1236+.P1237+When the error value is \fBnull\fR, it produces nothing and works just like \fBempty\fR\. So \fB[null | error]\fR and \fB[error(null)]\fR both emit \fB[]\fR\.1238+.1239+.IP "" 41240+.1241+.nf1242+1243+jq \'try error catch \.\'1244+ "error message"1245+=> "error message"1246+1247+jq \'try error("invalid value: \e(\.)") catch \.\'1248+ 421249+=> "invalid value: 42"1250+.1251+.fi1252+.1253+.IP "" 01254+.1255 .SS "halt"1256 Stops the jq program with no further outputs\. jq will exit with exit status \fB0\fR\.1257 .1258 .SS "halt_error, halt_error(exit_code)"335 unmodified lines1594 .P1595 The ordering for objects is a little complex: first they\'re compared by comparing their sets of keys (as arrays in sorted order), and if their keys are equal then the values are compared key by key\.1596 .1597 .P1575-\fBsort\fR may be used to sort by a particular field of an object, or by applying any jq filter\.1598+\fBsort_by\fR may be used to sort by a particular field of an object, or by applying any jq filter\. \fBsort_by(f)\fR compares two elements by comparing the result of \fBf\fR on each element\. When \fBf\fR produces multiple values, it firstly compares the first values, and the second values if the first values are equal, and so on\.1599 .1577-.P1578-\fBsort_by(f)\fR compares two elements by comparing the result of \fBf\fR on each element\.1579-.1600 .IP "" 41601 .1602 .nf1603 1 unmodified line1605 [8,3,null,6]1606 => [null,3,6,8]1607 1608 jq \'sort_by(\.foo)\'1589- [{"foo":4, "bar":10}, {"foo":3, "bar":100}, {"foo":2, "bar":1}]1590-=> [{"foo":2, "bar":1}, {"foo":3, "bar":100}, {"foo":4, "bar":10}]1609+ [{"foo":4, "bar":10}, {"foo":3, "bar":10}, {"foo":2, "bar":1}]1610+=> [{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":4, "bar":10}]1611+1612+jq \'sort_by(\.foo, \.bar)\'1613+ [{"foo":4, "bar":10}, {"foo":3, "bar":20}, {"foo":2, "bar":1}, {"foo":3, "bar":10}]1614+=> [{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":3, "bar":20}, {"foo":4, "bar":10}]1615 .1616 .fi1617 .1618 .IP "" 0143 unmodified lines1762 jq \'index(", ")\'1763 "a,b, cd, efg, hijk"1764 => 31765 1766+jq \'index(1)\'1767+ [0,1,2,1,3,1,4]1768+=> 11769+1770+jq \'index([1,2])\'1771+ [0,1,2,3,1,4,2,5,1,2,6,7]1772+=> 11773+1774 jq \'rindex(", ")\'1775 "a,b, cd, efg, hijk"1776 => 121777+1778+jq \'rindex(1)\'1779+ [0,1,2,1,3,1,4]1780+=> 51781+1782+jq \'rindex([1,2])\'1783+ [0,1,2,3,1,4,2,5,1,2,6,7]1784+=> 81785 .1786 .fi1787 .1788 .IP "" 0282 unmodified lines2071 .P2072 When called without an argument, \fBrecurse\fR is equivalent to \fBrecurse(\.[]?)\fR\.2073 .2074 .P2035-\fBrecurse(f)\fR is identical to \fBrecurse(f; \. != null)\fR and can be used without concerns about recursion depth\.2075+\fBrecurse(f)\fR is identical to \fBrecurse(f; true)\fR and can be used without concerns about recursion depth\.2076 .2077 .P2078 \fBrecurse(f; condition)\fR is a generator which begins by emitting \. and then emits in turn \.|f, \.|f|f, \.|f|f|f, \.\.\. so long as the computed value satisfies the condition\. For example, to generate all the integers, at least in principle, one could write \fBrecurse(\.+1; true)\fR\.2079 .1223 unmodified lines3303 .3304 .P3305 For example, in the following expression there is a binding which is visible "to the right" of it, \fB\.\.\. | \.*3 as $times_three | [\. + $times_three] | \.\.\.\fR, but not "to the left"\. Consider this expression now, \fB\.\.\. | (\.*3 as $times_three | [\. + $times_three]) | \.\.\.\fR: here the binding \fB$times_three\fR is \fInot\fR visible past the closing parenthesis\.3306 .3267-.SS "Reduce"3268-The \fBreduce\fR syntax in jq allows you to combine all of the results of an expression by accumulating them into a single answer\. As an example, we\'ll pass \fB[3,2,1]\fR to this expression:3269-.3270-.IP "" 43271-.3272-.nf3273-3274-reduce \.[] as $item (0; \. + $item)3275-.3276-.fi3277-.3278-.IP "" 03279-.3280-.P3281-For each result that \fB\.[]\fR produces, \fB\. + $item\fR is run to accumulate a running total, starting from 0\. In this example, \fB\.[]\fR produces the results 3, 2, and 1, so the effect is similar to running something like this:3282-.3283-.IP "" 43284-.3285-.nf3286-3287-0 | (3 as $item | \. + $item) |3288- (2 as $item | \. + $item) |3289- (1 as $item | \. + $item)3290-3291-jq \'reduce \.[] as $item (0; \. + $item)\'3292- [10,2,5,3]3293-=> 203294-.3295-.fi3296-.3297-.IP "" 03298-.3307 .SS "isempty(exp)"3308 Returns true if \fBexp\fR produces no outputs, false otherwise\.3309 .3310 .IP "" 42 unmodified lines3313 3314 jq \'isempty(empty)\'3315 null3316 => true3317+3318+jq \'isempty(\.[])\'3319+ []3320+=> true3321+3322+jq \'isempty(\.[])\'3323+ [1,2,3]3324+=> false3325 .3326 .fi3327 .3328 .IP "" 048 unmodified lines3377 .fi3378 .3379 .IP "" 03380 .3381+.SS "reduce"3382+The \fBreduce\fR syntax allows you to combine all of the results of an expression by accumulating them into a single answer\. The form is \fBreduce EXP as $var (INIT; UPDATE)\fR\. As an example, we\'ll pass \fB[1,2,3]\fR to this expression:3383+.3384+.IP "" 43385+.3386+.nf3387+3388+reduce \.[] as $item (0; \. + $item)3389+.3390+.fi3391+.3392+.IP "" 03393+.3394+.P3395+For each result that \fB\.[]\fR produces, \fB\. + $item\fR is run to accumulate a running total, starting from 0 as the input value\. In this example, \fB\.[]\fR produces the results \fB1\fR, \fB2\fR, and \fB3\fR, so the effect is similar to running something like this:3396+.3397+.IP "" 43398+.3399+.nf3400+3401+0 | 1 as $item | \. + $item |3402+ 2 as $item | \. + $item |3403+ 3 as $item | \. + $item3404+3405+jq \'reduce \.[] as $item (0; \. + $item)\'3406+ [1,2,3,4,5]3407+=> 153408+3409+jq \'reduce \.[] as [$i,$j] (0; \. + $i * $j)\'3410+ [[1,2],[3,4],[5,6]]3411+=> 443412+3413+jq \'reduce \.[] as {$x,$y} (null; \.x += $x | \.y += [$y])\'3414+ [{"x":"a","y":1},{"x":"b","y":2},{"x":"c","y":3}]3415+=> {"x":"abc","y":[1,2,3]}3416+.3417+.fi3418+.3419+.IP "" 03420+.3421 .SS "foreach"3366-The \fBforeach\fR syntax is similar to \fBreduce\fR, but intended to allow the construction of \fBlimit\fR and reducers that produce intermediate results (see example)\.3422+The \fBforeach\fR syntax is similar to \fBreduce\fR, but intended to allow the construction of \fBlimit\fR and reducers that produce intermediate results\.3423 .3424 .P3369-The form is \fBforeach EXP as $var (INIT; UPDATE; EXTRACT)\fR\. Like \fBreduce\fR, \fBINIT\fR is evaluated once to produce a state value, then each output of \fBEXP\fR is bound to \fB$var\fR, \fBUPDATE\fR is evaluated for each output of \fBEXP\fR with the current state and with \fB$var\fR visible\. Each value output by \fBUPDATE\fR replaces the previous state\. Finally, \fBEXTRACT\fR is evaluated for each new state to extract an output of \fBforeach\fR\.3425+The form is \fBforeach EXP as $var (INIT; UPDATE; EXTRACT)\fR\. As an example, we\'ll pass \fB[1,2,3]\fR to this expression:3426 .3427+.IP "" 43428+.3429+.nf3430+3431+foreach \.[] as $item (0; \. + $item; [$item, \. * 2])3432+.3433+.fi3434+.3435+.IP "" 03436+.3437 .P3372-This is mostly useful only for constructing \fBreduce\fR\- and \fBlimit\fR\-like functions\. But it is much more general, as it allows for partial reductions (see the example below)\.3438+Like the \fBreduce\fR syntax, \fB\. + $item\fR is run for each result that \fB\.[]\fR produces, but \fB[$item, \. * 2]\fR is run for each intermediate values\. In this example, since the intermediate values are \fB1\fR, \fB3\fR, and \fB6\fR, the \fBforeach\fR expression produces \fB[1,2]\fR, \fB[2,6]\fR, and \fB[3,12]\fR\. So the effect is similar to running something like this:3439 .3440 .IP "" 43441 .3442 .nf3443 3378-jq \'[foreach \.[] as $item ([[],[]]; if $item == null then [[],\.[0]] else [(\.[0] + [$item]),[]] end; if $item == null then \.[1] else empty end)]\'3379- [1,2,3,4,null,"a","b",null]3380-=> [[1,2,3,4],["a","b"]]3444+0 | 1 as $item | \. + $item | [$item, \. * 2],3445+ 2 as $item | \. + $item | [$item, \. * 2],3446+ 3 as $item | \. + $item | [$item, \. * 2]3447 .3448 .fi3449 .3450 .IP "" 03451 .3452+.P3453+When \fBEXTRACT\fR is omitted, the identity filter is used\. That is, it outputs the intermediate values as they are\.3454+.3455+.IP "" 43456+.3457+.nf3458+3459+jq \'foreach \.[] as $item (0; \. + $item)\'3460+ [1,2,3,4,5]3461+=> 1, 3, 6, 10, 153462+3463+jq \'foreach \.[] as $item (0; \. + $item; [$item, \. * 2])\'3464+ [1,2,3,4,5]3465+=> [1,2], [2,6], [3,12], [4,20], [5,30]3466+3467+jq \'foreach \.[] as $item (0; \. + 1; {index: \., $item})\'3468+ ["foo", "bar", "baz"]3469+=> {"index":1,"item":"foo"}, {"index":2,"item":"bar"}, {"index":3,"item":"baz"}3470+.3471+.fi3472+.3473+.IP "" 03474+.3475 .SS "Recursion"3476 As described above, \fBrecurse\fR uses recursion, and any jq function can be recursive\. The \fBwhile\fR builtin is also implemented in terms of recursion\.3477 .3478 .P83 unmodified lines3562 .SS "input"3563 Outputs one new input\.3564 .3565 .P3477-Note that when using \fBinput\fR it is generally be necessary to invoke jq with the \-n command\-line option, otherwise the first entity will be lost\.3566+Note that when using \fBinput\fR it is generally be necessary to invoke jq with the \fB\-n\fR command\-line option, otherwise the first entity will be lost\.3567 .3568+.IP "" 43569+.3570+.nf3571+3572+echo 1 2 3 4 | jq \'[\., input]\' # [1,2] [3,4]3573+.3574+.fi3575+.3576+.IP "" 03577+.3578 .SS "inputs"3579 Outputs all remaining inputs, one by one\.3580 .3581 .P3483-This is primarily useful for reductions over a program\'s inputs\. Note that when using \fBinputs\fR it is generally necessary to invoke jq with the \-n command\-line option, otherwise the first entity will be lost\.3582+This is primarily useful for reductions over a program\'s inputs\. Note that when using \fBinputs\fR it is generally necessary to invoke jq with the \fB\-n\fR command\-line option, otherwise the first entity will be lost\.3583 .3584+.IP "" 43585+.3586+.nf3587+3588+echo 1 2 3 | jq \-n \'reduce inputs as $i (0; \. + $i)\' # 63589+.3590+.fi3591+.3592+.IP "" 03593+.3594 .SS "debug, debug(msgs)"3595 These two filters are like \fB\.\fR but have as a side\-effect the production of one or more messages on stderr\.3596 .3597 .P72 unmodified lines3670 .IP "" 43671 .3672 .nf3673 3565-jq \'[1|truncate_stream([[0],1],[[1,0],2],[[1,0]],[[1]])]\'3674+jq \'truncate_stream([[0],1],[[1,0],2],[[1,0]],[[1]])\'3675 13567-=> [[[0],2],[[0]]]3676+=> [[0],2], [[0]]3677 .3678 .fi3679 .3680 .IP "" 0392 unmodified lines