Fix manual section titles, minor typos, and improve inline codes (#2626)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by itchyny · committed by GitHub · parent dd5ce98 · Browse files · 5 files·+283 added−311 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+283 added−311 removed
- other:+283 added−311 removed
Provenance
- Commit
6864aa8- Tree
829e6f7- Author
itchyny <itchyny@cybozu.co.jp>- Committer
GitHub <noreply@github.com>- Parent
dd5ce98
5 files changed+283 added−311 removed
modified:docs/content/manual/v1.3/manual.yml+29 added-34 removed
169 unmodified lines170 - title: "`.[foo]`, `.[2]`, `.[10:15]`"171 body: |172 173 You can also look up fields of an object using syntax like174- `.["foo"]` (.foo above is a shorthand version of this). This174+ `.["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.178 105 unmodified lines284 expression that takes an input, ignores it, and returns 42285 instead.286 287 entries:288- - title: Array construction - `[]`288+ - title: "Array construction: `[]`"289 body: |290 291 As in JSON, `[]` is used to construct arrays, as in292 `[1,2,3]`. The elements of the arrays can be any jq16 unmodified lines309 examples:310 - program: "[.user, .projects[]]"311 input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'312 output: ['["stedolan", "jq", "wikiflow"]']313- - title: Objects - `{}`313+ - title: "Objects: `{}`"314 body: |315 316 Like JSON, `{}` is for constructing objects (aka317 dictionaries or hashes), as in: `{"a": 42, "b": 17}`.52 unmodified lines370 371 - title: Builtin operators and functions372 body: |373 374- Some jq operator (for instance, `+`) do different things374+ Some jq operators (for instance, `+`) do different things375 depending on the type of their arguments (arrays, numbers,376 etc.). However, jq never does implicit type conversions. If you377 try to add a string to an object you'll get an error message and378 no result.379 380 entries:381- - title: Addition - `+`381+ - title: "Addition: `+`"382 body: |383 384 The operator `+` takes two filters, applies them both385 to the same input, and adds the results together. What29 unmodified lines415 - program: '{a: 1} + {b: 2} + {c: 3} + {a: 42}'416 input: 'null'417 output: ['{"a": 42, "b": 2, "c": 3}']418 419- - title: Subtraction - `-`419+ - title: "Subtraction: `-`"420 body: |421 422 As well as normal arithmetic subtraction on numbers, the `-`423 operator can be used on arrays to remove all occurrences of6 unmodified lines430 - program: . - ["xml", "yaml"]431 input: '["xml", "yaml", "json"]'432 output: ['["json"]']433 434- - title: Multiplication, division - `*` and `/`434+ - title: "Multiplication, division: `*` and `/`"435 body: |436 437 These operators only work on numbers, and do the expected.438 64 unmodified lines503 - program: 'map(has(2))'504 input: '[[0,1], ["a","b","c"]]'505 output: ['[false, true]']506 507- - title: '`to_entries`, `from_entries`, `with_entries`'507+ - title: "`to_entries`, `from_entries`, `with_entries(f)`"508 body: |509 510 These functions convert between an object and an array of511 key-value pairs. If `to_entries` is passed an object, then512 for each `k: v` entry in the input, the output array513 includes `{"key": k, "value": v}`.514 515 `from_entries` does the opposite conversion, and516- `with_entries(foo)` is a shorthand for `to_entries |517- map(foo) | from_entries`, useful for doing some operation to518- all keys and values of an object.516+ `with_entries(f)` is a shorthand for `to_entries | map(f) |517+ from_entries`, useful for doing some operation to all keys518+ and values of an object.519 520 examples:521 - program: 'to_entries'522 input: '{"a": 1, "b": 2}'35 unmodified lines558 - program: '[1,2,empty,3]'559 input: 'null'560 output: ['[1,2,3]']561 562- - title: '`map(x)`'562+ - title: '`map(f)`'563 body: |564 565- For any filter `x`, `map(x)` will run that filter for each565+ For any filter `f`, `map(f)` will run that filter for each566 element of the input array, and produce the outputs a new567 array. `map(.+1)` will increment each element of an array of numbers.568 569- `map(x)` is equivalent to `[.[] | x]`. In fact, this is how569+ `map(f)` is equivalent to `[.[] | f]`. In fact, this is how570 it's defined.571 572 examples:573 - program: 'map(.+1)'73 unmodified lines647 - program: 'map(type)'648 input: '[0, false, [], {}, null, "hello"]'649 output: ['["number", "boolean", "array", "object", "null", "string"]']650 651- - title: '`sort, sort_by`'651+ - title: '`sort`, `sort_by`'652 body: |653 654 The `sort` functions sorts its input, which must be an655 array. Values are sorted in the following order:139 unmodified lines795 - '{"foo":[{"foo":[]}]}'796 - '{"foo":[]}'797 798 799- - title: String interpolation - `\(foo)`799+ - title: "String interpolation: `\\(exp)`"800 body: |801 802 Inside a string, you can put an expression inside parens803 after a backslash. Whatever the expression returns will be67 unmodified lines871 - program: '@html'872 input: '"This works if x < y"'873 output: ['"This works if x < y"']874 875-# - program: '@html "<span>Anonymous said: \(.)</span>"'876-# input: '"<script>alert(\"lol hax\");</script>"'877-# output: ["<span>Anonymous said: <script>alert("lol hax");</script></span>"]878-875 - program: '@sh "echo \(.)"'876 input: "\"O'Hara's Ale\""877 output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]878 14 unmodified lines893 examples:894 - program: '.[] == 1'895 input: '[1, 1.0, "1", "banana"]'896 output: ['true', 'true', 'false', 'false']897+901- - title: if-then-else898+ - title: if-then-else-end899 body: |900 901 `if A then B else C end` will act the same as `B` if `A`902 produces a value other than false or null, but act the same24 unmodified lines927 end928 input: 2929 output: ['"many"']930 934- - title: '`>, >=, <=, <`'931+ - title: "`>`, `>=`, `<=`, `<`"932 body: |933 934 The comparison operators `>`, `>=`, `<=`, `<` return whether935 their left argument is greater than, greater than or equal6 unmodified lines942 - program: '. < 5'943 input: 2944 output: ['true']945 949- - title: and/or/not946+ - title: "`and`, `or`, `not`"947 body: |948 952- jq supports the normal Boolean operators and/or/not. They have the953- same standard of truth as if expressions - false and null are954- considered "false values", and anything else is a "true value".949+ jq supports the normal Boolean operators `and`, `or`, `not`.950+ They have the same standard of truth as if expressions -951+ `false` and `null` are considered "false values", and952+ anything else is a "true value".953 954 If an operand of one of these operators produces multiple955 results, the operator itself will produce a result for each input.956 1 unmodified line958 so it is called as a filter to which things can be piped959 rather than with special syntax, as in `.foo and .bar |960 not`.961 964- These three only produce the values "true" and "false", and962+ These three only produce the values `true` and `false`, and963 so are only useful for genuine Boolean operations, rather964 than the common Perl/Python/Ruby idiom of965 "value_that_may_be_null or default". If you want to use this966 form of "or", picking between two values rather than969- evaluating a condition, see the "//" operator below.967+ evaluating a condition, see the `//` operator below.968 969 examples:970 - program: '42 and "a string"'971 input: 'null'972 output: ['true']973 - program: '(true, false) or false'974 input: 'null'975 output: ['true', 'false']978-# - program: '(true, false) and (true, false)'979-# input: 'null'980-# output: ['true', 'false', 'false', 'false']976 - program: '(true, true) and (true, false)'977 input: 'null'978 output: ['true', 'false', 'true', 'false']979 - program: '[true, false | not]'980 input: 'null'981 output: ['[false, true]']982 988- - title: Alternative operator - `//`983+ - title: "Alternative operator: `//`"984 body: |985 986 A filter of the form `a // b` produces the same987 results as `a`, if `a` produces results other than `false`67 unmodified lines1055 Suppose we have an array of blog posts, with "author" and "title"1056 fields, and another object which is used to map author usernames to1057 real names. Our input looks like:1058 1064- {"posts": [{"title": "Frist psot", "author": "anon"},1059+ {"posts": [{"title": "First post", "author": "anon"},1060 {"title": "A well-written article", "author": "person1"}],1061 "realnames": {"anon": "Anonymous Coward",1062 "person1": "Person McPherson"}}1063 1064 We want to produce the posts with the author field containing a real1065 name, as in:1066 1072- {"title": "Frist psot", "author": "Anonymous Coward"}1067+ {"title": "First post", "author": "Anonymous Coward"}1068 {"title": "A well-written article", "author": "Person McPherson"}1069 1070 We use a variable, $names, to store the realnames object, so that we1071 can refer to it later when looking up author usernames:194 unmodified linesmodified:docs/content/manual/v1.4/manual.yml+32 added-37 removed
140 unmodified lines141 "\u03bc"). Using this option, you can force jq to produce pure142 ASCII output with every non-ASCII character replaced with the143 equivalent escape sequence.144 145- * `--unbuffered`145+ * `--unbuffered`:146 147 Flush the output after each JSON object is printed (useful if148 you're piping a slow data source into jq and piping jq's149 output elsewhere).15 unmodified lines165 awk's -f option. You can also use '#' to make comments.166 167 * `-e` / `--exit-status`:168 169- Sets the exit status of jq to 0 if the last output values was169+ Sets the exit status of jq to 0 if the last output value was170 neither `false` nor `null`, 1 if the last output value was171 either `false` or `null`, or 4 if no valid result was ever172 produced. Normally jq exits with 2 if there was any usage173 problem or system error, 3 if there was a jq program compile73 unmodified lines247 - program: '[.foo?]'248 input: '[1,2]'249 output: ['[]']250 251- - title: "`.[<string>]`, `.[2]`, `.[10:15]`"251+ - title: "`.[<string>]`, `.[<number>]`, `.[<number>:<number>]`"252 body: |253 254 You can also look up fields of an object using syntax like255- `.["foo"]` (.foo above is a shorthand version of this). This255+ `.["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.259 120 unmodified lines380 expression that takes an input, ignores it, and returns 42381 instead.382 383 entries:384- - title: Array construction - `[]`384+ - title: "Array construction: `[]`"385 body: |386 387 As in JSON, `[]` is used to construct arrays, as in388 `[1,2,3]`. The elements of the arrays can be any jq16 unmodified lines405 examples:406 - program: "[.user, .projects[]]"407 input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'408 output: ['["stedolan", "jq", "wikiflow"]']409- - title: Objects - `{}`409+ - title: "Objects: `{}`"410 body: |411 412 Like JSON, `{}` is for constructing objects (aka413 dictionaries or hashes), as in: `{"a": 42, "b": 17}`.52 unmodified lines466 467 - title: Builtin operators and functions468 body: |469 470- Some jq operator (for instance, `+`) do different things470+ Some jq operators (for instance, `+`) do different things471 depending on the type of their arguments (arrays, numbers,472 etc.). However, jq never does implicit type conversions. If you473 try to add a string to an object you'll get an error message and474 no result.475 476 entries:477- - title: Addition - `+`477+ - title: "Addition: `+`"478 body: |479 480 The operator `+` takes two filters, applies them both481 to the same input, and adds the results together. What30 unmodified lines512 - program: '{a: 1} + {b: 2} + {c: 3} + {a: 42}'513 input: 'null'514 output: ['{"a": 42, "b": 2, "c": 3}']515 516- - title: Subtraction - `-`516+ - title: "Subtraction: `-`"517 body: |518 519 As well as normal arithmetic subtraction on numbers, the `-`520 operator can be used on arrays to remove all occurrences of6 unmodified lines527 - program: . - ["xml", "yaml"]528 input: '["xml", "yaml", "json"]'529 output: ['["json"]']530 531- - title: Multiplication, division, modulo - `*`, `/`, and `%`531+ - title: "Multiplication, division, modulo: `*`, `/`, `%`"532 body: |533 534 These operators only work on numbers, and do the expected.535 95 unmodified lines631 - program: 'del(.[1, 2])'632 input: '["foo", "bar", "baz"]'633 output: ['["foo"]']634 635- - title: "`to_entries`, `from_entries`, `with_entries`"635+ - title: "`to_entries`, `from_entries`, `with_entries(f)`"636 body: |637 638 These functions convert between an object and an array of639 key-value pairs. If `to_entries` is passed an object, then640 for each `k: v` entry in the input, the output array641 includes `{"key": k, "value": v}`.642 643 `from_entries` does the opposite conversion, and644- `with_entries(foo)` is a shorthand for `to_entries |645- map(foo) | from_entries`, useful for doing some operation to646- all keys and values of an object.644+ `with_entries(f)` is a shorthand for `to_entries | map(f) |645+ from_entries`, useful for doing some operation to all keys646+ and values of an object.647 648 examples:649 - program: 'to_entries'650 input: '{"a": 1, "b": 2}'48 unmodified lines699 - program: '[1,2,empty,3]'700 input: 'null'701 output: ['[1,2,3]']702 703- - title: "`map(x)`"703+ - title: "`map(f)`"704 body: |705 706- For any filter `x`, `map(x)` will run that filter for each706+ For any filter `f`, `map(f)` will run that filter for each707 element of the input array, and produce the outputs a new708 array. `map(.+1)` will increment each element of an array of numbers.709 710- `map(x)` is equivalent to `[.[] | x]`. In fact, this is how710+ `map(f)` is equivalent to `[.[] | f]`. In fact, this is how711 it's defined.712 713 examples:714 - program: 'map(.+1)'159 unmodified lines874 - program: 'map(type)'875 input: '[0, false, [], {}, null, "hello"]'876 output: ['["number", "boolean", "array", "object", "null", "string"]']877 878- - title: "`sort, sort_by`"878+ - title: "`sort`, `sort_by`"879 body: |880 881 The `sort` functions sorts its input, which must be an882 array. Values are sorted in the following order:295 unmodified lines1178 - program: '..|.a?'1179 input: '[[{"a":1}]]'1180 output: ['1']1181 1182- - title: "String interpolation - `\\(foo)`"1182+ - title: "String interpolation: `\\(exp)`"1183 body: |1184 1185 Inside a string, you can put an expression inside parens1186 after a backslash. Whatever the expression returns will be86 unmodified lines1273 - program: '@html'1274 input: '"This works if x < y"'1275 output: ['"This works if x < y"']1276 1277-# - program: '@html "<span>Anonymous said: \(.)</span>"'1278-# input: '"<script>alert(\"lol hax\");</script>"'1279-# output: ["<span>Anonymous said: <script>alert("lol hax");</script></span>"]1280-1277 - program: '@sh "echo \(.)"'1278 input: "\"O'Hara's Ale\""1279 output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]1280 14 unmodified lines1295 examples:1296 - program: '.[] == 1'1297 input: '[1, 1.0, "1", "banana"]'1298 output: ['true', 'true', 'false', 'false']1299+1303- - title: if-then-else1300+ - title: if-then-else-end1301 body: |1302 1303 `if A then B else C end` will act the same as `B` if `A`1304 produces a value other than false or null, but act the same24 unmodified lines1329 end1330 input: 21331 output: ['"many"']1332 1336- - title: "`>, >=, <=, <`"1333+ - title: "`>`, `>=`, `<=`, `<`"1334 body: |1335 1336 The comparison operators `>`, `>=`, `<=`, `<` return whether1337 their left argument is greater than, greater than or equal6 unmodified lines1344 - program: '. < 5'1345 input: 21346 output: ['true']1347 1351- - title: and/or/not1348+ - title: "`and`, `or`, `not`"1349 body: |1350 1354- jq supports the normal Boolean operators and/or/not. They have the1355- same standard of truth as if expressions - false and null are1356- considered "false values", and anything else is a "true value".1351+ jq supports the normal Boolean operators `and`, `or`, `not`.1352+ They have the same standard of truth as if expressions -1353+ `false` and `null` are considered "false values", and1354+ anything else is a "true value".1355 1356 If an operand of one of these operators produces multiple1357 results, the operator itself will produce a result for each input.1358 1 unmodified line1360 so it is called as a filter to which things can be piped1361 rather than with special syntax, as in `.foo and .bar |1362 not`.1363 1366- These three only produce the values "true" and "false", and1364+ These three only produce the values `true` and `false`, and1365 so are only useful for genuine Boolean operations, rather1366 than the common Perl/Python/Ruby idiom of1367 "value_that_may_be_null or default". If you want to use this1368 form of "or", picking between two values rather than1371- evaluating a condition, see the "//" operator below.1369+ evaluating a condition, see the `//` operator below.1370 1371 examples:1372 - program: '42 and "a string"'1373 input: 'null'1374 output: ['true']1375 - program: '(true, false) or false'1376 input: 'null'1377 output: ['true', 'false']1380-# - program: '(true, false) and (true, false)'1381-# input: 'null'1382-# output: ['true', 'false', 'false', 'false']1378 - program: '(true, true) and (true, false)'1379 input: 'null'1380 output: ['true', 'false', 'true', 'false']1381 - program: '[true, false | not]'1382 input: 'null'1383 output: ['[false, true]']1384 1390- - title: Alternative operator - `//`1385+ - title: "Alternative operator: `//`"1386 body: |1387 1388 A filter of the form `a // b` produces the same1389 results as `a`, if `a` produces results other than `false`67 unmodified lines1457 Suppose we have an array of blog posts, with "author" and "title"1458 fields, and another object which is used to map author usernames to1459 real names. Our input looks like:1460 1466- {"posts": [{"title": "Frist psot", "author": "anon"},1461+ {"posts": [{"title": "First post", "author": "anon"},1462 {"title": "A well-written article", "author": "person1"}],1463 "realnames": {"anon": "Anonymous Coward",1464 "person1": "Person McPherson"}}1465 1466 We want to produce the posts with the author field containing a real1467 name, as in:1468 1474- {"title": "Frist psot", "author": "Anonymous Coward"}1469+ {"title": "First post", "author": "Anonymous Coward"}1470 {"title": "A well-written article", "author": "Person McPherson"}1471 1472 We use a variable, $names, to store the realnames object, so that we1473 can refer to it later when looking up author usernames:194 unmodified linesmodified:docs/content/manual/v1.5/manual.yml+52 added-58 removed
105 unmodified lines106 RS (record separator) character is printed before each value on107 output and an ASCII LF (line feed) is printed after every108 output. Input JSON texts that fail to parse are ignored (but109 warned about), discarding all subsequent input until the next110- RS. This more also parses the output of jq without the `--seq`110+ RS. This mode also parses the output of jq without the `--seq`111 option.112 113 * `--stream`:114 115 Parse the input in streaming fashion, outputting arrays of path116 and leaf values (scalars and empty arrays or empty objects).117 For example, `"a"` becomes `[[],"a"]`, and `[[],"a",["b"]]`118- becomes `[[0],[]]`, `[[1],"a"]`, and `[[1,0],"b"]`.118+ becomes `[[0],[]]`, `[[1],"a"]`, and `[[2,0],"b"]`.119 120 This is useful for processing very large inputs. Use this in121 conjunction with filtering and the `reduce` and `foreach` syntax122 to reduce large inputs incrementally.44 unmodified lines167 "\u03bc"). Using this option, you can force jq to produce pure168 ASCII output with every non-ASCII character replaced with the169 equivalent escape sequence.170 171- * `--unbuffered`171+ * `--unbuffered`:172 173 Flush the output after each JSON object is printed (useful if174 you're piping a slow data source into jq and piping jq's175 output elsewhere).25 unmodified lines201 section on modules below.202 203 * `-e` / `--exit-status`:204 205- Sets the exit status of jq to 0 if the last output values was205+ Sets the exit status of jq to 0 if the last output value was206 neither `false` nor `null`, 1 if the last output value was207 either `false` or `null`, or 4 if no valid result was ever208 produced. Normally jq exits with 2 if there was any usage209 problem or system error, 3 if there was a jq program compile103 unmodified lines313 - program: '[.foo?]'314 input: '[1,2]'315 output: ['[]']316 317- - title: "`.[<string>]`, `.[2]`, `.[10:15]`"317+ - title: "`.[<string>]`, `.[<number>]`, `.[<number>:<number>]`"318 body: |319 320 You can also look up fields of an object using syntax like321- `.["foo"]` (.foo above is a shorthand version of this). This321+ `.["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.325 135 unmodified lines461 expression that takes an input, ignores it, and returns 42462 instead.463 464 entries:465- - title: Array construction - `[]`465+ - title: "Array construction: `[]`"466 body: |467 468 As in JSON, `[]` is used to construct arrays, as in469 `[1,2,3]`. The elements of the arrays can be any jq16 unmodified lines486 examples:487 - program: "[.user, .projects[]]"488 input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'489 output: ['["stedolan", "jq", "wikiflow"]']490- - title: Objects - `{}`490+ - title: "Objects: `{}`"491 body: |492 493 Like JSON, `{}` is for constructing objects (aka494 dictionaries or hashes), as in: `{"a": 42, "b": 17}`.52 unmodified lines547 548 - title: Builtin operators and functions549 body: |550 551- Some jq operator (for instance, `+`) do different things551+ Some jq operators (for instance, `+`) do different things552 depending on the type of their arguments (arrays, numbers,553 etc.). However, jq never does implicit type conversions. If you554 try to add a string to an object you'll get an error message and555 no result.556 557 entries:558- - title: Addition - `+`558+ - title: "Addition: `+`"559 body: |560 561 The operator `+` takes two filters, applies them both562 to the same input, and adds the results together. What30 unmodified lines593 - program: '{a: 1} + {b: 2} + {c: 3} + {a: 42}'594 input: 'null'595 output: ['{"a": 42, "b": 2, "c": 3}']596 597- - title: Subtraction - `-`597+ - title: "Subtraction: `-`"598 body: |599 600 As well as normal arithmetic subtraction on numbers, the `-`601 operator can be used on arrays to remove all occurrences of6 unmodified lines608 - program: . - ["xml", "yaml"]609 input: '["xml", "yaml", "json"]'610 output: ['["json"]']611 612- - title: Multiplication, division, modulo - `*`, `/`, and `%`612+ - title: "Multiplication, division, modulo: `*`, `/`, `%`"613 body: |614 615 These infix operators behave as expected when given two numbers.616 Division by zero raises an error. `x % y` computes x modulo y.152 unmodified lines769 - program: 'del(.[1, 2])'770 input: '["foo", "bar", "baz"]'771 output: ['["foo"]']772 773- - title: "`to_entries`, `from_entries`, `with_entries`"773+ - title: "`to_entries`, `from_entries`, `with_entries(f)`"774 body: |775 776 These functions convert between an object and an array of777 key-value pairs. If `to_entries` is passed an object, then778 for each `k: v` entry in the input, the output array779 includes `{"key": k, "value": v}`.780 781 `from_entries` does the opposite conversion, and782- `with_entries(foo)` is a shorthand for `to_entries |783- map(foo) | from_entries`, useful for doing some operation to784- all keys and values of an object. `from_entries` accepts key, Key,785- Name, value and Value as keys.782+ `with_entries(f)` is a shorthand for `to_entries | map(f) |783+ from_entries`, useful for doing some operation to all keys784+ and values of an object. `from_entries` accepts `"key"`,785+ `"Key"`, `"Name"`, `"value"`, and `"Value"` as keys.786 787 examples:788 - program: 'to_entries'789 input: '{"a": 1, "b": 2}'8 unmodified lines798 799 - title: "`select(boolean_expression)`"800 body: |801 802- The function `select(foo)` produces its input unchanged if803- `foo` returns true for that input, and produces no output802+ The function `select(f)` produces its input unchanged if803+ `f` returns true for that input, and produces no output804 otherwise.805 806 It's useful for filtering lists: `[1,2,3] | map(select(. >= 2))`807 will give you `[2,3]`.53 unmodified lines861 - program: 'try error("\($__loc__)") catch .'862 input: 'null'863 output: ['"{\"file\":\"<top-level>\",\"line\":1}"']864 865- - title: "`map(x)`, `map_values(x)`"865+ - title: "`map(f)`, `map_values(f)`"866 body: |867 868- For any filter `x`, `map(x)` will run that filter for each868+ For any filter `f`, `map(f)` will run that filter for each869 element of the input array, and return the outputs in a new870 array. `map(.+1)` will increment each element of an array of numbers.871 872- Similarly, `map_values(x)` will run that filter for each element,872+ Similarly, `map_values(f)` will run that filter for each element,873 but it will return an object when an object is passed.874 875- `map(x)` is equivalent to `[.[] | x]`. In fact, this is how876- it's defined. Similarly, `map_values(x)` is defined as `.[] |= x`.875+ `map(f)` is equivalent to `[.[] | f]`. In fact, this is how876+ it's defined. Similarly, `map_values(f)` is defined as `.[] |= f`.877 878 examples:879 - program: 'map(.+1)'880 input: '[1,2,3]'123 unmodified lines1004 - program: flatten1005 input: '[{"foo": "bar"}, [{"foo": "baz"}]]'1006 output: ['[{"foo": "bar"}, {"foo": "baz"}]']1007 1008- - title: "`range(upto)`, `range(from;upto)` `range(from;upto;by)`"1008+ - title: "`range(upto)`, `range(from; upto)` `range(from; upto; by)`"1009 body: |1010 1011- The `range` function produces a range of numbers. `range(4;10)`1011+ The `range` function produces a range of numbers. `range(4; 10)`1012 produces 6 numbers, from 4 (inclusive) to 10 (exclusive). The numbers1013- are produced as separate outputs. Use `[range(4;10)]` to get a range as1013+ are produced as separate outputs. Use `[range(4; 10)]` to get a range as1014 an array.1015 1016 The one argument form generates numbers from 0 to the given1017 number, with an increment of 1.4 unmodified lines1022 The three argument form generates numbers `from` to `upto`1023 with an increment of `by`.1024 1025 examples:1026- - program: 'range(2;4)'1026+ - program: 'range(2; 4)'1027 input: 'null'1028 output: ['2', '3']1029- - program: '[range(2;4)]'1029+ - program: '[range(2; 4)]'1030 input: 'null'1031 output: ['[2,3]']1032 - program: '[range(4)]'1033 input: 'null'1034 output: ['[0,1,2,3]']1035- - program: '[range(0;10;3)]'1035+ - program: '[range(0; 10; 3)]'1036 input: 'null'1037 output: ['[0,3,6,9]']1038- - program: '[range(0;10;-1)]'1038+ - program: '[range(0; 10; -1)]'1039 input: 'null'1040 output: ['[]']1041- - program: '[range(0;-5;-1)]'1041+ - program: '[range(0; -5; -1)]'1042 input: 'null'1043 output: ['[0,-1,-2,-3,-4]']1044 1045 - title: "`floor`"75 unmodified lines1121 - program: 'infinite, nan | type'1122 input: 'null'1123 output: ['"number"', '"number"']1124 1125- - title: "`sort, sort_by(path_expression)`"1125+ - title: "`sort`, `sort_by(path_expression)`"1126 body: |1127 1128 The `sort` functions sorts its input, which must be an1129 array. Values are sorted in the following order:13 unmodified lines1143 1144 `sort` may be used to sort by a particular field of an1145 object, or by applying any jq filter.1146 1147- `sort_by(foo)` compares two elements by comparing the result of1148- `foo` on each element.1147+ `sort_by(f)` compares two elements by comparing the result of1148+ `f` on each element.1149 1150 examples:1151 - program: 'sort'1152 input: '[8,3,null,6]'406 unmodified lines1559 1560 - title: "`bsearch(x)`"1561 body: |1562 1563- bsearch(x) conducts a binary search for x in the input1563+ `bsearch(x)` conducts a binary search for x in the input1564 array. If the input is sorted and contains x, then1565- bsearch(x) will return its index in the array; otherwise, if1565+ `bsearch(x)` will return its index in the array; otherwise, if1566 the array is sorted, it will return (-1 - ix) where ix is an1567 insertion point such that the array would still be sorted1568 after the insertion of x at ix. If the array is not sorted,1569- bsearch(x) will return an integer that is probably of no1569+ `bsearch(x)` will return an integer that is probably of no1570 interest.1571 1572 examples:1573 - program: 'bsearch(0)'5 unmodified lines1579 - program: 'bsearch(4) as $ix | if $ix < 0 then .[-(1+$ix)] = 4 else . end'1580 input: '[1,2,3]'1581 output: ['[1,2,3,4]']1582 1583- - title: "String interpolation - `\\(foo)`"1583+ - title: "String interpolation: `\\(exp)`"1584 body: |1585 1586 Inside a string, you can put an expression inside parens1587 after a backslash. Whatever the expression returns will be96 unmodified lines1684 - program: '@html'1685 input: '"This works if x < y"'1686 output: ['"This works if x < y"']1687 1688-# - program: '@html "<span>Anonymous said: \(.)</span>"'1689-# input: '"<script>alert(\"lol hax\");</script>"'1690-# output: ["<span>Anonymous said: <script>alert("lol hax");</script></span>"]1691-1688 - program: '@sh "echo \(.)"'1689 input: "\"O'Hara's Ale\""1690 output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]1691 82 unmodified lines1774 - program: '.[] == 1'1775 input: '[1, 1.0, "1", "banana"]'1776 output: ['true', 'true', 'false', 'false']1777 1782- - title: if-then-else1778+ - title: if-then-else-end1779 body: |1780 1781 `if A then B else C end` will act the same as `B` if `A`1782 produces a value other than false or null, but act the same24 unmodified lines1807 end1808 input: 21809 output: ['"many"']1810 1815- - title: "`>, >=, <=, <`"1811+ - title: "`>`, `>=`, `<=`, `<`"1812 body: |1813 1814 The comparison operators `>`, `>=`, `<=`, `<` return whether1815 their left argument is greater than, greater than or equal6 unmodified lines1822 - program: '. < 5'1823 input: 21824 output: ['true']1825 1830- - title: and/or/not1826+ - title: "`and`, `or`, `not`"1827 body: |1828 1833- jq supports the normal Boolean operators and/or/not. They have the1834- same standard of truth as if expressions - false and null are1835- considered "false values", and anything else is a "true value".1829+ jq supports the normal Boolean operators `and`, `or`, `not`.1830+ They have the same standard of truth as if expressions -1831+ `false` and `null` are considered "false values", and1832+ anything else is a "true value".1833 1834 If an operand of one of these operators produces multiple1835 results, the operator itself will produce a result for each input.1836 1 unmodified line1838 so it is called as a filter to which things can be piped1839 rather than with special syntax, as in `.foo and .bar |1840 not`.1841 1845- These three only produce the values "true" and "false", and1842+ These three only produce the values `true` and `false`, and1843 so are only useful for genuine Boolean operations, rather1844 than the common Perl/Python/Ruby idiom of1845 "value_that_may_be_null or default". If you want to use this1846 form of "or", picking between two values rather than1850- evaluating a condition, see the "//" operator below.1847+ evaluating a condition, see the `//` operator below.1848 1849 examples:1850 - program: '42 and "a string"'1851 input: 'null'1852 output: ['true']1853 - program: '(true, false) or false'1854 input: 'null'1855 output: ['true', 'false']1859-# - program: '(true, false) and (true, false)'1860-# input: 'null'1861-# output: ['true', 'false', 'false', 'false']1856 - program: '(true, true) and (true, false)'1857 input: 'null'1858 output: ['true', 'false', 'true', 'false']1859 - program: '[true, false | not]'1860 input: 'null'1861 output: ['[false, true]']1862 1869- - title: Alternative operator - `//`1863+ - title: "Alternative operator: `//`"1864 body: |1865 1866 A filter of the form `a // b` produces the same1867 results as `a`, if `a` produces results other than `false`325 unmodified lines2193 Suppose we have an array of blog posts, with "author" and "title"2194 fields, and another object which is used to map author usernames to2195 real names. Our input looks like:2196 2203- {"posts": [{"title": "Frist psot", "author": "anon"},2197+ {"posts": [{"title": "First post", "author": "anon"},2198 {"title": "A well-written article", "author": "person1"}],2199 "realnames": {"anon": "Anonymous Coward",2200 "person1": "Person McPherson"}}2201 2202 We want to produce the posts with the author field containing a real2203 name, as in:2204 2211- {"title": "Frist psot", "author": "Anonymous Coward"}2205+ {"title": "First post", "author": "Anonymous Coward"}2206 {"title": "A well-written article", "author": "Person McPherson"}2207 2208 We use a variable, $names, to store the realnames object, so that we2209 can refer to it later when looking up author usernames:5 unmodified lines2215 with `$x` set to that value. Thus `as` functions as something of a2216 foreach loop.2217 2218 Just as `{foo}` is a handy way of writing `{foo: .foo}`, so2225- `{$foo}` is a handy way of writing `{foo:$foo}`.2219+ `{$foo}` is a handy way of writing `{foo: $foo}`.2220 2221 Multiple variables may be declared using a single `as` expression by2222 providing a pattern that matches the structure of the input2223 (this is known as "destructuring"):650 unmodified linesmodified:docs/content/manual/v1.6/manual.yml+96 added-103 removed
93 unmodified lines94 the Powershell (`powershell.exe`) or the Powershell Core95 (`pwsh`/`pwsh.exe`), use single-quote characters around the jq96 program and backslash-escaped double-quotes (`\"`) inside the jq97 program.98- 99- Unix shells: `jq '.["foo"]'`100- Powershell: `jq '.[\"foo\"]'`101- Windows command shell: `jq ".[\"foo\"]"`98 99+ * Unix shells: `jq '.["foo"]'`100+ * Powershell: `jq '.[\"foo\"]'`101+ * Windows command shell: `jq ".[\"foo\"]"`102+103 You can affect how jq reads and writes its input and output104 using some command-line options:105 106 * `--version`:15 unmodified lines122 123 Parse the input in streaming fashion, outputting arrays of path124 and leaf values (scalars and empty arrays or empty objects).125 For example, `"a"` becomes `[[],"a"]`, and `[[],"a",["b"]]`126- becomes `[[0],[]]`, `[[1],"a"]`, and `[[1,0],"b"]`.126+ becomes `[[0],[]]`, `[[1],"a"]`, and `[[2,0],"b"]`.127 128 This is useful for processing very large inputs. Use this in129 conjunction with filtering and the `reduce` and `foreach` syntax130 to reduce large inputs incrementally.47 unmodified lines178 "\u03bc"). Using this option, you can force jq to produce pure179 ASCII output with every non-ASCII character replaced with the180 equivalent escape sequence.181 182- * `--unbuffered`182+ * `--unbuffered`:183 184 Flush the output after each JSON object is printed (useful if185 you're piping a slow data source into jq and piping jq's186 output elsewhere).25 unmodified lines212 section on modules below.213 214 * `-e` / `--exit-status`:215 216- Sets the exit status of jq to 0 if the last output values was216+ Sets the exit status of jq to 0 if the last output value was217 neither `false` nor `null`, 1 if the last output value was218 either `false` or `null`, or 4 if no valid result was ever219 produced. Normally jq exits with 2 if there was any usage220 problem or system error, 3 if there was a jq program compile135 unmodified lines356 - program: '[.foo?]'357 input: '[1,2]'358 output: ['[]']359 360- - title: "Generic Object Index: `.[<string>]`"360+ - title: "Object Index: `.[<string>]`"361 body: |362 363 You can also look up fields of an object using syntax like364- `.["foo"]` (.foo above is a shorthand version of this, but364+ `.["foo"]` (`.foo` above is a shorthand version of this, but365 only for identifier-like strings).366 367- - title: "Array Index: `.[2]`"367+ - title: "Array Index: `.[<number>]`"368 body: |369 370- When the index value is an integer, `.[<value>]` can index370+ When the index value is an integer, `.[<number>]` can index371 arrays. Arrays are zero-based, so `.[2]` returns the third372 element.373 374 Negative indices are allowed, with -1 referring to the last11 unmodified lines386 - program: '.[-2]'387 input: '[1,2,3]'388 output: ['2']389 390- - title: "Array/String Slice: `.[10:15]`"390+ - title: "Array/String Slice: `.[<number>:<number>]`"391 body: |392 393- The `.[10:15]` syntax can be used to return a subarray of an394- array or substring of a string. The array returned by395- `.[10:15]` will be of length 5, containing the elements from396- index 10 (inclusive) to index 15 (exclusive). Either index may397- be negative (in which case it counts backwards from the end of398- the array), or omitted (in which case it refers to the start399- or end of the array).393+ The `.[<number>:<number>]` syntax can be used to return a394+ subarray of an array or substring of a string. The array395+ returned by `.[10:15]` will be of length 5, containing the396+ 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 401 examples:402 - program: '.[2:4]'403 input: '["a","b","c","d","e"]'249 unmodified lines653 654 - **Strings** are added by being joined into a larger string.655 656 - **Objects** are added by merging, that is, inserting all657- the key-value pairs from both objects into a single658- combined object. If both objects contain a value for the659- same key, the object on the right of the `+` wins. (For660- recursive merge use the `*` operator.)657+ the key-value pairs from both objects into a single658+ combined object. If both objects contain a value for the659+ same key, the object on the right of the `+` wins. (For660+ recursive merge use the `*` operator.)661 662 `null` can be added to any value, and returns the other663 value unchanged.664 28 unmodified lines693 - program: . - ["xml", "yaml"]694 input: '["xml", "yaml", "json"]'695 output: ['["json"]']696 697- - title: "Multiplication, division, modulo: `*`, `/`, and `%`"697+ - title: "Multiplication, division, modulo: `*`, `/`, `%`"698 body: |699 700 These infix operators behave as expected when given two numbers.701 Division by zero raises an error. `x % y` computes x modulo y.122 unmodified lines824 - program: 'map(in([0,1]))'825 input: '[2, 0]'826 output: ['[false, true]']827 828- - title: "`map(x)`, `map_values(x)`"828+ - title: "`map(f)`, `map_values(f)`"829 body: |830 831- For any filter `x`, `map(x)` will run that filter for each831+ For any filter `f`, `map(f)` will run that filter for each832 element of the input array, and return the outputs in a new833 array. `map(.+1)` will increment each element of an array of numbers.834 835- Similarly, `map_values(x)` will run that filter for each element,835+ Similarly, `map_values(f)` will run that filter for each element,836 but it will return an object when an object is passed.837 838- `map(x)` is equivalent to `[.[] | x]`. In fact, this is how839- it's defined. Similarly, `map_values(x)` is defined as `.[] |= x`.838+ `map(f)` is equivalent to `[.[] | f]`. In fact, this is how839+ it's defined. Similarly, `map_values(f)` is defined as `.[] |= f`.840 841 examples:842 - program: 'map(.+1)'843 input: '[1,2,3]'90 unmodified lines934 - program: 'delpaths([["a","b"]])'935 input: '{"a":{"b":1},"x":{"y":2}}'936 output: ['{"a":{},"x":{"y":2}}']937 938- - title: "`to_entries`, `from_entries`, `with_entries`"938+ - title: "`to_entries`, `from_entries`, `with_entries(f)`"939 body: |940 941 These functions convert between an object and an array of942 key-value pairs. If `to_entries` is passed an object, then943 for each `k: v` entry in the input, the output array944 includes `{"key": k, "value": v}`.945 946- `from_entries` does the opposite conversion, and947- `with_entries(foo)` is a shorthand for `to_entries |948- map(foo) | from_entries`, useful for doing some operation to949- all keys and values of an object. `from_entries` accepts key, Key,950- name, Name, value and Value as keys.946+ `from_entries` does the opposite conversion, and `with_entries(f)`947+ is a shorthand for `to_entries | map(f) | from_entries`, useful for948+ doing some operation to all keys and values of an object.949+ `from_entries` accepts `"key"`, `"Key"`, `"name"`, `"Name"`,950+ `"value"`, and `"Value"` as keys.951 952 examples:953 - program: 'to_entries'954 input: '{"a": 1, "b": 2}'8 unmodified lines963 964 - title: "`select(boolean_expression)`"965 body: |966 967- The function `select(foo)` produces its input unchanged if968- `foo` returns true for that input, and produces no output967+ The function `select(f)` produces its input unchanged if968+ `f` returns true for that input, and produces no output969 otherwise.970 971 It's useful for filtering lists: `[1,2,3] | map(select(. >= 2))`972 will give you `[2,3]`.192 unmodified lines1165 - program: flatten1166 input: '[{"foo": "bar"}, [{"foo": "baz"}]]'1167 output: ['[{"foo": "bar"}, {"foo": "baz"}]']1168 1169- - title: "`range(upto)`, `range(from;upto)` `range(from;upto;by)`"1169+ - title: "`range(upto)`, `range(from; upto)`, `range(from; upto; by)`"1170 body: |1171 1172- The `range` function produces a range of numbers. `range(4;10)`1172+ The `range` function produces a range of numbers. `range(4; 10)`1173 produces 6 numbers, from 4 (inclusive) to 10 (exclusive). The numbers1174- are produced as separate outputs. Use `[range(4;10)]` to get a range as1174+ are produced as separate outputs. Use `[range(4; 10)]` to get a range as1175 an array.1176 1177 The one argument form generates numbers from 0 to the given1178 number, with an increment of 1.4 unmodified lines1183 The three argument form generates numbers `from` to `upto`1184 with an increment of `by`.1185 1186 examples:1187- - program: 'range(2;4)'1187+ - program: 'range(2; 4)'1188 input: 'null'1189 output: ['2', '3']1190- - program: '[range(2;4)]'1190+ - program: '[range(2; 4)]'1191 input: 'null'1192 output: ['[2,3]']1193 - program: '[range(4)]'1194 input: 'null'1195 output: ['[0,1,2,3]']1196- - program: '[range(0;10;3)]'1196+ - program: '[range(0; 10; 3)]'1197 input: 'null'1198 output: ['[0,3,6,9]']1199- - program: '[range(0;10;-1)]'1199+ - program: '[range(0; 10; -1)]'1200 input: 'null'1201 output: ['[]']1202- - program: '[range(0;-5;-1)]'1202+ - program: '[range(0; -5; -1)]'1203 input: 'null'1204 output: ['[0,-1,-2,-3,-4]']1205 1206 - title: "`floor`"75 unmodified lines1282 - program: 'infinite, nan | type'1283 input: 'null'1284 output: ['"number"', '"number"']1285 1286- - title: "`sort, sort_by(path_expression)`"1286+ - title: "`sort`, `sort_by(path_expression)`"1287 body: |1288 1289 The `sort` functions sorts its input, which must be an1290 array. Values are sorted in the following order:13 unmodified lines1304 1305 `sort` may be used to sort by a particular field of an1306 object, or by applying any jq filter.1307 1308- `sort_by(foo)` compares two elements by comparing the result of1309- `foo` on each element.1308+ `sort_by(f)` compares two elements by comparing the result of1309+ `f` on each element.1310 1311 examples:1312 - program: 'sort'1313 input: '[8,3,null,6]'72 unmodified lines1386 - title: "`contains(element)`"1387 body: |1388 1389 The filter `contains(b)` will produce true if b is1390- completely contained within the input. String B is1390+ completely contained within the input. A string B is1391 contained in a string A if B is a substring of A. An array B1392 is contained in an array A if all elements in B are1393 contained in any element in A. An object B is contained in1394 object A if all of the values in B are contained in the353 unmodified lines1748 1749 - title: "`bsearch(x)`"1750 body: |1751 1752- bsearch(x) conducts a binary search for x in the input1752+ `bsearch(x)` conducts a binary search for x in the input1753 array. If the input is sorted and contains x, then1754- bsearch(x) will return its index in the array; otherwise, if1754+ `bsearch(x)` will return its index in the array; otherwise, if1755 the array is sorted, it will return (-1 - ix) where ix is an1756 insertion point such that the array would still be sorted1757 after the insertion of x at ix. If the array is not sorted,1758- bsearch(x) will return an integer that is probably of no1758+ `bsearch(x)` will return an integer that is probably of no1759 interest.1760 1761 examples:1762 - program: 'bsearch(0)'5 unmodified lines1768 - program: 'bsearch(4) as $ix | if $ix < 0 then .[-(1+$ix)] = 4 else . end'1769 input: '[1,2,3]'1770 output: ['[1,2,3,4]']1771 1772- - title: "String interpolation - `\\(foo)`"1772+ - title: "String interpolation: `\\(exp)`"1773 body: |1774 1775 Inside a string, you can put an expression inside parens1776 after a backslash. Whatever the expression returns will be7 unmodified lines1784 - title: "Convert to/from JSON"1785 body: |1786 1787 The `tojson` and `fromjson` builtins dump values as JSON texts1788- or parse JSON texts into values, respectively. The tojson1789- builtin differs from tostring in that tostring returns strings1790- unmodified, while tojson encodes strings as JSON strings.1788+ or parse JSON texts into values, respectively. The `tojson`1789+ builtin differs from `tostring` in that `tostring` returns strings1790+ unmodified, while `tojson` encodes strings as JSON strings.1791 1792 examples:1793 - program: '[.[]|tostring]'1794 input: '[1, "foo", ["foo"]]'83 unmodified lines1878 - program: '@html'1879 input: '"This works if x < y"'1880 output: ['"This works if x < y"']1881 1882-# - program: '@html "<span>Anonymous said: \(.)</span>"'1883-# input: '"<script>alert(\"lol hax\");</script>"'1884-# output: ["<span>Anonymous said: <script>alert("lol hax");</script></span>"]1885-1882 - program: '@sh "echo \(.)"'1883 input: "\"O'Hara's Ale\""1884 output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]1885 84 unmodified lines1970 body: |1971 1972 jq provides a few SQL-style operators.1973 1978- * INDEX(stream; index_expression):1974+ * INDEX(stream; index_expression):1975 1980- This builtin produces an object whose keys are computed by1981- the given index expression applied to each value from the1982- given stream.1976+ This builtin produces an object whose keys are computed by1977+ the given index expression applied to each value from the1978+ given stream.1979 1984- * JOIN($idx; stream; idx_expr; join_expr):1980+ * JOIN($idx; stream; idx_expr; join_expr):1981 1986- This builtin joins the values from the given stream to the1987- given index. The index's keys are computed by applying the1988- given index expression to each value from the given stream.1989- An array of the value in the stream and the corresponding1990- value from the index is fed to the given join expression to1991- produce each result.1982+ This builtin joins the values from the given stream to the1983+ given index. The index's keys are computed by applying the1984+ given index expression to each value from the given stream.1985+ An array of the value in the stream and the corresponding1986+ value from the index is fed to the given join expression to1987+ produce each result.1988 1993- * JOIN($idx; stream; idx_expr):1989+ * JOIN($idx; stream; idx_expr):1990 1995- Same as `JOIN($idx; stream; idx_expr; .)`.1991+ Same as `JOIN($idx; stream; idx_expr; .)`.1992 1997- * JOIN($idx; idx_expr):1993+ * JOIN($idx; idx_expr):1994 1999- This builtin joins the input `.` to the given index, applying2000- the given index expression to `.` to compute the index key.2001- The join operation is as described above.1995+ This builtin joins the input `.` to the given index, applying1996+ the given index expression to `.` to compute the index key.1997+ The join operation is as described above.1998 2003- * IN(s):1999+ * IN(s):2000 2005- This builtin outputs `true` if `.` appears in the given2006- stream, otherwise it outputs `false`.2001+ This builtin outputs `true` if `.` appears in the given2002+ stream, otherwise it outputs `false`.2003 2008- * IN(source; s):2004+ * IN(source; s):2005 2010- This builtin outputs `true` if any value in the source stream2011- appears in the second stream, otherwise it outputs `false`.2006+ This builtin outputs `true` if any value in the source stream2007+ appears in the second stream, otherwise it outputs `false`.2008 2009 - title: "`builtins`"2010 body: |2011 20 unmodified lines2032 - program: '.[] == 1'2033 input: '[1, 1.0, "1", "banana"]'2034 output: ['true', 'true', 'false', 'false']2035 2040- - title: if-then-else2036+ - title: if-then-else-end2037 body: |2038 2039 `if A then B else C end` will act the same as `B` if `A`2040 produces a value other than false or null, but act the same1 unmodified line2042 2043 Checking for false or null is a simpler notion of2044 "truthiness" than is found in JavaScript or Python, but it2045 means that you'll sometimes have to be more explicit about2050- the condition you want: you can't test whether, e.g. a2046+ the condition you want. You can't test whether, e.g. a2047 string is empty using `if .name then A else B end`, you'll2052- need something more like `if (.name | length) > 0 then A else2053- B end` instead.2048+ need something more like `if .name == "" then A else B end` instead.2049 2050 If the condition `A` produces multiple results, then `B` is evaluated2051 once for each result that is not false or null, and `C` is evaluated2052 once for each false or null.11 unmodified lines2064 end2065 input: 22066 output: ['"many"']2067 2073- - title: "`>, >=, <=, <`"2068+ - title: "`>`, `>=`, `<=`, `<`"2069 body: |2070 2071 The comparison operators `>`, `>=`, `<=`, `<` return whether2072 their left argument is greater than, greater than or equal6 unmodified lines2079 - program: '. < 5'2080 input: 22081 output: ['true']2082 2088- - title: and/or/not2083+ - title: "`and`, `or`, `not`"2084 body: |2085 2091- jq supports the normal Boolean operators and/or/not. They have the2092- same standard of truth as if expressions - false and null are2093- considered "false values", and anything else is a "true value".2086+ jq supports the normal Boolean operators `and`, `or`, `not`. They2087+ have the same standard of truth as if expressions - `false` and2088+ `null` are considered "false values", and anything else is a "true2089+ value".2090 2091 If an operand of one of these operators produces multiple2092 results, the operator itself will produce a result for each input.2093 1 unmodified line2095 so it is called as a filter to which things can be piped2096 rather than with special syntax, as in `.foo and .bar |2097 not`.2098 2103- These three only produce the values "true" and "false", and2099+ These three only produce the values `true` and `false`, and2100 so are only useful for genuine Boolean operations, rather2101 than the common Perl/Python/Ruby idiom of2102 "value_that_may_be_null or default". If you want to use this2103 form of "or", picking between two values rather than2108- evaluating a condition, see the "//" operator below.2104+ evaluating a condition, see the `//` operator below.2105 2106 examples:2107 - program: '42 and "a string"'2108 input: 'null'2109 output: ['true']2110 - program: '(true, false) or false'2111 input: 'null'2112 output: ['true', 'false']2117-# - program: '(true, false) and (true, false)'2118-# input: 'null'2119-# output: ['true', 'false', 'false', 'false']2113 - program: '(true, true) and (true, false)'2114 input: 'null'2115 output: ['true', 'false', 'true', 'false']2116 - program: '[true, false | not]'241 unmodified lines2358 - program: 'splits(", *")'2359 input: '("ab,cd", "ef, gh")'2360 output: ['"ab"', '"cd"', '"ef"', '"gh"']2361 2369- - title: "`sub(regex; tostring)` `sub(regex; string; flags)`"2362+ - title: "`sub(regex; tostring)`, `sub(regex; string; flags)`"2363 body: |2364 2365 Emit the string obtained by replacing the first match of regex in the2366 input string with `tostring`, after interpolation. `tostring` should83 unmodified lines2450 Suppose we have an array of blog posts, with "author" and "title"2451 fields, and another object which is used to map author usernames to2452 real names. Our input looks like:2453 2461- {"posts": [{"title": "Frist psot", "author": "anon"},2454+ {"posts": [{"title": "First post", "author": "anon"},2455 {"title": "A well-written article", "author": "person1"}],2456 "realnames": {"anon": "Anonymous Coward",2457 "person1": "Person McPherson"}}2458 2459 We want to produce the posts with the author field containing a real2460 name, as in:2461 2469- {"title": "Frist psot", "author": "Anonymous Coward"}2462+ {"title": "First post", "author": "Anonymous Coward"}2463 {"title": "A well-written article", "author": "Person McPherson"}2464 2465 We use a variable, $names, to store the realnames object, so that we2466 can refer to it later when looking up author usernames:5 unmodified lines2472 with `$x` set to that value. Thus `as` functions as something of a2473 foreach loop.2474 2475 Just as `{foo}` is a handy way of writing `{foo: .foo}`, so2483- `{$foo}` is a handy way of writing `{foo:$foo}`.2476+ `{$foo}` is a handy way of writing `{foo: $foo}`.2477 2478 Multiple variables may be declared using a single `as` expression by2479 providing a pattern that matches the structure of the input2480 (this is known as "destructuring"):152 unmodified lines2633 to be able to create recursive functions.2634 2635 For example, in the following expression there is a binding2636 which is visible "to the right" of it, `... | .*3 as2644- $times_three | [. + $times_three] | ...`, but not "to the2637+ $times_three | [. + $times_three] | ...`, but not "to the2638 left". Consider this expression now, `... | (.*3 as2646- $times_three | [.+ $times_three]) | ...`: here the binding2639+ $times_three | [. + $times_three]) | ...`: here the binding2640 `$times_three` is _not_ visible past the closing parenthesis.2641 2642 - title: Reduce2643 body: |652 unmodified linesmodified:docs/content/manual/manual.yml+74 added-79 removed
95 unmodified lines96 (`pwsh`/`pwsh.exe`), use single-quote characters around the jq97 program and backslash-escaped double-quotes (`\"`) inside the jq98 program.99 100- Unix shells: `jq '.["foo"]'`101- Powershell: `jq '.[\"foo\"]'`102- Windows command shell: `jq ".[\"foo\"]"`100+ * Unix shells: `jq '.["foo"]'`101+ * Powershell: `jq '.[\"foo\"]'`102+ * Windows command shell: `jq ".[\"foo\"]"`103 104 You can affect how jq reads and writes its input and output105 using some command-line options:106 176 unmodified lines283 * `--jsonargs`:284 285 Remaining arguments are positional JSON text arguments. These286 are available to the jq program as `$ARGS.positional[]`.287- 287+288 * `--`:289 290 Terminates argument processing. Remaining arguments are291 positional, either strings, JSON texts, or input filenames,25 unmodified lines317 Since jq by default pretty-prints all output, this trivial318 program can be a useful way of formatting JSON output from,319 say, `curl`.320 321- An important point about the identity filter is that it 322- guarantees to preserve the literal decimal representation 323- of values. This is particularly important when dealing with numbers 324- which can't be losslessly converted to an IEEE754 double precision 321+ An important point about the identity filter is that it322+ guarantees to preserve the literal decimal representation323+ of values. This is particularly important when dealing with numbers324+ which can't be losslessly converted to an IEEE754 double precision325 representation.326 327 jq doesn't truncate the literal numbers to double unless there328 is a need to make arithmetic operations with the number.329- Comparisons are carried out over the untruncated big decimal 329+ Comparisons are carried out over the untruncated big decimal330 representation of the number.331 332 jq will also try to maintain the original decimal precision of the provided333- number literal. See below for examples. 333+ number literal. See below for examples.334 335 examples:336 - program: '.'337 input: '"Hello, world!"'61 unmodified lines399 - program: '[.foo?]'400 input: '[1,2]'401 output: ['[]']402 403- - title: "Generic Object Index: `.[<string>]`"403+ - title: "Object Index: `.[<string>]`"404 body: |405 406 You can also look up fields of an object using syntax like407- `.["foo"]` (.foo above is a shorthand version of this, but407+ `.["foo"]` (`.foo` above is a shorthand version of this, but408 only for identifier-like strings).409 410- - title: "Array Index: `.[2]`"410+ - title: "Array Index: `.[<number>]`"411 body: |412 413- When the index value is an integer, `.[<value>]` can index413+ When the index value is an integer, `.[<number>]` can index414 arrays. Arrays are zero-based, so `.[2]` returns the third415 element.416 417 Negative indices are allowed, with -1 referring to the last11 unmodified lines429 - program: '.[-2]'430 input: '[1,2,3]'431 output: ['2']432 433- - title: "Array/String Slice: `.[10:15]`"433+ - title: "Array/String Slice: `.[<number>:<number>]`"434 body: |435 436- The `.[10:15]` syntax can be used to return a subarray of an437- array or substring of a string. The array returned by438- `.[10:15]` will be of length 5, containing the elements from439- index 10 (inclusive) to index 15 (exclusive). Either index may440- be negative (in which case it counts backwards from the end of441- the array), or omitted (in which case it refers to the start442- or end of the array).436+ The `.[<number>:<number>]` syntax can be used to return a437+ subarray of an array or substring of a string. The array438+ returned by `.[10:15]` will be of length 5, containing the439+ elements from index 10 (inclusive) to index 15 (exclusive).440+ Either index may be negative (in which case it counts441+ backwards from the end of the array), or omitted (in which442+ case it refers to the start or end of the array).443 444 examples:445 - program: '.[2:4]'446 input: '["a","b","c","d","e"]'116 unmodified lines563 values take an input and produce an output - `42` is a valid jq564 expression that takes an input, ignores it, and returns 42565 instead.566 567- Numbers in jq are internally represented by their IEEE754 double 568- precision approximation. Any arithmetic operation with numbers, 569- whether they are literals or results of previous filters, will 567+ Numbers in jq are internally represented by their IEEE754 double568+ precision approximation. Any arithmetic operation with numbers,569+ whether they are literals or results of previous filters, will570 produce a double precision floating point result.571 572 However, when parsing a literal jq will store the original literal573 string. If no mutation is applied to this value then it will make574- to the output in its original form, even if conversion to double 575- would result in a loss. 574+ to the output in its original form, even if conversion to double575+ would result in a loss.576 577 entries:578 - title: "Array construction: `[]`"579 body: |118 unmodified lines698 699 - title: Builtin operators and functions700 body: |701 702- Some jq operator (for instance, `+`) do different things702+ Some jq operators (for instance, `+`) do different things703 depending on the type of their arguments (arrays, numbers,704 etc.). However, jq never does implicit type conversions. If you705 try to add a string to an object you'll get an error message and706 no result.707 708- Please note that all numbers are converted to IEEE754 double precision 708+ Please note that all numbers are converted to IEEE754 double precision709 floating point representation. Arithmetic and logical operators are working710- with these converted doubles. Results of all such operations are also limited 711- to the double precision. 710+ with these converted doubles. Results of all such operations are also limited711+ to the double precision.712 713- The only exception to this behaviour of number is a snapshot of original number 713+ The only exception to this behaviour of number is a snapshot of original number714 literal. When a number which originally was provided as a literal is never715- mutated until the end of the program then it is printed to the output in its 716- original literal form. This also includes cases when the original literal 715+ mutated until the end of the program then it is printed to the output in its716+ original literal form. This also includes cases when the original literal717 would be truncated when converted to the IEEE754 double precision floating point718 number.719 720 entries:50 unmodified lines771 - program: . - ["xml", "yaml"]772 input: '["xml", "yaml", "json"]'773 output: ['["json"]']774 775- - title: "Multiplication, division, modulo: `*`, `/`, and `%`"775+ - title: "Multiplication, division, modulo: `*`, `/`, `%`"776 body: |777 778 These infix operators behave as expected when given two numbers.779 Division by zero raises an error. `x % y` computes x modulo y.122 unmodified lines902 - program: 'map(in([0,1]))'903 input: '[2, 0]'904 output: ['[false, true]']905 906- - title: "`map(x)`, `map_values(x)`"906+ - title: "`map(f)`, `map_values(f)`"907 body: |908 909- For any filter `x`, `map(x)` will run that filter for each909+ For any filter `f`, `map(f)` will run that filter for each910 element of the input array, and return the outputs in a new911 array. `map(.+1)` will increment each element of an array of numbers.912 913- Similarly, `map_values(x)` will run that filter for each element,913+ Similarly, `map_values(f)` will run that filter for each element,914 but it will return an object when an object is passed.915 916- `map(x)` is equivalent to `[.[] | x]`. In fact, this is how917- it's defined. Similarly, `map_values(x)` is defined as `.[] |= x`.916+ `map(f)` is equivalent to `[.[] | f]`. In fact, this is how917+ it's defined. Similarly, `map_values(f)` is defined as `.[] |= f`.918 919 examples:920 - program: 'map(.+1)'921 input: '[1,2,3]'90 unmodified lines1012 - program: 'delpaths([["a","b"]])'1013 input: '{"a":{"b":1},"x":{"y":2}}'1014 output: ['{"a":{},"x":{"y":2}}']1015 1016- - title: "`to_entries`, `from_entries`, `with_entries`"1016+ - title: "`to_entries`, `from_entries`, `with_entries(f)`"1017 body: |1018 1019 These functions convert between an object and an array of1020 key-value pairs. If `to_entries` is passed an object, then1021 for each `k: v` entry in the input, the output array1022 includes `{"key": k, "value": v}`.1023 1024- `from_entries` does the opposite conversion, and1025- `with_entries(foo)` is a shorthand for `to_entries |1026- map(foo) | from_entries`, useful for doing some operation to1027- all keys and values of an object. `from_entries` accepts key, Key,1028- name, Name, value and Value as keys.1024+ `from_entries` does the opposite conversion, and `with_entries(f)`1025+ is a shorthand for `to_entries | map(f) | from_entries`, useful for1026+ doing some operation to all keys and values of an object.1027+ `from_entries` accepts `"key"`, `"Key"`, `"name"`, `"Name"`,1028+ `"value"`, and `"Value"` as keys.1029 1030 examples:1031 - program: 'to_entries'1032 input: '{"a": 1, "b": 2}'8 unmodified lines1041 1042 - title: "`select(boolean_expression)`"1043 body: |1044 1045- The function `select(foo)` produces its input unchanged if1046- `foo` returns true for that input, and produces no output1045+ The function `select(f)` produces its input unchanged if1046+ `f` returns true for that input, and produces no output1047 otherwise.1048 1049 It's useful for filtering lists: `[1,2,3] | map(select(. >= 2))`1050 will give you `[2,3]`.192 unmodified lines1243 - program: flatten1244 input: '[{"foo": "bar"}, [{"foo": "baz"}]]'1245 output: ['[{"foo": "bar"}, {"foo": "baz"}]']1246 1247- - title: "`range(upto)`, `range(from;upto)`, `range(from;upto;by)`"1247+ - title: "`range(upto)`, `range(from; upto)`, `range(from; upto; by)`"1248 body: |1249 1250- The `range` function produces a range of numbers. `range(4;10)`1250+ The `range` function produces a range of numbers. `range(4; 10)`1251 produces 6 numbers, from 4 (inclusive) to 10 (exclusive). The numbers1252- are produced as separate outputs. Use `[range(4;10)]` to get a range as1252+ are produced as separate outputs. Use `[range(4; 10)]` to get a range as1253 an array.1254 1255 The one argument form generates numbers from 0 to the given1256 number, with an increment of 1.4 unmodified lines1261 The three argument form generates numbers `from` to `upto`1262 with an increment of `by`.1263 1264 examples:1265- - program: 'range(2;4)'1265+ - program: 'range(2; 4)'1266 input: 'null'1267 output: ['2', '3']1268- - program: '[range(2;4)]'1268+ - program: '[range(2; 4)]'1269 input: 'null'1270 output: ['[2,3]']1271 - program: '[range(4)]'1272 input: 'null'1273 output: ['[0,1,2,3]']1274- - program: '[range(0;10;3)]'1274+ - program: '[range(0; 10; 3)]'1275 input: 'null'1276 output: ['[0,3,6,9]']1277- - program: '[range(0;10;-1)]'1277+ - program: '[range(0; 10; -1)]'1278 input: 'null'1279 output: ['[]']1280- - program: '[range(0;-5;-1)]'1280+ - program: '[range(0; -5; -1)]'1281 input: 'null'1282 output: ['[0,-1,-2,-3,-4]']1283 1284 - title: "`floor`"97 unmodified lines1382 1383 `sort` may be used to sort by a particular field of an1384 object, or by applying any jq filter.1385 1386- `sort_by(foo)` compares two elements by comparing the result of1387- `foo` on each element.1386+ `sort_by(f)` compares two elements by comparing the result of1387+ `f` on each element.1388 1389 examples:1390 - program: 'sort'1391 input: '[8,3,null,6]'473 unmodified lines1865 - program: 'bsearch(4) as $ix | if $ix < 0 then .[-(1+$ix)] = 4 else . end'1866 input: '[1,2,3]'1867 output: ['[1,2,3,4]']1868 1869- - title: "String interpolation - `\\(foo)`"1869+ - title: "String interpolation: `\\(exp)`"1870 body: |1871 1872 Inside a string, you can put an expression inside parens1873 after a backslash. Whatever the expression returns will be101 unmodified lines1975 - program: '@html'1976 input: '"This works if x < y"'1977 output: ['"This works if x < y"']1978 1979-# - program: '@html "<span>Anonymous said: \(.)</span>"'1980-# input: '"<script>alert(\"lol hax\");</script>"'1981-# output: ["<span>Anonymous said: <script>alert("lol hax");</script></span>"]1982-1979 - program: '@sh "echo \(.)"'1980 input: "\"O'Hara's Ale\""1981 output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]1982 146 unmodified lines2129 - program: '.[] == 1'2130 input: '[1, 1.0, "1", "banana"]'2131 output: ['true', 'true', 'false', 'false']2132 2137- - title: if-then-else2133+ - title: if-then-else-end2134 body: |2135 2136 `if A then B else C end` will act the same as `B` if `A`2137 produces a value other than false or null, but act the same16 unmodified lines2154 2155 More cases can be added to an if using `elif A then B` syntax.2156 2157 examples:2162- - program: 'if . == 0 then2158+ - program: |-2159+ if . == 0 then2160 "zero"2161 elif . == 1 then2162 "one"2163 else2164 "many"2168- end'2165+ end2166 input: 22167 output: ['"many"']2168 2172- - title: "`>, >=, <=, <`"2169+ - title: "`>`, `>=`, `<=`, `<`"2170 body: |2171 2172 The comparison operators `>`, `>=`, `<=`, `<` return whether2173 their left argument is greater than, greater than or equal6 unmodified lines2180 - program: '. < 5'2181 input: 22182 output: ['true']2183 2187- - title: and/or/not2184+ - title: "`and`, `or`, `not`"2185 body: |2186 2190- jq supports the normal Boolean operators and/or/not. They have the2191- same standard of truth as if expressions - false and null are2192- considered "false values", and anything else is a "true value".2187+ jq supports the normal Boolean operators `and`, `or`, `not`.2188+ They have the same standard of truth as if expressions -2189+ `false` and `null` are considered "false values", and2190+ anything else is a "true value".2191 2192 If an operand of one of these operators produces multiple2193 results, the operator itself will produce a result for each input.2194 1 unmodified line2196 so it is called as a filter to which things can be piped2197 rather than with special syntax, as in `.foo and .bar |2198 not`.2199 2202- These three only produce the values "true" and "false", and2200+ These three only produce the values `true` and `false`, and2201 so are only useful for genuine Boolean operations, rather2202 than the common Perl/Python/Ruby idiom of2203 "value_that_may_be_null or default". If you want to use this2204 form of "or", picking between two values rather than2207- evaluating a condition, see the "//" operator below.2205+ evaluating a condition, see the `//` operator below.2206 2207 examples:2208 - program: '42 and "a string"'2209 input: 'null'2210 output: ['true']2211 - program: '(true, false) or false'2212 input: 'null'2213 output: ['true', 'false']2216-# - program: '(true, false) and (true, false)'2217-# input: 'null'2218-# output: ['true', 'false', 'false', 'false']2214 - program: '(true, true) and (true, false)'2215 input: 'null'2216 output: ['true', 'false', 'true', 'false']2217 - program: '[true, false | not]'344 unmodified lines2562 Suppose we have an array of blog posts, with "author" and "title"2563 fields, and another object which is used to map author usernames to2564 real names. Our input looks like:2565 2571- {"posts": [{"title": "Frist psot", "author": "anon"},2566+ {"posts": [{"title": "First post", "author": "anon"},2567 {"title": "A well-written article", "author": "person1"}],2568 "realnames": {"anon": "Anonymous Coward",2569 "person1": "Person McPherson"}}2570 2571 We want to produce the posts with the author field containing a real2572 name, as in:2573 2579- {"title": "Frist psot", "author": "Anonymous Coward"}2574+ {"title": "First post", "author": "Anonymous Coward"}2575 {"title": "A well-written article", "author": "Person McPherson"}2576 2577 We use a variable, $names, to store the realnames object, so that we2578 can refer to it later when looking up author usernames:5 unmodified lines2584 with `$x` set to that value. Thus `as` functions as something of a2585 foreach loop.2586 2587 Just as `{foo}` is a handy way of writing `{foo: .foo}`, so2593- `{$foo}` is a handy way of writing `{foo:$foo}`.2588+ `{$foo}` is a handy way of writing `{foo: $foo}`.2589 2590 Multiple variables may be declared using a single `as` expression by2591 providing a pattern that matches the structure of the input2592 (this is known as "destructuring"):834 unmodified lines