Skip to content
Myr

manual.yml

This file

Path

docs/content/manual/dev/manual.yml

3,912 lines · 147.0 KiB · YAML

Last changed by

docs: add simple and nested update-assignment examples (#3664)Hugohong258before adoption611c575

History of this file

Take it

The walk stopped after 200 commits: lines older than fa6a2ff point at it.

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1–263why, one step back
1---
2headline: jq Manual (development version)34body: |56 A jq program is a "filter": it takes an input, and produces an7 output. There are a lot of builtin filters for extracting a8 particular field of an object, or converting a number to a string,9 or various other standard tasks.1011 Filters can be combined in various ways - you can pipe the output of12 one filter into another filter, or collect the output of a filter13 into an array.1415 Some filters produce multiple results, for instance there's one that16 produces all the elements of its input array. Piping that filter17 into a second runs the second filter for each element of the18 array. Generally, things that would be done with loops and iteration19 in other languages are just done by gluing filters together in jq.2021 It's important to remember that every filter has an input and an22 output. Even literals like "hello" or 42 are filters - they take an23 input but always produce the same literal as output. Operations that24 combine two filters, like addition, generally feed the same input to25 both and combine the results. So, you can implement an averaging26 filter as `add / length` - feeding the input array both to the `add`27 filter and the `length` filter and then performing the division.2829 But that's getting ahead of ourselves. :) Let's start with something30 simpler:3132manpage_intro: |33 jq(1) -- Command-line JSON processor34 ====================================3536 ## SYNOPSIS3738 `jq` [<options>...] <filter> [<files>...]3940 `jq` can transform JSON in various ways, by selecting, iterating,41 reducing and otherwise mangling JSON documents. For instance,42 running the command `jq 'map(.price) | add'` will take an array of43 JSON objects as input and return the sum of their "price" fields.4445 `jq` can accept text input as well, but by default, `jq` reads a46 stream of JSON entities (including numbers and other literals) from47 `stdin`. Whitespace is only needed to separate entities such as 148 and 2, and true and false. One or more <files> may be specified, in49 which case `jq` will read input from those instead.5051 The <options> are described in the [INVOKING JQ] section; they52 mostly concern input and output formatting. The <filter> is written53 in the jq language and specifies how to transform the input54 file or document.5556 ## FILTERS5758manpage_epilogue: |59 ## BUGS6061 Presumably. Report them or discuss them at:6263 https://github.com/jqlang/jq/issues6465 ## AUTHOR6667 Stephen Dolan `<mu@netsoc.tcd.ie>`6869sections:70 - title: Invoking jq71 body: |7273 jq filters run on a stream of JSON data. The input to jq is74 parsed as a sequence of whitespace-separated JSON values which75 are passed through the provided filter one at a time. The76 output(s) of the filter are written to standard output, as a77 sequence of newline-separated JSON data.7879 The simplest and most common filter (or jq program) is `.`,80 which is the identity operator, copying the inputs of the jq81 processor to the output stream. Because the default behavior of82 the jq processor is to read JSON texts from the input stream,83 and to pretty-print outputs, the `.` program's main use is to84 validate and pretty-print the inputs. The jq programming85 language is quite rich and allows for much more than just86 validation and pretty-printing.8788 Note: it is important to mind the shell's quoting rules. As a89 general rule it's best to always quote (with single-quote90 characters on Unix shells) the jq program, as too many characters with special91 meaning to jq are also shell meta-characters. For example, `jq92 "foo"` will fail on most Unix shells because that will be the same93 as `jq foo`, which will generally fail because `foo is not94 defined`. When using the Windows command shell (cmd.exe) it's95 best to use double quotes around your jq program when given on the96 command-line (instead of the `-f program-file` option), but then97 double-quotes in the jq program need backslash escaping. When using98 the Powershell (`powershell.exe`) or the Powershell Core99 (`pwsh`/`pwsh.exe`), use single-quote characters around the jq100 program and backslash-escaped double-quotes (`\"`) inside the jq101 program.102103 * Unix shells: `jq '.["foo"]'`104 * Powershell: `jq '.[\"foo\"]'`105 * Windows command shell: `jq ".[\"foo\"]"`106107 Note: jq allows user-defined functions, but every jq program108 must have a top-level expression.109110 You can affect how jq reads and writes its input and output111 using some command-line options:112113 * `--null-input` / `-n`:114115 Don't read any input at all. Instead, the filter is run once116 using `null` as the input. This is useful when using jq as a117 simple calculator or to construct JSON data from scratch.118119 * `--raw-input` / `-R`:120121 Don't parse the input as JSON. Instead, each line of text is122 passed to the filter as a string. If combined with `--slurp`,123 then the entire input is passed to the filter as a single long124 string.125126 * `--slurp` / `-s`:127128 Instead of running the filter for each JSON object in the129 input, read the entire input stream into a large array and run130 the filter just once.131132 * `--compact-output` / `-c`:133134 By default, jq pretty-prints JSON output. Using this option135 will result in more compact output by instead putting each136 JSON object on a single line.137138 * `--raw-output` / `-r`:139140 With this option, if the filter's result is a string then it141 will be written directly to standard output rather than being142 formatted as a JSON string with quotes. This can be useful for143 making jq filters talk to non-JSON-based systems.144145 * `--raw-output0`:146147 Like `-r` but jq will print NUL instead of newline after each output.148 This can be useful when the values being output can contain newlines.149 When the output value contains NUL, jq exits with non-zero code.150151 * `--join-output` / `-j`:152153 Like `-r` but jq won't print a newline after each output.154155 * `--ascii-output` / `-a`:156157 jq usually outputs non-ASCII Unicode codepoints as UTF-8, even158 if the input specified them as escape sequences (like159 "\u03bc"). Using this option, you can force jq to produce pure160 ASCII output with every non-ASCII character replaced with the161 equivalent escape sequence.162163 * `--sort-keys` / `-S`:164165 Output the fields of each object with the keys in sorted order.166167 * `--color-output` / `-C` and `--monochrome-output` / `-M`:168169 By default, jq outputs colored JSON if writing to a170 terminal. You can force it to produce color even if writing to171 a pipe or a file using `-C`, and disable color with `-M`.172 When the `NO_COLOR` environment variable is not empty, jq disables173 colored output by default, but you can enable it by `-C`.174175 Colors can be configured with the `JQ_COLORS` environment176 variable (see below).177178 * `--tab`:179180 Use a tab for each indentation level instead of two spaces.181182 * `--indent n`:183184 Use the given number of spaces (no more than 7) for indentation.185186 * `--unbuffered`:187188 Flush the output after each JSON object is printed (useful if189 you're piping a slow data source into jq and piping jq's190 output elsewhere).191192 * `--stream`:193194 Parse the input in streaming fashion, outputting arrays of path195 and leaf values (scalars and empty arrays or empty objects).196 For example, `"a"` becomes `[[],"a"]`, and `[[],"a",["b"]]`197 becomes `[[0],[]]`, `[[1],"a"]`, and `[[2,0],"b"]`.198199 This is useful for processing very large inputs. Use this in200 conjunction with filtering and the `reduce` and `foreach` syntax201 to reduce large inputs incrementally.202203 * `--stream-errors`:204205 Like `--stream`, but invalid JSON inputs yield array values206 where the first element is the error and the second is a path.207 For example, `["a",n]` produces `["Invalid literal at line 1,208 column 7",[1]]`.209210 Implies `--stream`. Invalid JSON inputs produce no error values211 when `--stream` without `--stream-errors`.212213 * `--seq`:214215 Use the `application/json-seq` MIME type scheme for separating216 JSON texts in jq's input and output. This means that an ASCII217 RS (record separator) character is printed before each value on218 output and an ASCII LF (line feed) is printed after every219 output. Input JSON texts that fail to parse are ignored (but220 warned about), discarding all subsequent input until the next221 RS. This mode also parses the output of jq without the `--seq`222 option.223224 * `-f` / `--from-file`:225226 Read the filter from a file rather than from a command line,227 like awk's -f option. This changes the filter argument to be228 interpreted as a filename, instead of the source of a program.229230 * `-L directory` / `--library-path directory`:231232 Prepend `directory` to the search list for modules. If this233 option is used then no builtin search list is used. See the234 section on modules below.235236 * `--arg name value`:237238 This option passes a value to the jq program as a predefined239 variable. If you run jq with `--arg foo bar`, then `$foo` is240 available in the program and has the value `"bar"`. Note that241 `value` will be treated as a string, so `--arg foo 123` will242 bind `$foo` to `"123"`.243244 Named arguments are also available to the jq program as245 `$ARGS.named`. When the name is not a valid identifier, this is246 the only way to access it.247248 * `--argjson name JSON-text`:249250 This option passes a JSON-encoded value to the jq program as a251 predefined variable. If you run jq with `--argjson foo 123`, then252 `$foo` is available in the program and has the value `123`.253254 * `--slurpfile variable-name filename`:255256 This option reads all the JSON texts in the named file and binds257 an array of the parsed JSON values to the given global variable.258 If you run jq with `--slurpfile foo bar`, then `$foo` is available259 in the program and has an array whose elements correspond to the260 texts in the file named `bar`.261262 * `--rawfile variable-name filename`:263

Add missing word in manual for rawfile (#3434)Jan-Piet Mensbefore adoption4829059

line 264why, one step back
264 This option reads in the named file and binds its content to the given

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

line 265why, one step back
265 global variable. If you run jq with `--rawfile foo bar`, then `$foo` is

Add missing word in manual for rawfile (#3434)Jan-Piet Mensbefore adoption4829059

line 266why, one step back
266 available in the program and has a string whose content is set to the text

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 267–890why, one step back
267 in the file named `bar`.
268269 * `--args`:270271 Remaining arguments are positional string arguments. These are272 available to the jq program as `$ARGS.positional[]`.273274 * `--jsonargs`:275276 Remaining arguments are positional JSON text arguments. These277 are available to the jq program as `$ARGS.positional[]`.278279 * `--exit-status` / `-e`:280281 Sets the exit status of jq to 0 if the last output value was282 neither `false` nor `null`, 1 if the last output value was283 either `false` or `null`, or 4 if no valid result was ever284 produced. Normally jq exits with 2 if there was any usage285 problem or system error, 3 if there was a jq program compile286 error, or 0 if the jq program ran.287288 Another way to set the exit status is with the `halt_error`289 builtin function.290291 * `--binary` / `-b`:292293 Windows users using WSL, MSYS2, or Cygwin, should use this option294 when using a native jq.exe, otherwise jq will turn newlines (LFs)295 into carriage-return-then-newline (CRLF).296297 * `--version` / `-V`:298299 Output the jq version and exit with zero.300301 * `--build-configuration`:302303 Output the build configuration of jq and exit with zero.304 This output has no supported format or structure and may change305 without notice in future releases.306307 * `--help` / `-h`:308309 Output the jq help and exit with zero.310311 * `--`:312313 Terminates argument processing. Remaining arguments are not314 interpreted as options.315316 * `--run-tests [filename]`:317318 Runs the tests in the given file or standard input. This must319 be the last option given and does not honor all preceding320 options. The input consists of comment lines, empty lines, and321 program lines followed by one input line, as many lines of322 output as are expected (one per output), and a terminating empty323 line. Compilation failure tests start with a line containing324 only `%%FAIL`, then a line containing the program to compile,325 then a line containing an error message to compare to the326 actual.327328 Be warned that this option can change backwards-incompatibly.329330 - title: Basic filters331 entries:332 - title: "Identity: `.`"333 body: |334335 The absolute simplest filter is `.` . This filter takes its336 input and produces the same value as output. That is, this337 is the identity operator.338339 Since jq by default pretty-prints all output, a trivial340 program consisting of nothing but `.` can be used to format341 JSON output from, say, `curl`.342343 Although the identity filter never modifies the value of its344 input, jq processing can sometimes make it appear as though345 it does. For example, using the current implementation of346 jq, we would see that the expression:347348 1E1234567890 | .349350 produces `1.7976931348623157e+308` on at least one platform.351 This is because, in the process of parsing the number, this352 particular version of jq has converted it to an IEEE754353 double-precision representation, losing precision.354355 The way in which jq handles numbers has changed over time356 and further changes are likely within the parameters set by357 the relevant JSON standards. Moreover, build configuration358 options can alter how jq processes numbers.359360 The following remarks are therefore offered with the361 understanding that they are intended to be descriptive of the362 current version of jq and should not be interpreted as being363 prescriptive:364365 (1) Any arithmetic operation on a number that has not366 already been converted to an IEEE754 double precision367 representation will trigger a conversion to the IEEE754368 representation.369370 (2) jq will attempt to maintain the original decimal371 precision of number literals (if the `--disable-decnum`372 build configuration option was not used), but in expressions373 such `1E1234567890`, precision will be lost if the exponent374 is too large.375376 (3) Comparisons are carried out using the untruncated377 big decimal representation of numbers if available, as378 illustrated in one of the following examples.379380 The examples below use the builtin function `have_decnum` in381 order to demonstrate the expected effects of using / not382 using the `--disable-decnum` build configuration option, and383 also to allow automated tests derived from these examples to384 pass regardless of whether that option is used.385386 examples:387 - program: '.'388 input: '"Hello, world!"'389 output: ['"Hello, world!"']390391 - program: '.'392 input: '0.12345678901234567890123456789'393 output: ['0.12345678901234567890123456789']394395 - program: '[., tojson] == if have_decnum then [12345678909876543212345,"12345678909876543212345"] else [12345678909876543000000,"12345678909876543000000"] end'396 input: '12345678909876543212345'397 output: ['true']398399 - program: '[1234567890987654321,-1234567890987654321 | tojson] == if have_decnum then ["1234567890987654321","-1234567890987654321"] else ["1234567890987654400","-1234567890987654400"] end'400 input: 'null'401 output: ['true']402403 - program: '. < 0.12345678901234567890123456788'404 input: '0.12345678901234567890123456789'405 output: ['false']406407 - program: 'map([., . == 1]) | tojson == if have_decnum then "[[1,true],[1.000,true],[1.0,true],[1.00,true]]" else "[[1,true],[1,true],[1,true],[1,true]]" end'408 input: '[1, 1.000, 1.0, 100e-2]'409 output: ['true']410411 - program: '. as $big | [$big, $big + 1] | map(. > 10000000000000000000000000000000) | . == if have_decnum then [true, false] else [false, false] end'412 input: '10000000000000000000000000000001'413 output: ['true']414415 - title: "Object Identifier-Index: `.foo`, `.foo.bar`"416 body: |417418 The simplest *useful* filter has the form `.foo`. When given a419 JSON object (aka dictionary or hash) as input, `.foo` produces420 the value at the key "foo" if the key is present, or null otherwise.421422 A filter of the form `.foo.bar` is equivalent to `.foo | .bar`.423424 The `.foo` syntax only works for simple, identifier-like keys, that425 is, keys that are all made of alphanumeric characters and426 underscore, and which do not start with a digit.427428 If the key contains special characters or starts with a digit,429 you need to surround it with double quotes like this:430 `."foo$"`, or else `.["foo$"]`.431432 For example `.["foo::bar"]` and `.["foo.bar"]` work while433 `.foo::bar` does not.434435 examples:436 - program: '.foo'437 input: '{"foo": 42, "bar": "less interesting data"}'438 output: ['42']439440 - program: '.foo'441 input: '{"notfoo": true, "alsonotfoo": false}'442 output: ['null']443444 - program: '.["foo"]'445 input: '{"foo": 42}'446 output: ['42']447448 - title: "Optional Object Identifier-Index: `.foo?`"449 body: |450451 Just like `.foo`, but does not output an error when `.` is not an452 object.453454 examples:455 - program: '.foo?'456 input: '{"foo": 42, "bar": "less interesting data"}'457 output: ['42']458 - program: '.foo?'459 input: '{"notfoo": true, "alsonotfoo": false}'460 output: ['null']461 - program: '.["foo"]?'462 input: '{"foo": 42}'463 output: ['42']464 - program: '[.foo?]'465 input: '[1,2]'466 output: ['[]']467468 - title: "Object Index: `.[<string>]`"469 body: |470471 You can also look up fields of an object using syntax like472 `.["foo"]` (`.foo` above is a shorthand version of this, but473 only for identifier-like strings).474475 - title: "Array Index: `.[<number>]`"476 body: |477478 When the index value is an integer, `.[<number>]` can index479 arrays. Arrays are zero-based, so `.[2]` returns the third480 element.481482 Negative indices are allowed, with -1 referring to the last483 element, -2 referring to the next to last element, and so on.484485 examples:486 - program: '.[0]'487 input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'488 output: ['{"name":"JSON", "good":true}']489490 - program: '.[2]'491 input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'492 output: ['null']493494 - program: '.[-2]'495 input: '[1,2,3]'496 output: ['2']497498 - title: "Array/String Slice: `.[<number>:<number>]`"499 body: |500501 The `.[<number>:<number>]` syntax can be used to return a502 subarray of an array or substring of a string. The array503 returned by `.[10:15]` will be of length 5, containing the504 elements from index 10 (inclusive) to index 15 (exclusive).505 Either index may be negative (in which case it counts506 backwards from the end of the array), or omitted (in which507 case it refers to the start or end of the array).508 Indices are zero-based.509510 examples:511 - program: '.[2:4]'512 input: '["a","b","c","d","e"]'513 output: ['["c", "d"]']514515 - program: '.[2:4]'516 input: '"abcdefghi"'517 output: ['"cd"']518519 - program: '.[:3]'520 input: '["a","b","c","d","e"]'521 output: ['["a", "b", "c"]']522523 - program: '.[-2:]'524 input: '["a","b","c","d","e"]'525 output: ['["d", "e"]']526527 - title: "Array/Object Value Iterator: `.[]`"528 body: |529530 If you use the `.[index]` syntax, but omit the index531 entirely, it will return *all* of the elements of an532 array. Running `.[]` with the input `[1,2,3]` will produce the533 numbers as three separate results, rather than as a single534 array. A filter of the form `.foo[]` is equivalent to535 `.foo | .[]`.536537 You can also use this on an object, and it will return all538 the values of the object.539540 Note that the iterator operator is a generator of values.541542 examples:543 - program: '.[]'544 input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'545 output:546 - '{"name":"JSON", "good":true}'547 - '{"name":"XML", "good":false}'548549 - program: '.[]'550 input: '[]'551 output: []552553 - program: '.foo[]'554 input: '{"foo":[1,2,3]}'555 output: ['1','2','3']556557 - program: '.[]'558 input: '{"a": 1, "b": 1}'559 output: ['1', '1']560561 - title: "`.[]?`"562 body: |563564 Like `.[]`, but no errors will be output if . is not an array565 or object. A filter of the form `.foo[]?` is equivalent to566 `.foo | .[]?`.567568 - title: "Comma: `,`"569 body: |570571 If two filters are separated by a comma, then the572 same input will be fed into both and the two filters' output573 value streams will be concatenated in order: first, all of the574 outputs produced by the left expression, and then all of the575 outputs produced by the right. For instance, filter `.foo,576 .bar`, produces both the "foo" fields and "bar" fields as577 separate outputs.578579 The `,` operator is one way to construct generators.580581 examples:582 - program: '.foo, .bar'583 input: '{"foo": 42, "bar": "something else", "baz": true}'584 output: ['42', '"something else"']585586 - program: ".user, .projects[]"587 input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'588 output: ['"stedolan"', '"jq"', '"wikiflow"']589590 - program: '.[4,2]'591 input: '["a","b","c","d","e"]'592 output: ['"e"', '"c"']593594 - title: "Pipe: `|`"595 body: |596597 The | operator combines two filters by feeding the output(s) of598 the one on the left into the input of the one on the right. It's599 similar to the Unix shell's pipe, if you're used to that.600601 If the one on the left produces multiple results, the one on602 the right will be run for each of those results. So, the603 expression `.[] | .foo` retrieves the "foo" field of each604 element of the input array. This is a cartesian product,605 which can be surprising.606607 Note that `.a.b.c` is the same as `.a | .b | .c`.608609 Note too that `.` is the input value at the particular stage610 in a "pipeline", specifically: where the `.` expression appears.611 Thus `.a | . | .b` is the same as `.a.b`, as the `.` in the612 middle refers to whatever value `.a` produced.613614 examples:615 - program: '.[] | .name'616 input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'617 output: ['"JSON"', '"XML"']618619 - title: "Parenthesis"620 body: |621622 Parenthesis work as a grouping operator just as in any typical623 programming language.624625 examples:626 - program: '(. + 2) * 5'627 input: '1'628 output: ['15']629630 - title: Types and Values631 body: |632633 jq supports the same set of datatypes as JSON - numbers,634 strings, booleans, arrays, objects (which in JSON-speak are635 hashes with only string keys), and "null".636637 Booleans, null, strings and numbers are written the same way as638 in JSON. Just like everything else in jq, these simple639 values take an input and produce an output - `42` is a valid jq640 expression that takes an input, ignores it, and returns 42641 instead.642643 Numbers in jq are internally represented by their IEEE754 double644 precision approximation. Any arithmetic operation with numbers,645 whether they are literals or results of previous filters, will646 produce a double precision floating point result.647648 However, when parsing a literal jq will store the original literal649 string. If no mutation is applied to this value then it will make650 to the output in its original form, even if conversion to double651 would result in a loss.652653 entries:654 - title: "Array construction: `[]`"655 body: |656657 As in JSON, `[]` is used to construct arrays, as in658 `[1,2,3]`. The elements of the arrays can be any jq659 expression, including a pipeline. All of the results produced660 by all of the expressions are collected into one big array.661 You can use it to construct an array out of a known quantity662 of values (as in `[.foo, .bar, .baz]`) or to "collect" all the663 results of a filter into an array (as in `[.items[].name]`)664665 Once you understand the "," operator, you can look at jq's array666 syntax in a different light: the expression `[1,2,3]` is not using a667 built-in syntax for comma-separated arrays, but is instead applying668 the `[]` operator (collect results) to the expression 1,2,3 (which669 produces three different results).670671 If you have a filter `X` that produces four results,672 then the expression `[X]` will produce a single result, an673 array of four elements.674675 examples:676 - program: "[.user, .projects[]]"677 input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'678 output: ['["stedolan", "jq", "wikiflow"]']679 - program: "[ .[] | . * 2]"680 input: '[1, 2, 3]'681 output: ['[2, 4, 6]']682683 - title: "Object Construction: `{}`"684 body: |685686 Like JSON, `{}` is for constructing objects (aka687 dictionaries or hashes), as in: `{"a": 42, "b": 17}`.688689 If the keys are "identifier-like", then the quotes can be left690 off, as in `{a:42, b:17}`. Variable references as key691 expressions use the value of the variable as the key. Key692 expressions other than constant literals, identifiers, or693 variable references, need to be parenthesized, e.g.,694 `{("a"+"b"):59}`.695696 The value can be any expression (although you may need to wrap697 it in parentheses if, for example, it contains colons), which698 gets applied to the {} expression's input (remember, all699 filters have an input and an output).700701 {foo: .bar}702703 will produce the JSON object `{"foo": 42}` if given the JSON704 object `{"bar":42, "baz":43}` as its input. You can use this705 to select particular fields of an object: if the input is an706 object with "user", "title", "id", and "content" fields and707 you just want "user" and "title", you can write708709 {user: .user, title: .title}710711 Because that is so common, there's a shortcut syntax for it:712 `{user, title}`.713714 If one of the expressions produces multiple results,715 multiple dictionaries will be produced. If the input's716717 {"user":"stedolan","titles":["JQ Primer", "More JQ"]}718719 then the expression720721 {user, title: .titles[]}722723 will produce two outputs:724725 {"user":"stedolan", "title": "JQ Primer"}726 {"user":"stedolan", "title": "More JQ"}727728 Putting parentheses around the key means it will be evaluated as an729 expression. With the same input as above,730731 {(.user): .titles}732733 produces734735 {"stedolan": ["JQ Primer", "More JQ"]}736737 Variable references as keys use the value of the variable as738 the key. Without a value then the variable's name becomes the739 key and its value becomes the value,740741 "f o o" as $foo | "b a r" as $bar | {$foo, $bar:$foo}742743 produces744745 {"foo":"f o o","b a r":"f o o"}746747 examples:748 - program: '{user, title: .titles[]}'749 input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'750 output:751 - '{"user":"stedolan", "title": "JQ Primer"}'752 - '{"user":"stedolan", "title": "More JQ"}'753 - program: '{(.user): .titles}'754 input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'755 output: ['{"stedolan": ["JQ Primer", "More JQ"]}']756757 - title: "Recursive Descent: `..`"758 body: |759760 Recursively descends `.`, producing every value. This is the761 same as the zero-argument `recurse` builtin (see below). This762 is intended to resemble the XPath `//` operator. Note that763 `..a` does not work; use `.. | .a` instead. In the example764 below we use `.. | .a?` to find all the values of object keys765 "a" in any object found "below" `.`.766767 This is particularly useful in conjunction with `path(EXP)`768 (also see below) and the `?` operator.769770 examples:771 - program: '.. | .a?'772 input: '[[{"a":1}]]'773 output: ['1']774775 - title: Builtin operators and functions776 body: |777778 Some jq operators (for instance, `+`) do different things779 depending on the type of their arguments (arrays, numbers,780 etc.). However, jq never does implicit type conversions. If you781 try to add a string to an object you'll get an error message and782 no result.783784 Please note that all numbers are converted to IEEE754 double precision785 floating point representation. Arithmetic and logical operators are working786 with these converted doubles. Results of all such operations are also limited787 to the double precision.788789 The only exception to this behaviour of number is a snapshot of original number790 literal. When a number which originally was provided as a literal is never791 mutated until the end of the program then it is printed to the output in its792 original literal form. This also includes cases when the original literal793 would be truncated when converted to the IEEE754 double precision floating point794 number.795796 entries:797 - title: "Addition: `+`"798 body: |799800 The operator `+` takes two filters, applies them both801 to the same input, and adds the results together. What802 "adding" means depends on the types involved:803804 - **Numbers** are added by normal arithmetic.805806 - **Arrays** are added by being concatenated into a larger array.807808 - **Strings** are added by being joined into a larger string.809810 - **Objects** are added by merging, that is, inserting all811 the key-value pairs from both objects into a single812 combined object. If both objects contain a value for the813 same key, the object on the right of the `+` wins. (For814 recursive merge use the `*` operator.)815816 `null` can be added to any value, and returns the other817 value unchanged.818819 examples:820 - program: '.a + 1'821 input: '{"a": 7}'822 output: ['8']823 - program: '.a + .b'824 input: '{"a": [1,2], "b": [3,4]}'825 output: ['[1,2,3,4]']826 - program: '.a + null'827 input: '{"a": 1}'828 output: ['1']829 - program: '.a + 1'830 input: '{}'831 output: ['1']832 - program: '{a: 1} + {b: 2} + {c: 3} + {a: 42}'833 input: 'null'834 output: ['{"a": 42, "b": 2, "c": 3}']835836 - title: "Subtraction: `-`"837 body: |838839 As well as normal arithmetic subtraction on numbers, the `-`840 operator can be used on arrays to remove all occurrences of841 the second array's elements from the first array.842843 examples:844 - program: '4 - .a'845 input: '{"a":3}'846 output: ['1']847 - program: . - ["xml", "yaml"]848 input: '["xml", "yaml", "json"]'849 output: ['["json"]']850851 - title: "Multiplication, division, modulo: `*`, `/`, `%`"852 body: |853854 These infix operators behave as expected when given two numbers.855 Division by zero raises an error. `x % y` computes x modulo y.856857 Multiplying a string by a number produces the concatenation of858 that string that many times. `"x" * 0` produces `""`.859860 Dividing a string by another splits the first using the second861 as separators.862863 Multiplying two objects will merge them recursively: this works864 like addition but if both objects contain a value for the865 same key, and the values are objects, the two are merged with866 the same strategy.867868 examples:869 - program: '10 / . * 3'870 input: '5'871 output: ['6']872 - program: '. / ", "'873 input: '"a, b,c,d, e"'874 output: ['["a","b,c,d","e"]']875 - program: '{"k": {"a": 1, "b": 2}} * {"k": {"a": 0,"c": 3}}'876 input: 'null'877 output: ['{"k": {"a": 0, "b": 2, "c": 3}}']878 - program: '.[] | (1 / .)?'879 input: '[1,0,-1]'880 output: ['1', '-1']881882 - title: "`abs`"883 body: |884885 The builtin function `abs` is defined naively as: `if . < 0 then - . else . end`.886887 For numeric input, this is the absolute value. See the888 section on the identity filter for the implications of this889 definition for numeric input.890

Fix typo 'you may wish [to] use' (#3604)Anna Riftbefore adoption4bb50d7

line 891why, one step back
891 To compute the absolute value of a number as a floating point number, you may wish to use `fabs`.

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 892–1479why, one step back
892
893 examples:894 - program: 'map(abs)'895 input: '[-10, -1.1, -1e-1]'896 output: ['[10,1.1,1e-1]']897898 - title: "`length`"899 body: |900901 The builtin function `length` gets the length of various902 different types of value:903904 - The length of a **string** is the number of Unicode905 codepoints it contains (which will be the same as its906 JSON-encoded length in bytes if it's pure ASCII).907908 - The length of a **number** is its absolute value.909910 - The length of an **array** is the number of elements.911912 - The length of an **object** is the number of key-value pairs.913914 - The length of **null** is zero.915916 - It is an error to use `length` on a **boolean**.917918 examples:919 - program: '.[] | length'920 input: '[[1,2], "string", {"a":2}, null, -5]'921 output: ['2', '6', '1', '0', '5']922923924 - title: "`utf8bytelength`"925 body: |926927 The builtin function `utf8bytelength` outputs the number of928 bytes used to encode a string in UTF-8.929930 examples:931 - program: 'utf8bytelength'932 input: '"\u03bc"'933 output: ['2']934935 - title: "`keys`, `keys_unsorted`"936 body: |937938 The builtin function `keys`, when given an object, returns939 its keys in an array.940941 The keys are sorted "alphabetically", by unicode codepoint942 order. This is not an order that makes particular sense in943 any particular language, but you can count on it being the944 same for any two objects with the same set of keys,945 regardless of locale settings.946947 When `keys` is given an array, it returns the valid indices948 for that array: the integers from 0 to length-1.949950 The `keys_unsorted` function is just like `keys`, but if951 the input is an object then the keys will not be sorted,952 instead the keys will roughly be in insertion order.953954 examples:955 - program: 'keys'956 input: '{"abc": 1, "abcd": 2, "Foo": 3}'957 output: ['["Foo", "abc", "abcd"]']958 - program: 'keys'959 input: '[42,3,35]'960 output: ['[0,1,2]']961962 - title: "`has(key)`"963 body: |964965 The builtin function `has` returns whether the input object966 has the given key, or the input array has an element at the967 given index.968969 `has($key)` has the same effect as checking whether `$key`970 is a member of the array returned by `keys`, although `has`971 will be faster.972973 examples:974 - program: 'map(has("foo"))'975 input: '[{"foo": 42}, {}]'976 output: ['[true, false]']977 - program: 'map(has(2))'978 input: '[[0,1], ["a","b","c"]]'979 output: ['[false, true]']980981 - title: "`in`"982 body: |983984 The builtin function `in` returns whether or not the input key is in the985 given object, or the input index corresponds to an element986 in the given array. It is, essentially, an inversed version987 of `has`.988989 examples:990 - program: '.[] | in({"foo": 42})'991 input: '["foo", "bar"]'992 output: ['true', 'false']993 - program: 'map(in([0,1]))'994 input: '[2, 0]'995 output: ['[false, true]']996997 - title: "`map(f)`, `map_values(f)`"998 body: |9991000 For any filter `f`, `map(f)` and `map_values(f)` apply `f`1001 to each of the values in the input array or object, that is,1002 to the values of `.[]`.10031004 In the absence of errors, `map(f)` always outputs an array1005 whereas `map_values(f)` outputs an array if given an array,1006 or an object if given an object.10071008 When the input to `map_values(f)` is an object, the output1009 object has the same keys as the input object except for1010 those keys whose values when piped to `f` produce no values1011 at all.10121013 The key difference between `map(f)` and `map_values(f)` is1014 that the former simply forms an array from all the values of1015 `($x|f)` for each value, `$x`, in the input array or object,1016 but `map_values(f)` only uses `first($x|f)`.10171018 Specifically, for object inputs, `map_values(f)` constructs1019 the output object by examining in turn the value of1020 `first(.[$k]|f)` for each key, `$k`, of the input. If this1021 expression produces no values, then the corresponding key1022 will be dropped; otherwise, the output object will have that1023 value at the key, `$k`.10241025 Here are some examples to clarify the behavior of `map` and1026 `map_values` when applied to arrays. These examples assume the1027 input is `[1]` in all cases:10281029 map(.+1) #=> [2]1030 map(., .) #=> [1,1]1031 map(empty) #=> []10321033 map_values(.+1) #=> [2]1034 map_values(., .) #=> [1]1035 map_values(empty) #=> []10361037 `map(f)` is equivalent to `[.[] | f]` and1038 `map_values(f)` is equivalent to `.[] |= f`.10391040 In fact, these are their implementations.104110421043 examples:1044 - program: 'map(.+1)'1045 input: '[1,2,3]'1046 output: ['[2,3,4]']10471048 - program: 'map_values(.+1)'1049 input: '{"a": 1, "b": 2, "c": 3}'1050 output: ['{"a": 2, "b": 3, "c": 4}']10511052 - program: 'map(., .)'1053 input: '[1,2]'1054 output: ['[1,1,2,2]']10551056 - program: 'map_values(. // empty)'1057 input: '{"a": null, "b": true, "c": false}'1058 output: ['{"b":true}']105910601061 - title: "`pick(pathexps)`"1062 body: |10631064 Emit the projection of the input object or array defined by the1065 specified sequence of path expressions, such that if `p` is any1066 one of these specifications, then `(. | p)` will evaluate to the1067 same value as `(. | pick(pathexps) | p)`. For arrays, negative1068 indices and `.[m:n]` specifications should not be used.10691070 examples:1071 - program: 'pick(.a, .b.c, .x)'1072 input: '{"a": 1, "b": {"c": 2, "d": 3}, "e": 4}'1073 output: ['{"a":1,"b":{"c":2},"x":null}']10741075 - program: 'pick(.[2], .[0], .[0])'1076 input: '[1,2,3,4]'1077 output: ['[1,null,3]']107810791080 - title: "`path(path_expression)`"1081 body: |10821083 Outputs array representations of the given path expression1084 in `.`. The outputs are arrays of strings (object keys)1085 and/or numbers (array indices).10861087 Path expressions are jq expressions like `.a`, but also `.[]`.1088 There are two types of path expressions: ones that can match1089 exactly, and ones that cannot. For example, `.a.b.c` is an1090 exact match path expression, while `.a[].b` is not.10911092 `path(exact_path_expression)` will produce the array1093 representation of the path expression even if it does not1094 exist in `.`, if `.` is `null` or an array or an object.10951096 `path(pattern)` will produce array representations of the1097 paths matching `pattern` if the paths exist in `.`.10981099 Note that the path expressions are not different from normal1100 expressions. The expression1101 `path(..|select(type=="boolean"))` outputs all the paths to1102 boolean values in `.`, and only those paths.11031104 examples:1105 - program: 'path(.a[0].b)'1106 input: 'null'1107 output: ['["a",0,"b"]']1108 - program: '[path(..)]'1109 input: '{"a":[{"b":1}]}'1110 output: ['[[],["a"],["a",0],["a",0,"b"]]']11111112 - title: "`del(path_expression)`"1113 body: |11141115 The builtin function `del` removes a key and its corresponding1116 value from an object.11171118 examples:1119 - program: 'del(.foo)'1120 input: '{"foo": 42, "bar": 9001, "baz": 42}'1121 output: ['{"bar": 9001, "baz": 42}']1122 - program: 'del(.[1, 2])'1123 input: '["foo", "bar", "baz"]'1124 output: ['["foo"]']11251126 - title: "`getpath(PATHS)`"1127 body: |11281129 The builtin function `getpath` outputs the values in `.` found1130 at each path in `PATHS`.11311132 examples:1133 - program: 'getpath(["a","b"])'1134 input: 'null'1135 output: ['null']1136 - program: '[getpath(["a","b"], ["a","c"])]'1137 input: '{"a":{"b":0, "c":1}}'1138 output: ['[0, 1]']11391140 - title: "`setpath(PATHS; VALUE)`"1141 body: |11421143 The builtin function `setpath` sets the `PATHS` in `.` to `VALUE`.11441145 examples:1146 - program: 'setpath(["a","b"]; 1)'1147 input: 'null'1148 output: ['{"a": {"b": 1}}']1149 - program: 'setpath(["a","b"]; 1)'1150 input: '{"a":{"b":0}}'1151 output: ['{"a": {"b": 1}}']1152 - program: 'setpath([0,"a"]; 1)'1153 input: 'null'1154 output: ['[{"a":1}]']11551156 - title: "`delpaths(PATHS)`"1157 body: |11581159 The builtin function `delpaths` deletes the `PATHS` in `.`.1160 `PATHS` must be an array of paths, where each path is an array1161 of strings and numbers.11621163 examples:1164 - program: 'delpaths([["a","b"]])'1165 input: '{"a":{"b":1},"x":{"y":2}}'1166 output: ['{"a":{},"x":{"y":2}}']11671168 - title: "`to_entries`, `from_entries`, `with_entries(f)`"1169 body: |11701171 These functions convert between an object and an array of1172 key-value pairs. If `to_entries` is passed an object, then1173 for each `k: v` entry in the input, the output array1174 includes `{"key": k, "value": v}`.11751176 `from_entries` does the opposite conversion, and `with_entries(f)`1177 is a shorthand for `to_entries | map(f) | from_entries`, useful for1178 doing some operation to all keys and values of an object.1179 `from_entries` accepts `"key"`, `"Key"`, `"name"`, `"Name"`,1180 `"value"`, and `"Value"` as keys.11811182 examples:1183 - program: 'to_entries'1184 input: '{"a": 1, "b": 2}'1185 output: ['[{"key":"a", "value":1}, {"key":"b", "value":2}]']1186 - program: 'from_entries'1187 input: '[{"key":"a", "value":1}, {"key":"b", "value":2}]'1188 output: ['{"a": 1, "b": 2}']1189 - program: 'with_entries(.key |= "KEY_" + .)'1190 input: '{"a": 1, "b": 2}'1191 output: ['{"KEY_a": 1, "KEY_b": 2}']119211931194 - title: "`select(boolean_expression)`"1195 body: |11961197 The function `select(f)` produces its input unchanged if1198 `f` returns true for that input, and produces no output1199 otherwise.12001201 It's useful for filtering lists: `[1,2,3] | map(select(. >= 2))`1202 will give you `[2,3]`.12031204 examples:1205 - program: 'map(select(. >= 2))'1206 input: '[1,5,3,0,7]'1207 output: ['[5,3,7]']1208 - program: '.[] | select(.id == "second")'1209 input: '[{"id": "first", "val": 1}, {"id": "second", "val": 2}]'1210 output: ['{"id": "second", "val": 2}']121112121213 - title: "`arrays`, `objects`, `iterables`, `booleans`, `numbers`, `normals`, `finites`, `strings`, `nulls`, `values`, `scalars`"1214 body: |12151216 These built-ins select only inputs that are arrays, objects,1217 iterables (arrays or objects), booleans, numbers, normal1218 numbers, finite numbers, strings, null, non-null values, and1219 non-iterables, respectively.12201221 examples:1222 - program: '.[]|numbers'1223 input: '[[],{},1,"foo",null,true,false]'1224 output: ['1']12251226 - title: "`empty`"1227 body: |12281229 `empty` returns no results. None at all. Not even `null`.12301231 It's useful on occasion. You'll know if you need it :)12321233 examples:1234 - program: '1, empty, 2'1235 input: 'null'1236 output: ['1', '2']1237 - program: '[1,2,empty,3]'1238 input: 'null'1239 output: ['[1,2,3]']12401241 - title: "`error`, `error(message)`"1242 body: |12431244 Produces an error with the input value, or with the message1245 given as the argument. Errors can be caught with try/catch;1246 see below.12471248 examples:1249 - program: 'try error catch .'1250 input: '"error message"'1251 output: ['"error message"']12521253 - program: 'try error("invalid value: \(.)") catch .'1254 input: '42'1255 output: ['"invalid value: 42"']12561257 - title: "`halt`"1258 body: |12591260 Stops the jq program with no further outputs. jq will exit1261 with exit status `0`.12621263 - title: "`halt_error`, `halt_error(exit_code)`"1264 body: |12651266 Stops the jq program with no further outputs. The input will1267 be printed on `stderr` as raw output (i.e., strings will not1268 have double quotes) with no decoration, not even a newline.12691270 The given `exit_code` (defaulting to `5`) will be jq's exit1271 status.12721273 For example, `"Error: something went wrong\n"|halt_error(1)`.12741275 - title: "`$__loc__`"1276 body: |12771278 Produces an object with a "file" key and a "line" key, with1279 the filename and line number where `$__loc__` occurs, as1280 values.12811282 examples:1283 - program: 'try error("\($__loc__)") catch .'1284 input: 'null'1285 output: ['"{\"file\":\"<top-level>\",\"line\":1}"']12861287 - title: "`paths`, `paths(node_filter)`"1288 body: |12891290 `paths` outputs the paths to all the elements in its input1291 (except it does not output the empty list, representing .1292 itself).12931294 `paths(f)` outputs the paths to any values for which `f` is `true`.1295 That is, `paths(type == "number")` outputs the paths to all numeric1296 values.12971298 examples:1299 - program: '[paths]'1300 input: '[1,[[],{"a":2}]]'1301 output: ['[[0],[1],[1,0],[1,1],[1,1,"a"]]']1302 - program: '[paths(type == "number")]'1303 input: '[1,[[],{"a":2}]]'1304 output: ['[[0],[1,1,"a"]]']13051306 - title: "`add`, `add(generator)`"1307 body: |13081309 The filter `add` takes as input an array, and produces as1310 output the elements of the array added together. This might1311 mean summed, concatenated or merged depending on the types1312 of the elements of the input array - the rules are the same1313 as those for the `+` operator (described above).13141315 If the input is an empty array, `add` returns `null`.13161317 `add(generator)` operates on the given generator rather than1318 the input.13191320 examples:1321 - program: add1322 input: '["a","b","c"]'1323 output: ['"abc"']1324 - program: add1325 input: '[1, 2, 3]'1326 output: ['6']1327 - program: add1328 input: '[]'1329 output: ["null"]1330 - program: add(.[].a)1331 input: '[{"a":3}, {"a":5}, {"b":6}]'1332 output: ['8']13331334 - title: "`any`, `any(condition)`, `any(generator; condition)`"1335 body: |13361337 The filter `any` takes as input an array of boolean values,1338 and produces `true` as output if any of the elements of1339 the array are `true`.13401341 If the input is an empty array, `any` returns `false`.13421343 The `any(condition)` form applies the given condition to the1344 elements of the input array.13451346 The `any(generator; condition)` form applies the given1347 condition to all the outputs of the given generator.13481349 examples:1350 - program: any1351 input: '[true, false]'1352 output: ["true"]1353 - program: any1354 input: '[false, false]'1355 output: ["false"]1356 - program: any1357 input: '[]'1358 output: ["false"]13591360 - title: "`all`, `all(condition)`, `all(generator; condition)`"1361 body: |13621363 The filter `all` takes as input an array of boolean values,1364 and produces `true` as output if all of the elements of1365 the array are `true`.13661367 The `all(condition)` form applies the given condition to the1368 elements of the input array.13691370 The `all(generator; condition)` form applies the given1371 condition to all the outputs of the given generator.13721373 If the input is an empty array, `all` returns `true`.13741375 examples:1376 - program: all1377 input: '[true, false]'1378 output: ["false"]1379 - program: all1380 input: '[true, true]'1381 output: ["true"]1382 - program: all1383 input: '[]'1384 output: ["true"]13851386 - title: "`flatten`, `flatten(depth)`"1387 body: |13881389 The filter `flatten` takes as input an array of nested arrays,1390 and produces a flat array in which all arrays inside the original1391 array have been recursively replaced by their values. You can pass1392 an argument to it to specify how many levels of nesting to flatten.13931394 `flatten(2)` is like `flatten`, but going only up to two1395 levels deep.13961397 examples:1398 - program: flatten1399 input: '[1, [2], [[3]]]'1400 output: ["[1, 2, 3]"]1401 - program: flatten(1)1402 input: '[1, [2], [[3]]]'1403 output: ["[1, 2, [3]]"]1404 - program: flatten1405 input: '[[]]'1406 output: ["[]"]1407 - program: flatten1408 input: '[{"foo": "bar"}, [{"foo": "baz"}]]'1409 output: ['[{"foo": "bar"}, {"foo": "baz"}]']14101411 - title: "`range(upto)`, `range(from; upto)`, `range(from; upto; by)`"1412 body: |14131414 The `range` function produces a range of numbers. `range(4; 10)`1415 produces 6 numbers, from 4 (inclusive) to 10 (exclusive). The numbers1416 are produced as separate outputs. Use `[range(4; 10)]` to get a range as1417 an array.14181419 The one argument form generates numbers from 0 to the given1420 number, with an increment of 1.14211422 The two argument form generates numbers from `from` to `upto`1423 with an increment of 1.14241425 The three argument form generates numbers `from` to `upto`1426 with an increment of `by`.14271428 examples:1429 - program: 'range(2; 4)'1430 input: 'null'1431 output: ['2', '3']1432 - program: '[range(2; 4)]'1433 input: 'null'1434 output: ['[2,3]']1435 - program: '[range(4)]'1436 input: 'null'1437 output: ['[0,1,2,3]']1438 - program: '[range(0; 10; 3)]'1439 input: 'null'1440 output: ['[0,3,6,9]']1441 - program: '[range(0; 10; -1)]'1442 input: 'null'1443 output: ['[]']1444 - program: '[range(0; -5; -1)]'1445 input: 'null'1446 output: ['[0,-1,-2,-3,-4]']14471448 - title: "`floor`"1449 body: |14501451 The `floor` function returns the floor of its numeric input.14521453 examples:1454 - program: 'floor'1455 input: '3.14159'1456 output: ['3']14571458 - title: "`sqrt`"1459 body: |14601461 The `sqrt` function returns the square root of its numeric input.14621463 examples:1464 - program: 'sqrt'1465 input: '9'1466 output: ['3']14671468 - title: "`tonumber`"1469 body: |14701471 The `tonumber` function parses its input as a number. It1472 will convert correctly-formatted strings to their numeric1473 equivalent, leave numbers alone, and give an error on all other input.14741475 examples:1476 - program: '.[] | tonumber'1477 input: '[1, "1"]'1478 output: ['1', '1']1479

Adds "toboolean" builtin function for converting strings to booleans (#2098)Brahm Lowerbefore adoptionc350566

lines 1480–1491why, one step back
1480 - title: "`toboolean`"
1481 body: |14821483 The `toboolean` function parses its input as a boolean. It1484 will convert correctly-formatted strings to their boolean1485 equivalent, leave booleans alone, and give an error on all other input.14861487 examples:1488 - program: '.[] | toboolean'1489 input: '["true", "false", true, false]'1490 output: ['true', 'false', 'true', 'false']1491

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1492–1800why, one step back
1492 - title: "`tostring`"
1493 body: |14941495 The `tostring` function prints its input as a1496 string. Strings are left unchanged, and all other values are1497 JSON-encoded.14981499 examples:1500 - program: '.[] | tostring'1501 input: '[1, "1", [1]]'1502 output: ['"1"', '"1"', '"[1]"']15031504 - title: "`type`"1505 body: |15061507 The `type` function returns the type of its argument as a1508 string, which is one of null, boolean, number, string, array1509 or object.15101511 examples:1512 - program: 'map(type)'1513 input: '[0, false, [], {}, null, "hello"]'1514 output: ['["number", "boolean", "array", "object", "null", "string"]']15151516 - title: "`infinite`, `nan`, `isinfinite`, `isnan`, `isfinite`, `isnormal`"1517 body: |15181519 Some arithmetic operations can yield infinities and "not a1520 number" (NaN) values. The `isinfinite` builtin returns `true`1521 if its input is infinite. The `isnan` builtin returns `true`1522 if its input is a NaN. The `infinite` builtin returns a1523 positive infinite value. The `nan` builtin returns a NaN.1524 The `isnormal` builtin returns true if its input is a normal1525 number.15261527 Note that division by zero raises an error.15281529 Currently most arithmetic operations operating on infinities,1530 NaNs, and sub-normals do not raise errors.15311532 examples:1533 - program: '.[] | (infinite * .) < 0'1534 input: '[-1, 1]'1535 output: ['true', 'false']1536 - program: 'infinite, nan | type'1537 input: 'null'1538 output: ['"number"', '"number"']15391540 - title: "`sort`, `sort_by(path_expression)`"1541 body: |15421543 The `sort` functions sorts its input, which must be an1544 array. Values are sorted in the following order:15451546 * `null`1547 * `false`1548 * `true`1549 * numbers1550 * strings, in alphabetical order (by unicode codepoint value)1551 * arrays, in lexical order1552 * objects15531554 The ordering for objects is a little complex: first they're1555 compared by comparing their sets of keys (as arrays in1556 sorted order), and if their keys are equal then the values1557 are compared key by key.15581559 `sort_by` may be used to sort by a particular field of an1560 object, or by applying any jq filter. `sort_by(f)` compares1561 two elements by comparing the result of `f` on each element.1562 When `f` produces multiple values, it firstly compares the1563 first values, and the second values if the first values are1564 equal, and so on.15651566 examples:1567 - program: 'sort'1568 input: '[8,3,null,6]'1569 output: ['[null,3,6,8]']15701571 - program: 'sort_by(.foo)'1572 input: '[{"foo":4, "bar":10}, {"foo":3, "bar":10}, {"foo":2, "bar":1}]'1573 output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":4, "bar":10}]']15741575 - program: 'sort_by(.foo, .bar)'1576 input: '[{"foo":4, "bar":10}, {"foo":3, "bar":20}, {"foo":2, "bar":1}, {"foo":3, "bar":10}]'1577 output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":3, "bar":20}, {"foo":4, "bar":10}]']15781579 - title: "`group_by(path_expression)`"1580 body: |15811582 `group_by(.foo)` takes as input an array, groups the1583 elements having the same `.foo` field into separate arrays,1584 and produces all of these arrays as elements of a larger1585 array, sorted by the value of the `.foo` field.15861587 Any jq expression, not just a field access, may be used in1588 place of `.foo`. The sorting order is the same as described1589 in the `sort` function above.15901591 examples:1592 - program: 'group_by(.foo)'1593 input: '[{"foo":1, "bar":10}, {"foo":3, "bar":100}, {"foo":1, "bar":1}]'1594 output: ['[[{"foo":1, "bar":10}, {"foo":1, "bar":1}], [{"foo":3, "bar":100}]]']15951596 - title: "`min`, `max`, `min_by(path_exp)`, `max_by(path_exp)`"1597 body: |15981599 Find the minimum or maximum element of the input array.16001601 The `min_by(path_exp)` and `max_by(path_exp)` functions allow1602 you to specify a particular field or property to examine, e.g.1603 `min_by(.foo)` finds the object with the smallest `foo` field.16041605 examples:1606 - program: 'min'1607 input: '[5,4,2,7]'1608 output: ['2']1609 - program: 'max_by(.foo)'1610 input: '[{"foo":1, "bar":14}, {"foo":2, "bar":3}]'1611 output: ['{"foo":2, "bar":3}']16121613 - title: "`unique`, `unique_by(path_exp)`"1614 body: |16151616 The `unique` function takes as input an array and produces1617 an array of the same elements, in sorted order, with1618 duplicates removed.16191620 The `unique_by(path_exp)` function will keep only one element1621 for each value obtained by applying the argument. Think of it1622 as making an array by taking one element out of every group1623 produced by `group`.16241625 examples:1626 - program: 'unique'1627 input: '[1,2,5,3,5,3,1,3]'1628 output: ['[1,2,3,5]']1629 - program: 'unique_by(.foo)'1630 input: '[{"foo": 1, "bar": 2}, {"foo": 1, "bar": 3}, {"foo": 4, "bar": 5}]'1631 output: ['[{"foo": 1, "bar": 2}, {"foo": 4, "bar": 5}]']1632 - program: 'unique_by(length)'1633 input: '["chunky", "bacon", "kitten", "cicada", "asparagus"]'1634 output: ['["bacon", "chunky", "asparagus"]']16351636 - title: "`reverse`"1637 body: |16381639 This function reverses an array.16401641 examples:1642 - program: 'reverse'1643 input: '[1,2,3,4]'1644 output: ['[4,3,2,1]']16451646 - title: "`contains(element)`"1647 body: |16481649 The filter `contains(b)` will produce true if b is1650 completely contained within the input. A string B is1651 contained in a string A if B is a substring of A. An array B1652 is contained in an array A if all elements in B are1653 contained in any element in A. An object B is contained in1654 object A if all of the values in B are contained in the1655 value in A with the same key. All other types are assumed to1656 be contained in each other if they are equal.16571658 examples:1659 - program: 'contains("bar")'1660 input: '"foobar"'1661 output: ['true']1662 - program: 'contains(["baz", "bar"])'1663 input: '["foobar", "foobaz", "blarp"]'1664 output: ['true']1665 - program: 'contains(["bazzzzz", "bar"])'1666 input: '["foobar", "foobaz", "blarp"]'1667 output: ['false']1668 - program: 'contains({foo: 12, bar: [{barp: 12}]})'1669 input: '{"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]}'1670 output: ['true']1671 - program: 'contains({foo: 12, bar: [{barp: 15}]})'1672 input: '{"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]}'1673 output: ['false']16741675 - title: "`indices(s)`"1676 body: |16771678 Outputs an array containing the indices in `.` where `s`1679 occurs. The input may be an array, in which case if `s` is an1680 array then the indices output will be those where all elements1681 in `.` match those of `s`.16821683 examples:1684 - program: 'indices(", ")'1685 input: '"a,b, cd, efg, hijk"'1686 output: ['[3,7,12]']1687 - program: 'indices(1)'1688 input: '[0,1,2,1,3,1,4]'1689 output: ['[1,3,5]']1690 - program: 'indices([1,2])'1691 input: '[0,1,2,3,1,4,2,5,1,2,6,7]'1692 output: ['[1,8]']16931694 - title: "`index(s)`, `rindex(s)`"1695 body: |16961697 Outputs the index of the first (`index`) or last (`rindex`)1698 occurrence of `s` in the input.16991700 examples:1701 - program: 'index(", ")'1702 input: '"a,b, cd, efg, hijk"'1703 output: ['3']1704 - program: 'index(1)'1705 input: '[0,1,2,1,3,1,4]'1706 output: ['1']1707 - program: 'index([1,2])'1708 input: '[0,1,2,3,1,4,2,5,1,2,6,7]'1709 output: ['1']1710 - program: 'rindex(", ")'1711 input: '"a,b, cd, efg, hijk"'1712 output: ['12']1713 - program: 'rindex(1)'1714 input: '[0,1,2,1,3,1,4]'1715 output: ['5']1716 - program: 'rindex([1,2])'1717 input: '[0,1,2,3,1,4,2,5,1,2,6,7]'1718 output: ['8']17191720 - title: "`inside`"1721 body: |17221723 The filter `inside(b)` will produce true if the input is1724 completely contained within b. It is, essentially, an1725 inversed version of `contains`.17261727 examples:1728 - program: 'inside("foobar")'1729 input: '"bar"'1730 output: ['true']1731 - program: 'inside(["foobar", "foobaz", "blarp"])'1732 input: '["baz", "bar"]'1733 output: ['true']1734 - program: 'inside(["foobar", "foobaz", "blarp"])'1735 input: '["bazzzzz", "bar"]'1736 output: ['false']1737 - program: 'inside({"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]})'1738 input: '{"foo": 12, "bar": [{"barp": 12}]}'1739 output: ['true']1740 - program: 'inside({"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]})'1741 input: '{"foo": 12, "bar": [{"barp": 15}]}'1742 output: ['false']17431744 - title: "`startswith(str)`"1745 body: |17461747 Outputs `true` if . starts with the given string argument.17481749 examples:1750 - program: '[.[]|startswith("foo")]'1751 input: '["fo", "foo", "barfoo", "foobar", "barfoob"]'1752 output: ['[false, true, false, true, false]']17531754 - title: "`endswith(str)`"1755 body: |17561757 Outputs `true` if . ends with the given string argument.17581759 examples:1760 - program: '[.[]|endswith("foo")]'1761 input: '["foobar", "barfoo"]'1762 output: ['[false, true]']17631764 - title: "`combinations`, `combinations(n)`"1765 body: |17661767 Outputs all combinations of the elements of the arrays in the1768 input array. If given an argument `n`, it outputs all combinations1769 of `n` repetitions of the input array.17701771 examples:1772 - program: 'combinations'1773 input: '[[1,2], [3, 4]]'1774 output: ['[1, 3]', '[1, 4]', '[2, 3]', '[2, 4]']1775 - program: 'combinations(2)'1776 input: '[0, 1]'1777 output: ['[0, 0]', '[0, 1]', '[1, 0]', '[1, 1]']17781779 - title: "`ltrimstr(str)`"1780 body: |17811782 Outputs its input with the given prefix string removed, if it1783 starts with it.17841785 examples:1786 - program: '[.[]|ltrimstr("foo")]'1787 input: '["fo", "foo", "barfoo", "foobar", "afoo"]'1788 output: ['["fo","","barfoo","bar","afoo"]']17891790 - title: "`rtrimstr(str)`"1791 body: |17921793 Outputs its input with the given suffix string removed, if it1794 ends with it.17951796 examples:1797 - program: '[.[]|rtrimstr("foo")]'1798 input: '["fo", "foo", "barfoo", "foobar", "foob"]'1799 output: ['["fo","","bar","foobar","foob"]']1800

Add trimstr/1 function (#3319)Gabriel Marinbefore adoption7d8c096

lines 1801–1811why, one step back
1801 - title: "`trimstr(str)`"
1802 body: |18031804 Outputs its input with the given string removed at both ends, if it1805 starts or ends with it.18061807 examples:1808 - program: '[.[]|trimstr("foo")]'1809 input: '["fo", "foo", "barfoo", "foobarfoo", "foob"]'1810 output: ['["fo","","bar","bar","b"]']1811

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1812–1822why, one step back
1812 - title: "`trim`, `ltrim`, `rtrim`"
1813 body: |18141815 `trim` trims both leading and trailing whitespace.18161817 `ltrim` trims only leading (left side) whitespace.18181819 `rtrim` trims only trailing (right side) whitespace.18201821 Whitespace characters are the usual `" "`, `"\n"` `"\t"`, `"\r"`1822 and also all characters in the Unicode character database with the

Fix a handful of typos (#3605)Anna Riftbefore adoption41b8edf

line 1823why, one step back
1823 whitespace property. Note that what is considered whitespace might

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1824–1867why, one step back
1824 change in the future.
18251826 examples:1827 - program: 'trim, ltrim, rtrim'1828 input: '" abc "'1829 output: ['"abc"', '"abc "', '" abc"']18301831 - title: "`explode`"1832 body: |18331834 Converts an input string into an array of the string's1835 codepoint numbers.18361837 examples:1838 - program: 'explode'1839 input: '"foobar"'1840 output: ['[102,111,111,98,97,114]']18411842 - title: "`implode`"1843 body: |18441845 The inverse of explode.18461847 examples:1848 - program: 'implode'1849 input: '[65, 66, 67]'1850 output: ['"ABC"']18511852 - title: "`split(str)`"1853 body: |18541855 Splits an input string on the separator argument.18561857 `split` can also split on regex matches when called with1858 two arguments (see the regular expressions section below).18591860 examples:1861 - program: 'split(", ")'1862 input: '"a, b,c,d, e, "'1863 output: ['["a","b,c,d","e",""]']18641865 - title: "`join(str)`"1866 body: |1867

docs: clarify join behavior on objects (#3555)ded-furbybefore adoption2d410d6

line 1868why, one step back
1868 Joins the values from the input array or object, using the

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1869–1873why, one step back
1869 argument as separator. It is the inverse of `split`: that is,
1870 running `split("foo") | join("foo")` over any input string1871 returns said input string.18721873 Numbers and booleans in the input are converted to strings.

docs: clarify join behavior on objects (#3555)ded-furbybefore adoption2d410d6

lines 1874–1877why, one step back
1874 Null values are treated as empty strings. When the input is
1875 an object, `join` operates on the values that `.[]`1876 produces. Arrays and objects among the joined values are not1877 supported.

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1878–1885why, one step back
1878
1879 examples:1880 - program: 'join(", ")'1881 input: '["a","b,c,d","e"]'1882 output: ['"a, b,c,d, e"']1883 - program: 'join(" ")'1884 input: '["a",1,2.3,true,null,false]'1885 output: ['"a 1 2.3 true false"']

docs: clarify join behavior on objects (#3555)ded-furbybefore adoption2d410d6

lines 1886–1888why, one step back
1886 - program: 'join(", ")'
1887 input: '{"a":1,"b":2}'1888 output: ['"1, 2"']

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1889–2252why, one step back
1889
1890 - title: "`ascii_downcase`, `ascii_upcase`"1891 body: |18921893 Emit a copy of the input string with its alphabetic characters (a-z and A-Z)1894 converted to the specified case.18951896 examples:1897 - program: 'ascii_upcase'1898 input: '"useful but not for é"'1899 output: ['"USEFUL BUT NOT FOR é"']19001901 - title: "`while(cond; update)`"1902 body: |19031904 The `while(cond; update)` function allows you to repeatedly1905 apply an update to `.` until `cond` is false.19061907 Note that `while(cond; update)` is internally defined as a1908 recursive jq function. Recursive calls within `while` will1909 not consume additional memory if `update` produces at most one1910 output for each input. See advanced topics below.19111912 examples:1913 - program: '[while(.<100; .*2)]'1914 input: '1'1915 output: ['[1,2,4,8,16,32,64]']19161917 - title: "`repeat(exp)`"1918 body: |19191920 The `repeat(exp)` function allows you to repeatedly1921 apply expression `exp` to `.` until an error is raised.19221923 Note that `repeat(exp)` is internally defined as a1924 recursive jq function. Recursive calls within `repeat` will1925 not consume additional memory if `exp` produces at most one1926 output for each input. See advanced topics below.19271928 examples:1929 - program: '[repeat(.*2, error)?]'1930 input: '1'1931 output: ['[2]']19321933 - title: "`until(cond; next)`"1934 body: |19351936 The `until(cond; next)` function allows you to repeatedly1937 apply the expression `next`, initially to `.` then to its own1938 output, until `cond` is true. For example, this can be used1939 to implement a factorial function (see below).19401941 Note that `until(cond; next)` is internally defined as a1942 recursive jq function. Recursive calls within `until()` will1943 not consume additional memory if `next` produces at most one1944 output for each input. See advanced topics below.19451946 examples:1947 - program: '[.,1]|until(.[0] < 1; [.[0] - 1, .[1] * .[0]])|.[1]'1948 input: '4'1949 output: ['24']195019511952 - title: "`recurse(f)`, `recurse`, `recurse(f; condition)`"1953 body: |19541955 The `recurse(f)` function allows you to search through a1956 recursive structure, and extract interesting data from all1957 levels. Suppose your input represents a filesystem:19581959 {"name": "/", "children": [1960 {"name": "/bin", "children": [1961 {"name": "/bin/ls", "children": []},1962 {"name": "/bin/sh", "children": []}]},1963 {"name": "/home", "children": [1964 {"name": "/home/stephen", "children": [1965 {"name": "/home/stephen/jq", "children": []}]}]}]}19661967 Now suppose you want to extract all of the filenames1968 present. You need to retrieve `.name`, `.children[].name`,1969 `.children[].children[].name`, and so on. You can do this1970 with:19711972 recurse(.children[]) | .name19731974 When called without an argument, `recurse` is equivalent to1975 `recurse(.[]?)`.19761977 `recurse(f)` is identical to `recurse(f; true)` and can be1978 used without concerns about recursion depth.19791980 `recurse(f; condition)` is a generator which begins by1981 emitting . and then emits in turn .|f, .|f|f, .|f|f|f, ... so long1982 as the computed value satisfies the condition. For example,1983 to generate all the integers, at least in principle, one1984 could write `recurse(.+1; true)`.19851986 The recursive calls in `recurse` will not consume additional1987 memory whenever `f` produces at most a single output for each1988 input.19891990 examples:1991 - program: 'recurse(.foo[])'1992 input: '{"foo":[{"foo": []}, {"foo":[{"foo":[]}]}]}'1993 output:1994 - '{"foo":[{"foo":[]},{"foo":[{"foo":[]}]}]}'1995 - '{"foo":[]}'1996 - '{"foo":[{"foo":[]}]}'1997 - '{"foo":[]}'19981999 - program: 'recurse'2000 input: '{"a":0,"b":[1]}'

The first 2,000 of 3,912 lines.