Skip to content
Myr

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

5 files+283 added−311 removed

modified:docs/content/manual/v1.3/manual.yml+29 added-34 removed
169 unmodified lines
170 - title: "`.[foo]`, `.[2]`, `.[10:15]`"171 body: |172173 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 lines
284 expression that takes an input, ignores it, and returns 42285 instead.286287 entries:288 - title: Array construction - `[]`288 - title: "Array construction: `[]`"289 body: |290291 As in JSON, `[]` is used to construct arrays, as in292 `[1,2,3]`. The elements of the arrays can be any jq
16 unmodified lines
309 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: |315316 Like JSON, `{}` is for constructing objects (aka317 dictionaries or hashes), as in: `{"a": 42, "b": 17}`.
52 unmodified lines
370371 - title: Builtin operators and functions372 body: |373374 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.379380 entries:381 - title: Addition - `+`381 - title: "Addition: `+`"382 body: |383384 The operator `+` takes two filters, applies them both385 to the same input, and adds the results together. What
29 unmodified lines
415 - program: '{a: 1} + {b: 2} + {c: 3} + {a: 42}'416 input: 'null'417 output: ['{"a": 42, "b": 2, "c": 3}']418419 - title: Subtraction - `-`419 - title: "Subtraction: `-`"420 body: |421422 As well as normal arithmetic subtraction on numbers, the `-`423 operator can be used on arrays to remove all occurrences of
6 unmodified lines
430 - program: . - ["xml", "yaml"]431 input: '["xml", "yaml", "json"]'432 output: ['["json"]']433434 - title: Multiplication, division - `*` and `/`434 - title: "Multiplication, division: `*` and `/`"435 body: |436437 These operators only work on numbers, and do the expected.438
64 unmodified lines
503 - program: 'map(has(2))'504 input: '[[0,1], ["a","b","c"]]'505 output: ['[false, true]']506507 - title: '`to_entries`, `from_entries`, `with_entries`'507 - title: "`to_entries`, `from_entries`, `with_entries(f)`"508 body: |509510 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}`.514515 `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.519520 examples:521 - program: 'to_entries'522 input: '{"a": 1, "b": 2}'
35 unmodified lines
558 - program: '[1,2,empty,3]'559 input: 'null'560 output: ['[1,2,3]']561562 - title: '`map(x)`'562 - title: '`map(f)`'563 body: |564565 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.568569 `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.571572 examples:573 - program: 'map(.+1)'
73 unmodified lines
647 - program: 'map(type)'648 input: '[0, false, [], {}, null, "hello"]'649 output: ['["number", "boolean", "array", "object", "null", "string"]']650651 - title: '`sort, sort_by`'651 - title: '`sort`, `sort_by`'652 body: |653654 The `sort` functions sorts its input, which must be an655 array. Values are sorted in the following order:
139 unmodified lines
795 - '{"foo":[{"foo":[]}]}'796 - '{"foo":[]}'797798799 - title: String interpolation - `\(foo)`799 - title: "String interpolation: `\\(exp)`"800 body: |801802 Inside a string, you can put an expression inside parens803 after a backslash. Whatever the expression returns will be
67 unmodified lines
871 - program: '@html'872 input: '"This works if x < y"'873 output: ['"This works if x &lt; y"']874875# - program: '@html "<span>Anonymous said: \(.)</span>"'876# input: '"<script>alert(\"lol hax\");</script>"'877# output: ["<span>Anonymous said: &lt;script&gt;alert(&quot;lol hax&quot;);&lt;/script&gt;</span>"]878875 - program: '@sh "echo \(.)"'876 input: "\"O'Hara's Ale\""877 output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]878
14 unmodified lines
893 examples:894 - program: '.[] == 1'895 input: '[1, 1.0, "1", "banana"]'896 output: ['true', 'true', 'false', 'false']897901 - title: if-then-else898 - title: if-then-else-end899 body: |900901 `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 same
24 unmodified lines
927 end928 input: 2929 output: ['"many"']930934 - title: '`>, >=, <=, <`'931 - title: "`>`, `>=`, `<=`, `<`"932 body: |933934 The comparison operators `>`, `>=`, `<=`, `<` return whether935 their left argument is greater than, greater than or equal
6 unmodified lines
942 - program: '. < 5'943 input: 2944 output: ['true']945949 - title: and/or/not946 - title: "`and`, `or`, `not`"947 body: |948952 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".953954 If an operand of one of these operators produces multiple955 results, the operator itself will produce a result for each input.956
1 unmodified line
958 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`.961964 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.968969 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]']982988 - title: Alternative operator - `//`983 - title: "Alternative operator: `//`"984 body: |985986 A filter of the form `a // b` produces the same987 results as `a`, if `a` produces results other than `false`
67 unmodified lines
1055 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:10581064 {"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"}}10631064 We want to produce the posts with the author field containing a real1065 name, as in:10661072 {"title": "Frist psot", "author": "Anonymous Coward"}1067 {"title": "First post", "author": "Anonymous Coward"}1068 {"title": "A well-written article", "author": "Person McPherson"}10691070 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 lines
modified:docs/content/manual/v1.4/manual.yml+32 added-37 removed
140 unmodified lines
141 "\u03bc"). Using this option, you can force jq to produce pure142 ASCII output with every non-ASCII character replaced with the143 equivalent escape sequence.144145 * `--unbuffered`145 * `--unbuffered`:146147 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 lines
165 awk's -f option. You can also use '#' to make comments.166167 * `-e` / `--exit-status`:168169 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 compile
73 unmodified lines
247 - program: '[.foo?]'248 input: '[1,2]'249 output: ['[]']250251 - title: "`.[<string>]`, `.[2]`, `.[10:15]`"251 - title: "`.[<string>]`, `.[<number>]`, `.[<number>:<number>]`"252 body: |253254 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 lines
380 expression that takes an input, ignores it, and returns 42381 instead.382383 entries:384 - title: Array construction - `[]`384 - title: "Array construction: `[]`"385 body: |386387 As in JSON, `[]` is used to construct arrays, as in388 `[1,2,3]`. The elements of the arrays can be any jq
16 unmodified lines
405 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: |411412 Like JSON, `{}` is for constructing objects (aka413 dictionaries or hashes), as in: `{"a": 42, "b": 17}`.
52 unmodified lines
466467 - title: Builtin operators and functions468 body: |469470 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.475476 entries:477 - title: Addition - `+`477 - title: "Addition: `+`"478 body: |479480 The operator `+` takes two filters, applies them both481 to the same input, and adds the results together. What
30 unmodified lines
512 - program: '{a: 1} + {b: 2} + {c: 3} + {a: 42}'513 input: 'null'514 output: ['{"a": 42, "b": 2, "c": 3}']515516 - title: Subtraction - `-`516 - title: "Subtraction: `-`"517 body: |518519 As well as normal arithmetic subtraction on numbers, the `-`520 operator can be used on arrays to remove all occurrences of
6 unmodified lines
527 - program: . - ["xml", "yaml"]528 input: '["xml", "yaml", "json"]'529 output: ['["json"]']530531 - title: Multiplication, division, modulo - `*`, `/`, and `%`531 - title: "Multiplication, division, modulo: `*`, `/`, `%`"532 body: |533534 These operators only work on numbers, and do the expected.535
95 unmodified lines
631 - program: 'del(.[1, 2])'632 input: '["foo", "bar", "baz"]'633 output: ['["foo"]']634635 - title: "`to_entries`, `from_entries`, `with_entries`"635 - title: "`to_entries`, `from_entries`, `with_entries(f)`"636 body: |637638 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}`.642643 `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.647648 examples:649 - program: 'to_entries'650 input: '{"a": 1, "b": 2}'
48 unmodified lines
699 - program: '[1,2,empty,3]'700 input: 'null'701 output: ['[1,2,3]']702703 - title: "`map(x)`"703 - title: "`map(f)`"704 body: |705706 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.709710 `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.712713 examples:714 - program: 'map(.+1)'
159 unmodified lines
874 - program: 'map(type)'875 input: '[0, false, [], {}, null, "hello"]'876 output: ['["number", "boolean", "array", "object", "null", "string"]']877878 - title: "`sort, sort_by`"878 - title: "`sort`, `sort_by`"879 body: |880881 The `sort` functions sorts its input, which must be an882 array. Values are sorted in the following order:
295 unmodified lines
1178 - program: '..|.a?'1179 input: '[[{"a":1}]]'1180 output: ['1']11811182 - title: "String interpolation - `\\(foo)`"1182 - title: "String interpolation: `\\(exp)`"1183 body: |11841185 Inside a string, you can put an expression inside parens1186 after a backslash. Whatever the expression returns will be
86 unmodified lines
1273 - program: '@html'1274 input: '"This works if x < y"'1275 output: ['"This works if x &lt; y"']12761277# - program: '@html "<span>Anonymous said: \(.)</span>"'1278# input: '"<script>alert(\"lol hax\");</script>"'1279# output: ["<span>Anonymous said: &lt;script&gt;alert(&quot;lol hax&quot;);&lt;/script&gt;</span>"]12801277 - program: '@sh "echo \(.)"'1278 input: "\"O'Hara's Ale\""1279 output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]1280
14 unmodified lines
1295 examples:1296 - program: '.[] == 1'1297 input: '[1, 1.0, "1", "banana"]'1298 output: ['true', 'true', 'false', 'false']12991303 - title: if-then-else1300 - title: if-then-else-end1301 body: |13021303 `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 same
24 unmodified lines
1329 end1330 input: 21331 output: ['"many"']13321336 - title: "`>, >=, <=, <`"1333 - title: "`>`, `>=`, `<=`, `<`"1334 body: |13351336 The comparison operators `>`, `>=`, `<=`, `<` return whether1337 their left argument is greater than, greater than or equal
6 unmodified lines
1344 - program: '. < 5'1345 input: 21346 output: ['true']13471351 - title: and/or/not1348 - title: "`and`, `or`, `not`"1349 body: |13501354 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".13551356 If an operand of one of these operators produces multiple1357 results, the operator itself will produce a result for each input.1358
1 unmodified line
1360 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`.13631366 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.13701371 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]']13841390 - title: Alternative operator - `//`1385 - title: "Alternative operator: `//`"1386 body: |13871388 A filter of the form `a // b` produces the same1389 results as `a`, if `a` produces results other than `false`
67 unmodified lines
1457 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:14601466 {"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"}}14651466 We want to produce the posts with the author field containing a real1467 name, as in:14681474 {"title": "Frist psot", "author": "Anonymous Coward"}1469 {"title": "First post", "author": "Anonymous Coward"}1470 {"title": "A well-written article", "author": "Person McPherson"}14711472 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 lines
modified:docs/content/manual/v1.5/manual.yml+52 added-58 removed
105 unmodified lines
106 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.112113 * `--stream`:114115 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"]`.119120 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 lines
167 "\u03bc"). Using this option, you can force jq to produce pure168 ASCII output with every non-ASCII character replaced with the169 equivalent escape sequence.170171 * `--unbuffered`171 * `--unbuffered`:172173 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 lines
201 section on modules below.202203 * `-e` / `--exit-status`:204205 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 compile
103 unmodified lines
313 - program: '[.foo?]'314 input: '[1,2]'315 output: ['[]']316317 - title: "`.[<string>]`, `.[2]`, `.[10:15]`"317 - title: "`.[<string>]`, `.[<number>]`, `.[<number>:<number>]`"318 body: |319320 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 lines
461 expression that takes an input, ignores it, and returns 42462 instead.463464 entries:465 - title: Array construction - `[]`465 - title: "Array construction: `[]`"466 body: |467468 As in JSON, `[]` is used to construct arrays, as in469 `[1,2,3]`. The elements of the arrays can be any jq
16 unmodified lines
486 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: |492493 Like JSON, `{}` is for constructing objects (aka494 dictionaries or hashes), as in: `{"a": 42, "b": 17}`.
52 unmodified lines
547548 - title: Builtin operators and functions549 body: |550551 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.556557 entries:558 - title: Addition - `+`558 - title: "Addition: `+`"559 body: |560561 The operator `+` takes two filters, applies them both562 to the same input, and adds the results together. What
30 unmodified lines
593 - program: '{a: 1} + {b: 2} + {c: 3} + {a: 42}'594 input: 'null'595 output: ['{"a": 42, "b": 2, "c": 3}']596597 - title: Subtraction - `-`597 - title: "Subtraction: `-`"598 body: |599600 As well as normal arithmetic subtraction on numbers, the `-`601 operator can be used on arrays to remove all occurrences of
6 unmodified lines
608 - program: . - ["xml", "yaml"]609 input: '["xml", "yaml", "json"]'610 output: ['["json"]']611612 - title: Multiplication, division, modulo - `*`, `/`, and `%`612 - title: "Multiplication, division, modulo: `*`, `/`, `%`"613 body: |614615 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 lines
769 - program: 'del(.[1, 2])'770 input: '["foo", "bar", "baz"]'771 output: ['["foo"]']772773 - title: "`to_entries`, `from_entries`, `with_entries`"773 - title: "`to_entries`, `from_entries`, `with_entries(f)`"774 body: |775776 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}`.780781 `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.786787 examples:788 - program: 'to_entries'789 input: '{"a": 1, "b": 2}'
8 unmodified lines
798799 - title: "`select(boolean_expression)`"800 body: |801802 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.805806 It's useful for filtering lists: `[1,2,3] | map(select(. >= 2))`807 will give you `[2,3]`.
53 unmodified lines
861 - program: 'try error("\($__loc__)") catch .'862 input: 'null'863 output: ['"{\"file\":\"<top-level>\",\"line\":1}"']864865 - title: "`map(x)`, `map_values(x)`"865 - title: "`map(f)`, `map_values(f)`"866 body: |867868 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.871872 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.874875 `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`.877878 examples:879 - program: 'map(.+1)'880 input: '[1,2,3]'
123 unmodified lines
1004 - program: flatten1005 input: '[{"foo": "bar"}, [{"foo": "baz"}]]'1006 output: ['[{"foo": "bar"}, {"foo": "baz"}]']10071008 - title: "`range(upto)`, `range(from;upto)` `range(from;upto;by)`"1008 - title: "`range(upto)`, `range(from; upto)` `range(from; upto; by)`"1009 body: |10101011 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.10151016 The one argument form generates numbers from 0 to the given1017 number, with an increment of 1.
4 unmodified lines
1022 The three argument form generates numbers `from` to `upto`1023 with an increment of `by`.10241025 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]']10441045 - title: "`floor`"
75 unmodified lines
1121 - program: 'infinite, nan | type'1122 input: 'null'1123 output: ['"number"', '"number"']11241125 - title: "`sort, sort_by(path_expression)`"1125 - title: "`sort`, `sort_by(path_expression)`"1126 body: |11271128 The `sort` functions sorts its input, which must be an1129 array. Values are sorted in the following order:
13 unmodified lines
11431144 `sort` may be used to sort by a particular field of an1145 object, or by applying any jq filter.11461147 `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.11491150 examples:1151 - program: 'sort'1152 input: '[8,3,null,6]'
406 unmodified lines
15591560 - title: "`bsearch(x)`"1561 body: |15621563 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.15711572 examples:1573 - program: 'bsearch(0)'
5 unmodified lines
1579 - 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]']15821583 - title: "String interpolation - `\\(foo)`"1583 - title: "String interpolation: `\\(exp)`"1584 body: |15851586 Inside a string, you can put an expression inside parens1587 after a backslash. Whatever the expression returns will be
96 unmodified lines
1684 - program: '@html'1685 input: '"This works if x < y"'1686 output: ['"This works if x &lt; y"']16871688# - program: '@html "<span>Anonymous said: \(.)</span>"'1689# input: '"<script>alert(\"lol hax\");</script>"'1690# output: ["<span>Anonymous said: &lt;script&gt;alert(&quot;lol hax&quot;);&lt;/script&gt;</span>"]16911688 - program: '@sh "echo \(.)"'1689 input: "\"O'Hara's Ale\""1690 output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]1691
82 unmodified lines
1774 - program: '.[] == 1'1775 input: '[1, 1.0, "1", "banana"]'1776 output: ['true', 'true', 'false', 'false']17771782 - title: if-then-else1778 - title: if-then-else-end1779 body: |17801781 `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 same
24 unmodified lines
1807 end1808 input: 21809 output: ['"many"']18101815 - title: "`>, >=, <=, <`"1811 - title: "`>`, `>=`, `<=`, `<`"1812 body: |18131814 The comparison operators `>`, `>=`, `<=`, `<` return whether1815 their left argument is greater than, greater than or equal
6 unmodified lines
1822 - program: '. < 5'1823 input: 21824 output: ['true']18251830 - title: and/or/not1826 - title: "`and`, `or`, `not`"1827 body: |18281833 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".18331834 If an operand of one of these operators produces multiple1835 results, the operator itself will produce a result for each input.1836
1 unmodified line
1838 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`.18411845 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.18481849 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]']18621869 - title: Alternative operator - `//`1863 - title: "Alternative operator: `//`"1864 body: |18651866 A filter of the form `a // b` produces the same1867 results as `a`, if `a` produces results other than `false`
325 unmodified lines
2193 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:21962203 {"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"}}22012202 We want to produce the posts with the author field containing a real2203 name, as in:22042211 {"title": "Frist psot", "author": "Anonymous Coward"}2205 {"title": "First post", "author": "Anonymous Coward"}2206 {"title": "A well-written article", "author": "Person McPherson"}22072208 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 lines
2215 with `$x` set to that value. Thus `as` functions as something of a2216 foreach loop.22172218 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}`.22202221 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 lines
modified:docs/content/manual/v1.6/manual.yml+96 added-103 removed
93 unmodified lines
94 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\"]"`9899 * Unix shells: `jq '.["foo"]'`100 * Powershell: `jq '.[\"foo\"]'`101 * Windows command shell: `jq ".[\"foo\"]"`102103 You can affect how jq reads and writes its input and output104 using some command-line options:105106 * `--version`:
15 unmodified lines
122123 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"]`.127128 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 lines
178 "\u03bc"). Using this option, you can force jq to produce pure179 ASCII output with every non-ASCII character replaced with the180 equivalent escape sequence.181182 * `--unbuffered`182 * `--unbuffered`:183184 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 lines
212 section on modules below.213214 * `-e` / `--exit-status`:215216 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 compile
135 unmodified lines
356 - program: '[.foo?]'357 input: '[1,2]'358 output: ['[]']359360 - title: "Generic Object Index: `.[<string>]`"360 - title: "Object Index: `.[<string>]`"361 body: |362363 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).366367 - title: "Array Index: `.[2]`"367 - title: "Array Index: `.[<number>]`"368 body: |369370 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.373374 Negative indices are allowed, with -1 referring to the last
11 unmodified lines
386 - program: '.[-2]'387 input: '[1,2,3]'388 output: ['2']389390 - title: "Array/String Slice: `.[10:15]`"390 - title: "Array/String Slice: `.[<number>:<number>]`"391 body: |392393 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).400401 examples:402 - program: '.[2:4]'403 input: '["a","b","c","d","e"]'
249 unmodified lines
653654 - **Strings** are added by being joined into a larger string.655656 - **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.)661662 `null` can be added to any value, and returns the other663 value unchanged.664
28 unmodified lines
693 - program: . - ["xml", "yaml"]694 input: '["xml", "yaml", "json"]'695 output: ['["json"]']696697 - title: "Multiplication, division, modulo: `*`, `/`, and `%`"697 - title: "Multiplication, division, modulo: `*`, `/`, `%`"698 body: |699700 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 lines
824 - program: 'map(in([0,1]))'825 input: '[2, 0]'826 output: ['[false, true]']827828 - title: "`map(x)`, `map_values(x)`"828 - title: "`map(f)`, `map_values(f)`"829 body: |830831 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.834835 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.837838 `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`.840841 examples:842 - program: 'map(.+1)'843 input: '[1,2,3]'
90 unmodified lines
934 - program: 'delpaths([["a","b"]])'935 input: '{"a":{"b":1},"x":{"y":2}}'936 output: ['{"a":{},"x":{"y":2}}']937938 - title: "`to_entries`, `from_entries`, `with_entries`"938 - title: "`to_entries`, `from_entries`, `with_entries(f)`"939 body: |940941 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}`.945946 `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.951952 examples:953 - program: 'to_entries'954 input: '{"a": 1, "b": 2}'
8 unmodified lines
963964 - title: "`select(boolean_expression)`"965 body: |966967 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.970971 It's useful for filtering lists: `[1,2,3] | map(select(. >= 2))`972 will give you `[2,3]`.
192 unmodified lines
1165 - program: flatten1166 input: '[{"foo": "bar"}, [{"foo": "baz"}]]'1167 output: ['[{"foo": "bar"}, {"foo": "baz"}]']11681169 - title: "`range(upto)`, `range(from;upto)` `range(from;upto;by)`"1169 - title: "`range(upto)`, `range(from; upto)`, `range(from; upto; by)`"1170 body: |11711172 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.11761177 The one argument form generates numbers from 0 to the given1178 number, with an increment of 1.
4 unmodified lines
1183 The three argument form generates numbers `from` to `upto`1184 with an increment of `by`.11851186 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]']12051206 - title: "`floor`"
75 unmodified lines
1282 - program: 'infinite, nan | type'1283 input: 'null'1284 output: ['"number"', '"number"']12851286 - title: "`sort, sort_by(path_expression)`"1286 - title: "`sort`, `sort_by(path_expression)`"1287 body: |12881289 The `sort` functions sorts its input, which must be an1290 array. Values are sorted in the following order:
13 unmodified lines
13041305 `sort` may be used to sort by a particular field of an1306 object, or by applying any jq filter.13071308 `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.13101311 examples:1312 - program: 'sort'1313 input: '[8,3,null,6]'
72 unmodified lines
1386 - title: "`contains(element)`"1387 body: |13881389 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 the
353 unmodified lines
17481749 - title: "`bsearch(x)`"1750 body: |17511752 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.17601761 examples:1762 - program: 'bsearch(0)'
5 unmodified lines
1768 - 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]']17711772 - title: "String interpolation - `\\(foo)`"1772 - title: "String interpolation: `\\(exp)`"1773 body: |17741775 Inside a string, you can put an expression inside parens1776 after a backslash. Whatever the expression returns will be
7 unmodified lines
1784 - title: "Convert to/from JSON"1785 body: |17861787 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.17911792 examples:1793 - program: '[.[]|tostring]'1794 input: '[1, "foo", ["foo"]]'
83 unmodified lines
1878 - program: '@html'1879 input: '"This works if x < y"'1880 output: ['"This works if x &lt; y"']18811882# - program: '@html "<span>Anonymous said: \(.)</span>"'1883# input: '"<script>alert(\"lol hax\");</script>"'1884# output: ["<span>Anonymous said: &lt;script&gt;alert(&quot;lol hax&quot;);&lt;/script&gt;</span>"]18851882 - program: '@sh "echo \(.)"'1883 input: "\"O'Hara's Ale\""1884 output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]1885
84 unmodified lines
1970 body: |19711972 jq provides a few SQL-style operators.19731978 * INDEX(stream; index_expression):1974 * INDEX(stream; index_expression):19751980 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.19791984 * JOIN($idx; stream; idx_expr; join_expr):1980 * JOIN($idx; stream; idx_expr; join_expr):19811986 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.19881993 * JOIN($idx; stream; idx_expr):1989 * JOIN($idx; stream; idx_expr):19901995 Same as `JOIN($idx; stream; idx_expr; .)`.1991 Same as `JOIN($idx; stream; idx_expr; .)`.19921997 * JOIN($idx; idx_expr):1993 * JOIN($idx; idx_expr):19941999 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.19982003 * IN(s):1999 * IN(s):20002005 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`.20032008 * IN(source; s):2004 * IN(source; s):20052010 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`.20082009 - title: "`builtins`"2010 body: |2011
20 unmodified lines
2032 - program: '.[] == 1'2033 input: '[1, 1.0, "1", "banana"]'2034 output: ['true', 'true', 'false', 'false']20352040 - title: if-then-else2036 - title: if-then-else-end2037 body: |20382039 `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 same
1 unmodified line
20422043 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.20492050 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 lines
2064 end2065 input: 22066 output: ['"many"']20672073 - title: "`>, >=, <=, <`"2068 - title: "`>`, `>=`, `<=`, `<`"2069 body: |20702071 The comparison operators `>`, `>=`, `<=`, `<` return whether2072 their left argument is greater than, greater than or equal
6 unmodified lines
2079 - program: '. < 5'2080 input: 22081 output: ['true']20822088 - title: and/or/not2083 - title: "`and`, `or`, `not`"2084 body: |20852091 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".20902091 If an operand of one of these operators produces multiple2092 results, the operator itself will produce a result for each input.2093
1 unmodified line
2095 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`.20982103 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.21052106 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 lines
2358 - program: 'splits(", *")'2359 input: '("ab,cd", "ef, gh")'2360 output: ['"ab"', '"cd"', '"ef"', '"gh"']23612369 - title: "`sub(regex; tostring)` `sub(regex; string; flags)`"2362 - title: "`sub(regex; tostring)`, `sub(regex; string; flags)`"2363 body: |23642365 Emit the string obtained by replacing the first match of regex in the2366 input string with `tostring`, after interpolation. `tostring` should
83 unmodified lines
2450 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:24532461 {"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"}}24582459 We want to produce the posts with the author field containing a real2460 name, as in:24612469 {"title": "Frist psot", "author": "Anonymous Coward"}2462 {"title": "First post", "author": "Anonymous Coward"}2463 {"title": "A well-written article", "author": "Person McPherson"}24642465 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 lines
2472 with `$x` set to that value. Thus `as` functions as something of a2473 foreach loop.24742475 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}`.24772478 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 lines
2633 to be able to create recursive functions.26342635 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.26412642 - title: Reduce2643 body: |
652 unmodified lines
modified:docs/content/manual/manual.yml+74 added-79 removed
95 unmodified lines
96 (`pwsh`/`pwsh.exe`), use single-quote characters around the jq97 program and backslash-escaped double-quotes (`\"`) inside the jq98 program.99100 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\"]"`103104 You can affect how jq reads and writes its input and output105 using some command-line options:106
176 unmodified lines
283 * `--jsonargs`:284285 Remaining arguments are positional JSON text arguments. These286 are available to the jq program as `$ARGS.positional[]`.287 287288 * `--`:289290 Terminates argument processing. Remaining arguments are291 positional, either strings, JSON texts, or input filenames,
25 unmodified lines
317 Since jq by default pretty-prints all output, this trivial318 program can be a useful way of formatting JSON output from,319 say, `curl`.320321 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.326327 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.331332 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.334335 examples:336 - program: '.'337 input: '"Hello, world!"'
61 unmodified lines
399 - program: '[.foo?]'400 input: '[1,2]'401 output: ['[]']402403 - title: "Generic Object Index: `.[<string>]`"403 - title: "Object Index: `.[<string>]`"404 body: |405406 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).409410 - title: "Array Index: `.[2]`"410 - title: "Array Index: `.[<number>]`"411 body: |412413 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.416417 Negative indices are allowed, with -1 referring to the last
11 unmodified lines
429 - program: '.[-2]'430 input: '[1,2,3]'431 output: ['2']432433 - title: "Array/String Slice: `.[10:15]`"433 - title: "Array/String Slice: `.[<number>:<number>]`"434 body: |435436 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).443444 examples:445 - program: '.[2:4]'446 input: '["a","b","c","d","e"]'
116 unmodified lines
563 values take an input and produce an output - `42` is a valid jq564 expression that takes an input, ignores it, and returns 42565 instead.566567 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.571572 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.576577 entries:578 - title: "Array construction: `[]`"579 body: |
118 unmodified lines
698699 - title: Builtin operators and functions700 body: |701702 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.707708 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.712713 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.719720 entries:
50 unmodified lines
771 - program: . - ["xml", "yaml"]772 input: '["xml", "yaml", "json"]'773 output: ['["json"]']774775 - title: "Multiplication, division, modulo: `*`, `/`, and `%`"775 - title: "Multiplication, division, modulo: `*`, `/`, `%`"776 body: |777778 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 lines
902 - program: 'map(in([0,1]))'903 input: '[2, 0]'904 output: ['[false, true]']905906 - title: "`map(x)`, `map_values(x)`"906 - title: "`map(f)`, `map_values(f)`"907 body: |908909 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.912913 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.915916 `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`.918919 examples:920 - program: 'map(.+1)'921 input: '[1,2,3]'
90 unmodified lines
1012 - program: 'delpaths([["a","b"]])'1013 input: '{"a":{"b":1},"x":{"y":2}}'1014 output: ['{"a":{},"x":{"y":2}}']10151016 - title: "`to_entries`, `from_entries`, `with_entries`"1016 - title: "`to_entries`, `from_entries`, `with_entries(f)`"1017 body: |10181019 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}`.10231024 `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.10291030 examples:1031 - program: 'to_entries'1032 input: '{"a": 1, "b": 2}'
8 unmodified lines
10411042 - title: "`select(boolean_expression)`"1043 body: |10441045 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.10481049 It's useful for filtering lists: `[1,2,3] | map(select(. >= 2))`1050 will give you `[2,3]`.
192 unmodified lines
1243 - program: flatten1244 input: '[{"foo": "bar"}, [{"foo": "baz"}]]'1245 output: ['[{"foo": "bar"}, {"foo": "baz"}]']12461247 - title: "`range(upto)`, `range(from;upto)`, `range(from;upto;by)`"1247 - title: "`range(upto)`, `range(from; upto)`, `range(from; upto; by)`"1248 body: |12491250 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.12541255 The one argument form generates numbers from 0 to the given1256 number, with an increment of 1.
4 unmodified lines
1261 The three argument form generates numbers `from` to `upto`1262 with an increment of `by`.12631264 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]']12831284 - title: "`floor`"
97 unmodified lines
13821383 `sort` may be used to sort by a particular field of an1384 object, or by applying any jq filter.13851386 `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.13881389 examples:1390 - program: 'sort'1391 input: '[8,3,null,6]'
473 unmodified lines
1865 - 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]']18681869 - title: "String interpolation - `\\(foo)`"1869 - title: "String interpolation: `\\(exp)`"1870 body: |18711872 Inside a string, you can put an expression inside parens1873 after a backslash. Whatever the expression returns will be
101 unmodified lines
1975 - program: '@html'1976 input: '"This works if x < y"'1977 output: ['"This works if x &lt; y"']19781979# - program: '@html "<span>Anonymous said: \(.)</span>"'1980# input: '"<script>alert(\"lol hax\");</script>"'1981# output: ["<span>Anonymous said: &lt;script&gt;alert(&quot;lol hax&quot;);&lt;/script&gt;</span>"]19821979 - program: '@sh "echo \(.)"'1980 input: "\"O'Hara's Ale\""1981 output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]1982
146 unmodified lines
2129 - program: '.[] == 1'2130 input: '[1, 1.0, "1", "banana"]'2131 output: ['true', 'true', 'false', 'false']21322137 - title: if-then-else2133 - title: if-then-else-end2134 body: |21352136 `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 same
16 unmodified lines
21542155 More cases can be added to an if using `elif A then B` syntax.21562157 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"']21682172 - title: "`>, >=, <=, <`"2169 - title: "`>`, `>=`, `<=`, `<`"2170 body: |21712172 The comparison operators `>`, `>=`, `<=`, `<` return whether2173 their left argument is greater than, greater than or equal
6 unmodified lines
2180 - program: '. < 5'2181 input: 22182 output: ['true']21832187 - title: and/or/not2184 - title: "`and`, `or`, `not`"2185 body: |21862190 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".21912192 If an operand of one of these operators produces multiple2193 results, the operator itself will produce a result for each input.2194
1 unmodified line
2196 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`.21992202 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.22062207 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 lines
2562 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:25652571 {"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"}}25702571 We want to produce the posts with the author field containing a real2572 name, as in:25732579 {"title": "Frist psot", "author": "Anonymous Coward"}2574 {"title": "First post", "author": "Anonymous Coward"}2575 {"title": "A well-written article", "author": "Person McPherson"}25762577 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 lines
2584 with `$x` set to that value. Thus `as` functions as something of a2585 foreach loop.25862587 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}`.25892590 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