jq.1.prebuilt
This file
Path
jq.1.prebuilt
4,338 lines · 124.1 KiB
Last changed by
docs: add simple and nested update-assignment examples (#3664)Hugohong258before adoption611c575
History of this fileTake it
1.2.TH "JQ" "1" "" "" ""3.4.SH "NAME"5\fBjq\fR \- Command\-line JSON processor6.7.SH "SYNOPSIS"8\fBjq\fR [\fIoptions\fR\.\.\.] \fIfilter\fR [\fIfiles\fR\.\.\.]9.10.P11\fBjq\fR can transform JSON in various ways, by selecting, iterating, reducing and otherwise mangling JSON documents\. For instance, running the command \fBjq \'map(\.price) | add\'\fR will take an array of JSON objects as input and return the sum of their "price" fields\.12.13.P14\fBjq\fR can accept text input as well, but by default, \fBjq\fR reads a stream of JSON entities (including numbers and other literals) from \fBstdin\fR\. Whitespace is only needed to separate entities such as 1 and 2, and true and false\. One or more \fIfiles\fR may be specified, in which case \fBjq\fR will read input from those instead\.15.16.P17The \fIoptions\fR are described in the [INVOKING JQ] section; they mostly concern input and output formatting\. The \fIfilter\fR is written in the jq language and specifies how to transform the input file or document\.18.19.SH "FILTERS"20A jq program is a "filter": it takes an input, and produces an output\. There are a lot of builtin filters for extracting a particular field of an object, or converting a number to a string, or various other standard tasks\.21.22.P23Filters can be combined in various ways \- you can pipe the output of one filter into another filter, or collect the output of a filter into an array\.24.25.P26Some filters produce multiple results, for instance there\'s one that produces all the elements of its input array\. Piping that filter into a second runs the second filter for each element of the array\. Generally, things that would be done with loops and iteration in other languages are just done by gluing filters together in jq\.27.28.P29It\'s important to remember that every filter has an input and an output\. Even literals like "hello" or 42 are filters \- they take an input but always produce the same literal as output\. Operations that combine two filters, like addition, generally feed the same input to both and combine the results\. So, you can implement an averaging filter as \fBadd / length\fR \- feeding the input array both to the \fBadd\fR filter and the \fBlength\fR filter and then performing the division\.30.31.P32But that\'s getting ahead of ourselves\. :) Let\'s start with something simpler:33.34.SH "INVOKING JQ"35jq filters run on a stream of JSON data\. The input to jq is parsed as a sequence of whitespace\-separated JSON values which are passed through the provided filter one at a time\. The output(s) of the filter are written to standard output, as a sequence of newline\-separated JSON data\.36.37.P38The simplest and most common filter (or jq program) is \fB\.\fR, which is the identity operator, copying the inputs of the jq processor to the output stream\. Because the default behavior of the jq processor is to read JSON texts from the input stream, and to pretty\-print outputs, the \fB\.\fR program\'s main use is to validate and pretty\-print the inputs\. The jq programming language is quite rich and allows for much more than just validation and pretty\-printing\.39.40.P41Note: it is important to mind the shell\'s quoting rules\. As a general rule it\'s best to always quote (with single\-quote characters on Unix shells) the jq program, as too many characters with special meaning to jq are also shell meta\-characters\. For example, \fBjq "foo"\fR will fail on most Unix shells because that will be the same as \fBjq foo\fR, which will generally fail because \fBfoo is not defined\fR\. When using the Windows command shell (cmd\.exe) it\'s best to use double quotes around your jq program when given on the command\-line (instead of the \fB\-f program\-file\fR option), but then double\-quotes in the jq program need backslash escaping\. When using the Powershell (\fBpowershell\.exe\fR) or the Powershell Core (\fBpwsh\fR/\fBpwsh\.exe\fR), use single\-quote characters around the jq program and backslash\-escaped double\-quotes (\fB\e"\fR) inside the jq program\.42.43.IP "\(bu" 444Unix shells: \fBjq \'\.["foo"]\'\fR45.46.IP "\(bu" 447Powershell: \fBjq \'\.[\e"foo\e"]\'\fR48.49.IP "\(bu" 450Windows command shell: \fBjq "\.[\e"foo\e"]"\fR51.52.IP "" 053.54.P55Note: jq allows user\-defined functions, but every jq program must have a top\-level expression\.56.57.P58You can affect how jq reads and writes its input and output using some command\-line options:59.60.TP61\fB\-\-null\-input\fR / \fB\-n\fR:62.63.IP64Don\'t read any input at all\. Instead, the filter is run once using \fBnull\fR as the input\. This is useful when using jq as a simple calculator or to construct JSON data from scratch\.65.66.TP67\fB\-\-raw\-input\fR / \fB\-R\fR:68.69.IP70Don\'t parse the input as JSON\. Instead, each line of text is passed to the filter as a string\. If combined with \fB\-\-slurp\fR, then the entire input is passed to the filter as a single long string\.71.72.TP73\fB\-\-slurp\fR / \fB\-s\fR:74.75.IP76Instead of running the filter for each JSON object in the input, read the entire input stream into a large array and run the filter just once\.77.78.TP79\fB\-\-compact\-output\fR / \fB\-c\fR:80.81.IP82By default, jq pretty\-prints JSON output\. Using this option will result in more compact output by instead putting each JSON object on a single line\.83.84.TP85\fB\-\-raw\-output\fR / \fB\-r\fR:86.87.IP88With this option, if the filter\'s result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes\. This can be useful for making jq filters talk to non\-JSON\-based systems\.89.90.TP91\fB\-\-raw\-output0\fR:92.93.IP94Like \fB\-r\fR but jq will print NUL instead of newline after each output\. This can be useful when the values being output can contain newlines\. When the output value contains NUL, jq exits with non\-zero code\.95.96.TP97\fB\-\-join\-output\fR / \fB\-j\fR:98.99.IP100Like \fB\-r\fR but jq won\'t print a newline after each output\.101.102.TP103\fB\-\-ascii\-output\fR / \fB\-a\fR:104.105.IP106jq usually outputs non\-ASCII Unicode codepoints as UTF\-8, even if the input specified them as escape sequences (like "\eu03bc")\. Using this option, you can force jq to produce pure ASCII output with every non\-ASCII character replaced with the equivalent escape sequence\.107.108.TP109\fB\-\-sort\-keys\fR / \fB\-S\fR:110.111.IP112Output the fields of each object with the keys in sorted order\.113.114.TP115\fB\-\-color\-output\fR / \fB\-C\fR and \fB\-\-monochrome\-output\fR / \fB\-M\fR:116.117.IP118By default, jq outputs colored JSON if writing to a terminal\. You can force it to produce color even if writing to a pipe or a file using \fB\-C\fR, and disable color with \fB\-M\fR\. When the \fBNO_COLOR\fR environment variable is not empty, jq disables colored output by default, but you can enable it by \fB\-C\fR\.119.120.IP121Colors can be configured with the \fBJQ_COLORS\fR environment variable (see below)\.122.123.TP124\fB\-\-tab\fR:125.126.IP127Use a tab for each indentation level instead of two spaces\.128.129.TP130\fB\-\-indent n\fR:131.132.IP133Use the given number of spaces (no more than 7) for indentation\.134.135.TP136\fB\-\-unbuffered\fR:137.138.IP139Flush the output after each JSON object is printed (useful if you\'re piping a slow data source into jq and piping jq\'s output elsewhere)\.140.141.TP142\fB\-\-stream\fR:143.144.IP145Parse the input in streaming fashion, outputting arrays of path and leaf values (scalars and empty arrays or empty objects)\. For example, \fB"a"\fR becomes \fB[[],"a"]\fR, and \fB[[],"a",["b"]]\fR becomes \fB[[0],[]]\fR, \fB[[1],"a"]\fR, and \fB[[2,0],"b"]\fR\.146.147.IP148This is useful for processing very large inputs\. Use this in conjunction with filtering and the \fBreduce\fR and \fBforeach\fR syntax to reduce large inputs incrementally\.149.150.TP151\fB\-\-stream\-errors\fR:152.153.IP154Like \fB\-\-stream\fR, but invalid JSON inputs yield array values where the first element is the error and the second is a path\. For example, \fB["a",n]\fR produces \fB["Invalid literal at line 1, column 7",[1]]\fR\.155.156.IP157Implies \fB\-\-stream\fR\. Invalid JSON inputs produce no error values when \fB\-\-stream\fR without \fB\-\-stream\-errors\fR\.158.159.TP160\fB\-\-seq\fR:161.162.IP163Use the \fBapplication/json\-seq\fR MIME type scheme for separating JSON texts in jq\'s input and output\. This means that an ASCII RS (record separator) character is printed before each value on output and an ASCII LF (line feed) is printed after every output\. Input JSON texts that fail to parse are ignored (but warned about), discarding all subsequent input until the next RS\. This mode also parses the output of jq without the \fB\-\-seq\fR option\.164.165.TP166\fB\-f\fR / \fB\-\-from\-file\fR:167.168.IP169Read the filter from a file rather than from a command line, like awk\'s \-f option\. This changes the filter argument to be interpreted as a filename, instead of the source of a program\.170.171.TP172\fB\-L directory\fR / \fB\-\-library\-path directory\fR:173.174.IP175Prepend \fBdirectory\fR to the search list for modules\. If this option is used then no builtin search list is used\. See the section on modules below\.176.177.TP178\fB\-\-arg name value\fR:179.180.IP181This option passes a value to the jq program as a predefined variable\. If you run jq with \fB\-\-arg foo bar\fR, then \fB$foo\fR is available in the program and has the value \fB"bar"\fR\. Note that \fBvalue\fR will be treated as a string, so \fB\-\-arg foo 123\fR will bind \fB$foo\fR to \fB"123"\fR\.182.183.IP184Named arguments are also available to the jq program as \fB$ARGS\.named\fR\. When the name is not a valid identifier, this is the only way to access it\.185.186.TP187\fB\-\-argjson name JSON\-text\fR:188.189.IP190This option passes a JSON\-encoded value to the jq program as a predefined variable\. If you run jq with \fB\-\-argjson foo 123\fR, then \fB$foo\fR is available in the program and has the value \fB123\fR\.191.192.TP193\fB\-\-slurpfile variable\-name filename\fR:194.195.IP196This option reads all the JSON texts in the named file and binds an array of the parsed JSON values to the given global variable\. If you run jq with \fB\-\-slurpfile foo bar\fR, then \fB$foo\fR is available in the program and has an array whose elements correspond to the texts in the file named \fBbar\fR\.197.198.TP199\fB\-\-rawfile variable\-name filename\fR:200.201.IP202This option reads in the named file and binds its content to the given global variable\. If you run jq with \fB\-\-rawfile foo bar\fR, then \fB$foo\fR is available in the program and has a string whose content is set to the text in the file named \fBbar\fR\.203.204.TP205\fB\-\-args\fR:206.207.IP208Remaining arguments are positional string arguments\. These are available to the jq program as \fB$ARGS\.positional[]\fR\.209.210.TP211\fB\-\-jsonargs\fR:212.213.IP214Remaining arguments are positional JSON text arguments\. These are available to the jq program as \fB$ARGS\.positional[]\fR\.215.216.TP217\fB\-\-exit\-status\fR / \fB\-e\fR:218.219.IP220Sets the exit status of jq to 0 if the last output value was neither \fBfalse\fR nor \fBnull\fR, 1 if the last output value was either \fBfalse\fR or \fBnull\fR, or 4 if no valid result was ever produced\. Normally jq exits with 2 if there was any usage problem or system error, 3 if there was a jq program compile error, or 0 if the jq program ran\.221.222.IP223Another way to set the exit status is with the \fBhalt_error\fR builtin function\.224.225.TP226\fB\-\-binary\fR / \fB\-b\fR:227.228.IP229Windows users using WSL, MSYS2, or Cygwin, should use this option when using a native jq\.exe, otherwise jq will turn newlines (LFs) into carriage\-return\-then\-newline (CRLF)\.230.231.TP232\fB\-\-version\fR / \fB\-V\fR:233.234.IP235Output the jq version and exit with zero\.236.237.TP238\fB\-\-build\-configuration\fR:239.240.IP241Output the build configuration of jq and exit with zero\. This output has no supported format or structure and may change without notice in future releases\.242.243.TP244\fB\-\-help\fR / \fB\-h\fR:245.246.IP247Output the jq help and exit with zero\.248.249.TP250\fB\-\-\fR:251.252.IP253Terminates argument processing\. Remaining arguments are not interpreted as options\.254.255.TP256\fB\-\-run\-tests [filename]\fR:257.258.IP259Runs the tests in the given file or standard input\. This must be the last option given and does not honor all preceding options\. The input consists of comment lines, empty lines, and program lines followed by one input line, as many lines of output as are expected (one per output), and a terminating empty line\. Compilation failure tests start with a line containing only \fB%%FAIL\fR, then a line containing the program to compile, then a line containing an error message to compare to the actual\.260.261.IP262Be warned that this option can change backwards\-incompatibly\.263.264.SH "BASIC FILTERS"265.266.SS "Identity: \."267The absolute simplest filter is \fB\.\fR \. This filter takes its input and produces the same value as output\. That is, this is the identity operator\.268.269.P270Since jq by default pretty\-prints all output, a trivial program consisting of nothing but \fB\.\fR can be used to format JSON output from, say, \fBcurl\fR\.271.272.P273Although the identity filter never modifies the value of its input, jq processing can sometimes make it appear as though it does\. For example, using the current implementation of jq, we would see that the expression:274.275.IP "" 4276.277.nf2782791E1234567890 | \.280.281.fi282.283.IP "" 0284.285.P286produces \fB1\.7976931348623157e+308\fR on at least one platform\. This is because, in the process of parsing the number, this particular version of jq has converted it to an IEEE754 double\-precision representation, losing precision\.287.288.P289The way in which jq handles numbers has changed over time and further changes are likely within the parameters set by the relevant JSON standards\. Moreover, build configuration options can alter how jq processes numbers\.290.291.P292The following remarks are therefore offered with the understanding that they are intended to be descriptive of the current version of jq and should not be interpreted as being prescriptive:293.294.P295(1) Any arithmetic operation on a number that has not already been converted to an IEEE754 double precision representation will trigger a conversion to the IEEE754 representation\.296.297.P298(2) jq will attempt to maintain the original decimal precision of number literals (if the \fB\-\-disable\-decnum\fR build configuration option was not used), but in expressions such \fB1E1234567890\fR, precision will be lost if the exponent is too large\.299.300.P301(3) Comparisons are carried out using the untruncated big decimal representation of numbers if available, as illustrated in one of the following examples\.302.303.P304The examples below use the builtin function \fBhave_decnum\fR in order to demonstrate the expected effects of using / not using the \fB\-\-disable\-decnum\fR build configuration option, and also to allow automated tests derived from these examples to pass regardless of whether that option is used\.305.306.IP "" 4307.308.nf309310jq \'\.\'311 "Hello, world!"312=> "Hello, world!"313314jq \'\.\'315 0\.12345678901234567890123456789316=> 0\.12345678901234567890123456789317318jq \'[\., tojson] == if have_decnum then [12345678909876543212345,"12345678909876543212345"] else [12345678909876543000000,"12345678909876543000000"] end\'319 12345678909876543212345320=> true321322jq \'[1234567890987654321,\-1234567890987654321 | tojson] == if have_decnum then ["1234567890987654321","\-1234567890987654321"] else ["1234567890987654400","\-1234567890987654400"] end\'323 null324=> true325326jq \'\. < 0\.12345678901234567890123456788\'327 0\.12345678901234567890123456789328=> false329330jq \'map([\., \. == 1]) | tojson == if have_decnum then "[[1,true],[1\.000,true],[1\.0,true],[1\.00,true]]" else "[[1,true],[1,true],[1,true],[1,true]]" end\'331 [1, 1\.000, 1\.0, 100e\-2]332=> true333334jq \'\. as $big | [$big, $big + 1] | map(\. > 10000000000000000000000000000000) | \. == if have_decnum then [true, false] else [false, false] end\'335 10000000000000000000000000000001336=> true337.338.fi339.340.IP "" 0341.342.SS "Object Identifier\-Index: \.foo, \.foo\.bar"343The simplest \fIuseful\fR filter has the form \fB\.foo\fR\. When given a JSON object (aka dictionary or hash) as input, \fB\.foo\fR produces the value at the key "foo" if the key is present, or null otherwise\.344.345.P346A filter of the form \fB\.foo\.bar\fR is equivalent to \fB\.foo | \.bar\fR\.347.348.P349The \fB\.foo\fR syntax only works for simple, identifier\-like keys, that is, keys that are all made of alphanumeric characters and underscore, and which do not start with a digit\.350.351.P352If the key contains special characters or starts with a digit, you need to surround it with double quotes like this: \fB\."foo$"\fR, or else \fB\.["foo$"]\fR\.353.354.P355For example \fB\.["foo::bar"]\fR and \fB\.["foo\.bar"]\fR work while \fB\.foo::bar\fR does not\.356.357.IP "" 4358.359.nf360361jq \'\.foo\'362 {"foo": 42, "bar": "less interesting data"}363=> 42364365jq \'\.foo\'366 {"notfoo": true, "alsonotfoo": false}367=> null368369jq \'\.["foo"]\'370 {"foo": 42}371=> 42372.373.fi374.375.IP "" 0376.377.SS "Optional Object Identifier\-Index: \.foo?"378Just like \fB\.foo\fR, but does not output an error when \fB\.\fR is not an object\.379.380.IP "" 4381.382.nf383384jq \'\.foo?\'385 {"foo": 42, "bar": "less interesting data"}386=> 42387388jq \'\.foo?\'389 {"notfoo": true, "alsonotfoo": false}390=> null391392jq \'\.["foo"]?\'393 {"foo": 42}394=> 42395396jq \'[\.foo?]\'397 [1,2]398=> []399.400.fi401.402.IP "" 0403.404.SS "Object Index: \.[<string>]"405You can also look up fields of an object using syntax like \fB\.["foo"]\fR (\fB\.foo\fR above is a shorthand version of this, but only for identifier\-like strings)\.406.407.SS "Array Index: \.[<number>]"408When the index value is an integer, \fB\.[<number>]\fR can index arrays\. Arrays are zero\-based, so \fB\.[2]\fR returns the third element\.409.410.P411Negative indices are allowed, with \-1 referring to the last element, \-2 referring to the next to last element, and so on\.412.413.IP "" 4414.415.nf416417jq \'\.[0]\'418 [{"name":"JSON", "good":true}, {"name":"XML", "good":false}]419=> {"name":"JSON", "good":true}420421jq \'\.[2]\'422 [{"name":"JSON", "good":true}, {"name":"XML", "good":false}]423=> null424425jq \'\.[\-2]\'426 [1,2,3]427=> 2428.429.fi430.431.IP "" 0432.433.SS "Array/String Slice: \.[<number>:<number>]"434The \fB\.[<number>:<number>]\fR syntax can be used to return a subarray of an array or substring of a string\. The array returned by \fB\.[10:15]\fR will be of length 5, containing the elements from index 10 (inclusive) to index 15 (exclusive)\. Either index may be negative (in which case it counts backwards from the end of the array), or omitted (in which case it refers to the start or end of the array)\. Indices are zero\-based\.435.436.IP "" 4437.438.nf439440jq \'\.[2:4]\'441 ["a","b","c","d","e"]442=> ["c", "d"]443444jq \'\.[2:4]\'445 "abcdefghi"446=> "cd"447448jq \'\.[:3]\'449 ["a","b","c","d","e"]450=> ["a", "b", "c"]451452jq \'\.[\-2:]\'453 ["a","b","c","d","e"]454=> ["d", "e"]455.456.fi457.458.IP "" 0459.460.SS "Array/Object Value Iterator: \.[]"461If you use the \fB\.[index]\fR syntax, but omit the index entirely, it will return \fIall\fR of the elements of an array\. Running \fB\.[]\fR with the input \fB[1,2,3]\fR will produce the numbers as three separate results, rather than as a single array\. A filter of the form \fB\.foo[]\fR is equivalent to \fB\.foo | \.[]\fR\.462.463.P464You can also use this on an object, and it will return all the values of the object\.465.466.P467Note that the iterator operator is a generator of values\.468.469.IP "" 4470.471.nf472473jq \'\.[]\'474 [{"name":"JSON", "good":true}, {"name":"XML", "good":false}]475=> {"name":"JSON", "good":true}, {"name":"XML", "good":false}476477jq \'\.[]\'478 []479=> 480481jq \'\.foo[]\'482 {"foo":[1,2,3]}483=> 1, 2, 3484485jq \'\.[]\'486 {"a": 1, "b": 1}487=> 1, 1488.489.fi490.491.IP "" 0492.493.SS "\.[]?"494Like \fB\.[]\fR, but no errors will be output if \. is not an array or object\. A filter of the form \fB\.foo[]?\fR is equivalent to \fB\.foo | \.[]?\fR\.495.496.SS "Comma: ,"497If two filters are separated by a comma, then the same input will be fed into both and the two filters\' output value streams will be concatenated in order: first, all of the outputs produced by the left expression, and then all of the outputs produced by the right\. For instance, filter \fB\.foo, \.bar\fR, produces both the "foo" fields and "bar" fields as separate outputs\.498.499.P500The \fB,\fR operator is one way to construct generators\.501.502.IP "" 4503.504.nf505506jq \'\.foo, \.bar\'507 {"foo": 42, "bar": "something else", "baz": true}508=> 42, "something else"509510jq \'\.user, \.projects[]\'511 {"user":"stedolan", "projects": ["jq", "wikiflow"]}512=> "stedolan", "jq", "wikiflow"513514jq \'\.[4,2]\'515 ["a","b","c","d","e"]516=> "e", "c"517.518.fi519.520.IP "" 0521.522.SS "Pipe: |"523The | operator combines two filters by feeding the output(s) of the one on the left into the input of the one on the right\. It\'s similar to the Unix shell\'s pipe, if you\'re used to that\.524.525.P526If the one on the left produces multiple results, the one on the right will be run for each of those results\. So, the expression \fB\.[] | \.foo\fR retrieves the "foo" field of each element of the input array\. This is a cartesian product, which can be surprising\.527.528.P529Note that \fB\.a\.b\.c\fR is the same as \fB\.a | \.b | \.c\fR\.530.531.P532Note too that \fB\.\fR is the input value at the particular stage in a "pipeline", specifically: where the \fB\.\fR expression appears\. Thus \fB\.a | \. | \.b\fR is the same as \fB\.a\.b\fR, as the \fB\.\fR in the middle refers to whatever value \fB\.a\fR produced\.533.534.IP "" 4535.536.nf537538jq \'\.[] | \.name\'539 [{"name":"JSON", "good":true}, {"name":"XML", "good":false}]540=> "JSON", "XML"541.542.fi543.544.IP "" 0545.546.SS "Parenthesis"547Parenthesis work as a grouping operator just as in any typical programming language\.548.549.IP "" 4550.551.nf552553jq \'(\. + 2) * 5\'554 1555=> 15556.557.fi558.559.IP "" 0560.561.SH "TYPES AND VALUES"562jq supports the same set of datatypes as JSON \- numbers, strings, booleans, arrays, objects (which in JSON\-speak are hashes with only string keys), and "null"\.563.564.P565Booleans, null, strings and numbers are written the same way as in JSON\. Just like everything else in jq, these simple values take an input and produce an output \- \fB42\fR is a valid jq expression that takes an input, ignores it, and returns 42 instead\.566.567.P568Numbers in jq are internally represented by their IEEE754 double precision approximation\. Any arithmetic operation with numbers, whether they are literals or results of previous filters, will produce a double precision floating point result\.569.570.P571However, when parsing a literal jq will store the original literal string\. If no mutation is applied to this value then it will make to the output in its original form, even if conversion to double would result in a loss\.572.573.SS "Array construction: []"574As in JSON, \fB[]\fR is used to construct arrays, as in \fB[1,2,3]\fR\. The elements of the arrays can be any jq expression, including a pipeline\. All of the results produced by all of the expressions are collected into one big array\. You can use it to construct an array out of a known quantity of values (as in \fB[\.foo, \.bar, \.baz]\fR) or to "collect" all the results of a filter into an array (as in \fB[\.items[]\.name]\fR)575.576.P577Once you understand the "," operator, you can look at jq\'s array syntax in a different light: the expression \fB[1,2,3]\fR is not using a built\-in syntax for comma\-separated arrays, but is instead applying the \fB[]\fR operator (collect results) to the expression 1,2,3 (which produces three different results)\.578.579.P580If you have a filter \fBX\fR that produces four results, then the expression \fB[X]\fR will produce a single result, an array of four elements\.581.582.IP "" 4583.584.nf585586jq \'[\.user, \.projects[]]\'587 {"user":"stedolan", "projects": ["jq", "wikiflow"]}588=> ["stedolan", "jq", "wikiflow"]589590jq \'[ \.[] | \. * 2]\'591 [1, 2, 3]592=> [2, 4, 6]593.594.fi595.596.IP "" 0597.598.SS "Object Construction: {}"599Like JSON, \fB{}\fR is for constructing objects (aka dictionaries or hashes), as in: \fB{"a": 42, "b": 17}\fR\.600.601.P602If the keys are "identifier\-like", then the quotes can be left off, as in \fB{a:42, b:17}\fR\. Variable references as key expressions use the value of the variable as the key\. Key expressions other than constant literals, identifiers, or variable references, need to be parenthesized, e\.g\., \fB{("a"+"b"):59}\fR\.603.604.P605The value can be any expression (although you may need to wrap it in parentheses if, for example, it contains colons), which gets applied to the {} expression\'s input (remember, all filters have an input and an output)\.606.607.IP "" 4608.609.nf610611{foo: \.bar}612.613.fi614.615.IP "" 0616.617.P618will produce the JSON object \fB{"foo": 42}\fR if given the JSON object \fB{"bar":42, "baz":43}\fR as its input\. You can use this to select particular fields of an object: if the input is an object with "user", "title", "id", and "content" fields and you just want "user" and "title", you can write619.620.IP "" 4621.622.nf623624{user: \.user, title: \.title}625.626.fi627.628.IP "" 0629.630.P631Because that is so common, there\'s a shortcut syntax for it: \fB{user, title}\fR\.632.633.P634If one of the expressions produces multiple results, multiple dictionaries will be produced\. If the input\'s635.636.IP "" 4637.638.nf639640{"user":"stedolan","titles":["JQ Primer", "More JQ"]}641.642.fi643.644.IP "" 0645.646.P647then the expression648.649.IP "" 4650.651.nf652653{user, title: \.titles[]}654.655.fi656.657.IP "" 0658.659.P660will produce two outputs:661.662.IP "" 4663.664.nf665666{"user":"stedolan", "title": "JQ Primer"}667{"user":"stedolan", "title": "More JQ"}668.669.fi670.671.IP "" 0672.673.P674Putting parentheses around the key means it will be evaluated as an expression\. With the same input as above,675.676.IP "" 4677.678.nf679680{(\.user): \.titles}681.682.fi683.684.IP "" 0685.686.P687produces688.689.IP "" 4690.691.nf692693{"stedolan": ["JQ Primer", "More JQ"]}694.695.fi696.697.IP "" 0698.699.P700Variable references as keys use the value of the variable as the key\. Without a value then the variable\'s name becomes the key and its value becomes the value,701.702.IP "" 4703.704.nf705706"f o o" as $foo | "b a r" as $bar | {$foo, $bar:$foo}707.708.fi709.710.IP "" 0711.712.P713produces714.715.IP "" 4716.717.nf718719{"foo":"f o o","b a r":"f o o"}720721jq \'{user, title: \.titles[]}\'722 {"user":"stedolan","titles":["JQ Primer", "More JQ"]}723=> {"user":"stedolan", "title": "JQ Primer"}, {"user":"stedolan", "title": "More JQ"}724725jq \'{(\.user): \.titles}\'726 {"user":"stedolan","titles":["JQ Primer", "More JQ"]}727=> {"stedolan": ["JQ Primer", "More JQ"]}728.729.fi730.731.IP "" 0732.733.SS "Recursive Descent: \.\."734Recursively descends \fB\.\fR, producing every value\. This is the same as the zero\-argument \fBrecurse\fR builtin (see below)\. This is intended to resemble the XPath \fB//\fR operator\. Note that \fB\.\.a\fR does not work; use \fB\.\. | \.a\fR instead\. In the example below we use \fB\.\. | \.a?\fR to find all the values of object keys "a" in any object found "below" \fB\.\fR\.735.736.P737This is particularly useful in conjunction with \fBpath(EXP)\fR (also see below) and the \fB?\fR operator\.738.739.IP "" 4740.741.nf742743jq \'\.\. | \.a?\'744 [[{"a":1}]]745=> 1746.747.fi748.749.IP "" 0750.751.SH "BUILTIN OPERATORS AND FUNCTIONS"752Some jq operators (for instance, \fB+\fR) do different things depending on the type of their arguments (arrays, numbers, etc\.)\. However, jq never does implicit type conversions\. If you try to add a string to an object you\'ll get an error message and no result\.753.754.P755Please note that all numbers are converted to IEEE754 double precision floating point representation\. Arithmetic and logical operators are working with these converted doubles\. Results of all such operations are also limited to the double precision\.756.757.P758The only exception to this behaviour of number is a snapshot of original number literal\. When a number which originally was provided as a literal is never mutated until the end of the program then it is printed to the output in its original literal form\. This also includes cases when the original literal would be truncated when converted to the IEEE754 double precision floating point number\.759.760.SS "Addition: +"761The operator \fB+\fR takes two filters, applies them both to the same input, and adds the results together\. What "adding" means depends on the types involved:762.763.IP "\(bu" 4764\fBNumbers\fR are added by normal arithmetic\.765.766.IP "\(bu" 4767\fBArrays\fR are added by being concatenated into a larger array\.768.769.IP "\(bu" 4770\fBStrings\fR are added by being joined into a larger string\.771.772.IP "\(bu" 4773\fBObjects\fR are added by merging, that is, inserting all the key\-value pairs from both objects into a single combined object\. If both objects contain a value for the same key, the object on the right of the \fB+\fR wins\. (For recursive merge use the \fB*\fR operator\.)774.775.IP "" 0776.777.P778\fBnull\fR can be added to any value, and returns the other value unchanged\.779.780.IP "" 4781.782.nf783784jq \'\.a + 1\'785 {"a": 7}786=> 8787788jq \'\.a + \.b\'789 {"a": [1,2], "b": [3,4]}790=> [1,2,3,4]791792jq \'\.a + null\'793 {"a": 1}794=> 1795796jq \'\.a + 1\'797 {}798=> 1799800jq \'{a: 1} + {b: 2} + {c: 3} + {a: 42}\'801 null802=> {"a": 42, "b": 2, "c": 3}803.804.fi805.806.IP "" 0807.808.SS "Subtraction: \-"809As well as normal arithmetic subtraction on numbers, the \fB\-\fR operator can be used on arrays to remove all occurrences of the second array\'s elements from the first array\.810.811.IP "" 4812.813.nf814815jq \'4 \- \.a\'816 {"a":3}817=> 1818819jq \'\. \- ["xml", "yaml"]\'820 ["xml", "yaml", "json"]821=> ["json"]822.823.fi824.825.IP "" 0826.827.SS "Multiplication, division, modulo: *, /, %"828These infix operators behave as expected when given two numbers\. Division by zero raises an error\. \fBx % y\fR computes x modulo y\.829.830.P831Multiplying a string by a number produces the concatenation of that string that many times\. \fB"x" * 0\fR produces \fB""\fR\.832.833.P834Dividing a string by another splits the first using the second as separators\.835.836.P837Multiplying two objects will merge them recursively: this works like addition but if both objects contain a value for the same key, and the values are objects, the two are merged with the same strategy\.838.839.IP "" 4840.841.nf842843jq \'10 / \. * 3\'844 5845=> 6846847jq \'\. / ", "\'848 "a, b,c,d, e"849=> ["a","b,c,d","e"]850851jq \'{"k": {"a": 1, "b": 2}} * {"k": {"a": 0,"c": 3}}\'852 null853=> {"k": {"a": 0, "b": 2, "c": 3}}854855jq \'\.[] | (1 / \.)?\'856 [1,0,\-1]857=> 1, \-1858.859.fi860.861.IP "" 0862.863.SS "abs"864The builtin function \fBabs\fR is defined naively as: \fBif \. < 0 then \- \. else \. end\fR\.865.866.P867For numeric input, this is the absolute value\. See the section on the identity filter for the implications of this definition for numeric input\.868.869.P870To compute the absolute value of a number as a floating point number, you may wish to use \fBfabs\fR\.871.872.IP "" 4873.874.nf875876jq \'map(abs)\'877 [\-10, \-1\.1, \-1e\-1]878=> [10,1\.1,1e\-1]879.880.fi881.882.IP "" 0883.884.SS "length"885The builtin function \fBlength\fR gets the length of various different types of value:886.887.IP "\(bu" 4888The length of a \fBstring\fR is the number of Unicode codepoints it contains (which will be the same as its JSON\-encoded length in bytes if it\'s pure ASCII)\.889.890.IP "\(bu" 4891The length of a \fBnumber\fR is its absolute value\.892.893.IP "\(bu" 4894The length of an \fBarray\fR is the number of elements\.895.896.IP "\(bu" 4897The length of an \fBobject\fR is the number of key\-value pairs\.898.899.IP "\(bu" 4900The length of \fBnull\fR is zero\.901.902.IP "\(bu" 4903It is an error to use \fBlength\fR on a \fBboolean\fR\.904.905.IP "" 4906.907.nf908909jq \'\.[] | length\'910 [[1,2], "string", {"a":2}, null, \-5]911=> 2, 6, 1, 0, 5912.913.fi914.915.IP "" 0916.917.SS "utf8bytelength"918The builtin function \fButf8bytelength\fR outputs the number of bytes used to encode a string in UTF\-8\.919.920.IP "" 4921.922.nf923924jq \'utf8bytelength\'925 "\eu03bc"926=> 2927.928.fi929.930.IP "" 0931.932.SS "keys, keys_unsorted"933The builtin function \fBkeys\fR, when given an object, returns its keys in an array\.934.935.P936The keys are sorted "alphabetically", by unicode codepoint order\. This is not an order that makes particular sense in any particular language, but you can count on it being the same for any two objects with the same set of keys, regardless of locale settings\.937.938.P939When \fBkeys\fR is given an array, it returns the valid indices for that array: the integers from 0 to length\-1\.940.941.P942The \fBkeys_unsorted\fR function is just like \fBkeys\fR, but if the input is an object then the keys will not be sorted, instead the keys will roughly be in insertion order\.943.944.IP "" 4945.946.nf947948jq \'keys\'949 {"abc": 1, "abcd": 2, "Foo": 3}950=> ["Foo", "abc", "abcd"]951952jq \'keys\'953 [42,3,35]954=> [0,1,2]955.956.fi957.958.IP "" 0959.960.SS "has(key)"961The builtin function \fBhas\fR returns whether the input object has the given key, or the input array has an element at the given index\.962.963.P964\fBhas($key)\fR has the same effect as checking whether \fB$key\fR is a member of the array returned by \fBkeys\fR, although \fBhas\fR will be faster\.965.966.IP "" 4967.968.nf969970jq \'map(has("foo"))\'971 [{"foo": 42}, {}]972=> [true, false]973974jq \'map(has(2))\'975 [[0,1], ["a","b","c"]]976=> [false, true]977.978.fi979.980.IP "" 0981.982.SS "in"983The builtin function \fBin\fR returns whether or not the input key is in the given object, or the input index corresponds to an element in the given array\. It is, essentially, an inversed version of \fBhas\fR\.984.985.IP "" 4986.987.nf988989jq \'\.[] | in({"foo": 42})\'990 ["foo", "bar"]991=> true, false992993jq \'map(in([0,1]))\'994 [2, 0]995=> [false, true]996.997.fi998.999.IP "" 01000.1001.SS "map(f), map_values(f)"1002For any filter \fBf\fR, \fBmap(f)\fR and \fBmap_values(f)\fR apply \fBf\fR to each of the values in the input array or object, that is, to the values of \fB\.[]\fR\.1003.1004.P1005In the absence of errors, \fBmap(f)\fR always outputs an array whereas \fBmap_values(f)\fR outputs an array if given an array, or an object if given an object\.1006.1007.P1008When the input to \fBmap_values(f)\fR is an object, the output object has the same keys as the input object except for those keys whose values when piped to \fBf\fR produce no values at all\.1009.1010.P1011The key difference between \fBmap(f)\fR and \fBmap_values(f)\fR is that the former simply forms an array from all the values of \fB($x|f)\fR for each value, \fB$x\fR, in the input array or object, but \fBmap_values(f)\fR only uses \fBfirst($x|f)\fR\.1012.1013.P1014Specifically, for object inputs, \fBmap_values(f)\fR constructs the output object by examining in turn the value of \fBfirst(\.[$k]|f)\fR for each key, \fB$k\fR, of the input\. If this expression produces no values, then the corresponding key will be dropped; otherwise, the output object will have that value at the key, \fB$k\fR\.1015.1016.P1017Here are some examples to clarify the behavior of \fBmap\fR and \fBmap_values\fR when applied to arrays\. These examples assume the input is \fB[1]\fR in all cases:1018.1019.IP "" 41020.1021.nf10221023map(\.+1) #=> [2]1024map(\., \.) #=> [1,1]1025map(empty) #=> []10261027map_values(\.+1) #=> [2]1028map_values(\., \.) #=> [1]1029map_values(empty) #=> []1030.1031.fi1032.1033.IP "" 01034.1035.P1036\fBmap(f)\fR is equivalent to \fB[\.[] | f]\fR and \fBmap_values(f)\fR is equivalent to \fB\.[] |= f\fR\.1037.1038.P1039In fact, these are their implementations\.1040.1041.IP "" 41042.1043.nf10441045jq \'map(\.+1)\'1046 [1,2,3]1047=> [2,3,4]10481049jq \'map_values(\.+1)\'1050 {"a": 1, "b": 2, "c": 3}1051=> {"a": 2, "b": 3, "c": 4}10521053jq \'map(\., \.)\'1054 [1,2]1055=> [1,1,2,2]10561057jq \'map_values(\. // empty)\'1058 {"a": null, "b": true, "c": false}1059=> {"b":true}1060.1061.fi1062.1063.IP "" 01064.1065.SS "pick(pathexps)"1066Emit the projection of the input object or array defined by the specified sequence of path expressions, such that if \fBp\fR is any one of these specifications, then \fB(\. | p)\fR will evaluate to the same value as \fB(\. | pick(pathexps) | p)\fR\. For arrays, negative indices and \fB\.[m:n]\fR specifications should not be used\.1067.1068.IP "" 41069.1070.nf10711072jq \'pick(\.a, \.b\.c, \.x)\'1073 {"a": 1, "b": {"c": 2, "d": 3}, "e": 4}1074=> {"a":1,"b":{"c":2},"x":null}10751076jq \'pick(\.[2], \.[0], \.[0])\'1077 [1,2,3,4]1078=> [1,null,3]1079.1080.fi1081.1082.IP "" 01083.1084.SS "path(path_expression)"1085Outputs array representations of the given path expression in \fB\.\fR\. The outputs are arrays of strings (object keys) and/or numbers (array indices)\.1086.1087.P1088Path expressions are jq expressions like \fB\.a\fR, but also \fB\.[]\fR\. There are two types of path expressions: ones that can match exactly, and ones that cannot\. For example, \fB\.a\.b\.c\fR is an exact match path expression, while \fB\.a[]\.b\fR is not\.1089.1090.P1091\fBpath(exact_path_expression)\fR will produce the array representation of the path expression even if it does not exist in \fB\.\fR, if \fB\.\fR is \fBnull\fR or an array or an object\.1092.1093.P1094\fBpath(pattern)\fR will produce array representations of the paths matching \fBpattern\fR if the paths exist in \fB\.\fR\.1095.1096.P1097Note that the path expressions are not different from normal expressions\. The expression \fBpath(\.\.|select(type=="boolean"))\fR outputs all the paths to boolean values in \fB\.\fR, and only those paths\.1098.1099.IP "" 41100.1101.nf11021103jq \'path(\.a[0]\.b)\'1104 null1105=> ["a",0,"b"]11061107jq \'[path(\.\.)]\'1108 {"a":[{"b":1}]}1109=> [[],["a"],["a",0],["a",0,"b"]]1110.1111.fi1112.1113.IP "" 01114.1115.SS "del(path_expression)"1116The builtin function \fBdel\fR removes a key and its corresponding value from an object\.1117.1118.IP "" 41119.1120.nf11211122jq \'del(\.foo)\'1123 {"foo": 42, "bar": 9001, "baz": 42}1124=> {"bar": 9001, "baz": 42}11251126jq \'del(\.[1, 2])\'1127 ["foo", "bar", "baz"]1128=> ["foo"]1129.1130.fi1131.1132.IP "" 01133.1134.SS "getpath(PATHS)"1135The builtin function \fBgetpath\fR outputs the values in \fB\.\fR found at each path in \fBPATHS\fR\.1136.1137.IP "" 41138.1139.nf11401141jq \'getpath(["a","b"])\'1142 null1143=> null11441145jq \'[getpath(["a","b"], ["a","c"])]\'1146 {"a":{"b":0, "c":1}}1147=> [0, 1]1148.1149.fi1150.1151.IP "" 01152.1153.SS "setpath(PATHS; VALUE)"1154The builtin function \fBsetpath\fR sets the \fBPATHS\fR in \fB\.\fR to \fBVALUE\fR\.1155.1156.IP "" 41157.1158.nf11591160jq \'setpath(["a","b"]; 1)\'1161 null1162=> {"a": {"b": 1}}11631164jq \'setpath(["a","b"]; 1)\'1165 {"a":{"b":0}}1166=> {"a": {"b": 1}}11671168jq \'setpath([0,"a"]; 1)\'1169 null1170=> [{"a":1}]1171.1172.fi1173.1174.IP "" 01175.1176.SS "delpaths(PATHS)"1177The builtin function \fBdelpaths\fR deletes the \fBPATHS\fR in \fB\.\fR\. \fBPATHS\fR must be an array of paths, where each path is an array of strings and numbers\.1178.1179.IP "" 41180.1181.nf11821183jq \'delpaths([["a","b"]])\'1184 {"a":{"b":1},"x":{"y":2}}1185=> {"a":{},"x":{"y":2}}1186.1187.fi1188.1189.IP "" 01190.1191.SS "to_entries, from_entries, with_entries(f)"1192These functions convert between an object and an array of key\-value pairs\. If \fBto_entries\fR is passed an object, then for each \fBk: v\fR entry in the input, the output array includes \fB{"key": k, "value": v}\fR\.1193.1194.P1195\fBfrom_entries\fR does the opposite conversion, and \fBwith_entries(f)\fR is a shorthand for \fBto_entries | map(f) | from_entries\fR, useful for doing some operation to all keys and values of an object\. \fBfrom_entries\fR accepts \fB"key"\fR, \fB"Key"\fR, \fB"name"\fR, \fB"Name"\fR, \fB"value"\fR, and \fB"Value"\fR as keys\.1196.1197.IP "" 41198.1199.nf12001201jq \'to_entries\'1202 {"a": 1, "b": 2}1203=> [{"key":"a", "value":1}, {"key":"b", "value":2}]12041205jq \'from_entries\'1206 [{"key":"a", "value":1}, {"key":"b", "value":2}]1207=> {"a": 1, "b": 2}12081209jq \'with_entries(\.key |= "KEY_" + \.)\'1210 {"a": 1, "b": 2}1211=> {"KEY_a": 1, "KEY_b": 2}1212.1213.fi1214.1215.IP "" 01216.1217.SS "select(boolean_expression)"1218The function \fBselect(f)\fR produces its input unchanged if \fBf\fR returns true for that input, and produces no output otherwise\.1219.1220.P1221It\'s useful for filtering lists: \fB[1,2,3] | map(select(\. >= 2))\fR will give you \fB[2,3]\fR\.1222.1223.IP "" 41224.1225.nf12261227jq \'map(select(\. >= 2))\'1228 [1,5,3,0,7]1229=> [5,3,7]12301231jq \'\.[] | select(\.id == "second")\'1232 [{"id": "first", "val": 1}, {"id": "second", "val": 2}]1233=> {"id": "second", "val": 2}1234.1235.fi1236.1237.IP "" 01238.1239.SS "arrays, objects, iterables, booleans, numbers, normals, finites, strings, nulls, values, scalars"1240These built\-ins select only inputs that are arrays, objects, iterables (arrays or objects), booleans, numbers, normal numbers, finite numbers, strings, null, non\-null values, and non\-iterables, respectively\.1241.1242.IP "" 41243.1244.nf12451246jq \'\.[]|numbers\'1247 [[],{},1,"foo",null,true,false]1248=> 11249.1250.fi1251.1252.IP "" 01253.1254.SS "empty"1255\fBempty\fR returns no results\. None at all\. Not even \fBnull\fR\.1256.1257.P1258It\'s useful on occasion\. You\'ll know if you need it :)1259.1260.IP "" 41261.1262.nf12631264jq \'1, empty, 2\'1265 null1266=> 1, 212671268jq \'[1,2,empty,3]\'1269 null1270=> [1,2,3]1271.1272.fi1273.1274.IP "" 01275.1276.SS "error, error(message)"1277Produces an error with the input value, or with the message given as the argument\. Errors can be caught with try/catch; see below\.1278.1279.IP "" 41280.1281.nf12821283jq \'try error catch \.\'1284 "error message"1285=> "error message"12861287jq \'try error("invalid value: \e(\.)") catch \.\'1288 421289=> "invalid value: 42"1290.1291.fi1292.1293.IP "" 01294.1295.SS "halt"1296Stops the jq program with no further outputs\. jq will exit with exit status \fB0\fR\.1297.1298.SS "halt_error, halt_error(exit_code)"1299Stops the jq program with no further outputs\. The input will be printed on \fBstderr\fR as raw output (i\.e\., strings will not have double quotes) with no decoration, not even a newline\.1300.1301.P1302The given \fBexit_code\fR (defaulting to \fB5\fR) will be jq\'s exit status\.1303.1304.P1305For example, \fB"Error: something went wrong\en"|halt_error(1)\fR\.1306.1307.SS "$__loc__"1308Produces an object with a "file" key and a "line" key, with the filename and line number where \fB$__loc__\fR occurs, as values\.1309.1310.IP "" 41311.1312.nf13131314jq \'try error("\e($__loc__)") catch \.\'1315 null1316=> "{\e"file\e":\e"<top\-level>\e",\e"line\e":1}"1317.1318.fi1319.1320.IP "" 01321.1322.SS "paths, paths(node_filter)"1323\fBpaths\fR outputs the paths to all the elements in its input (except it does not output the empty list, representing \. itself)\.1324.1325.P1326\fBpaths(f)\fR outputs the paths to any values for which \fBf\fR is \fBtrue\fR\. That is, \fBpaths(type == "number")\fR outputs the paths to all numeric values\.1327.1328.IP "" 41329.1330.nf13311332jq \'[paths]\'1333 [1,[[],{"a":2}]]1334=> [[0],[1],[1,0],[1,1],[1,1,"a"]]13351336jq \'[paths(type == "number")]\'1337 [1,[[],{"a":2}]]1338=> [[0],[1,1,"a"]]1339.1340.fi1341.1342.IP "" 01343.1344.SS "add, add(generator)"1345The filter \fBadd\fR takes as input an array, and produces as output the elements of the array added together\. This might mean summed, concatenated or merged depending on the types of the elements of the input array \- the rules are the same as those for the \fB+\fR operator (described above)\.1346.1347.P1348If the input is an empty array, \fBadd\fR returns \fBnull\fR\.1349.1350.P1351\fBadd(generator)\fR operates on the given generator rather than the input\.1352.1353.IP "" 41354.1355.nf13561357jq \'add\'1358 ["a","b","c"]1359=> "abc"13601361jq \'add\'1362 [1, 2, 3]1363=> 613641365jq \'add\'1366 []1367=> null13681369jq \'add(\.[]\.a)\'1370 [{"a":3}, {"a":5}, {"b":6}]1371=> 81372.1373.fi1374.1375.IP "" 01376.1377.SS "any, any(condition), any(generator; condition)"1378The filter \fBany\fR takes as input an array of boolean values, and produces \fBtrue\fR as output if any of the elements of the array are \fBtrue\fR\.1379.1380.P1381If the input is an empty array, \fBany\fR returns \fBfalse\fR\.1382.1383.P1384The \fBany(condition)\fR form applies the given condition to the elements of the input array\.1385.1386.P1387The \fBany(generator; condition)\fR form applies the given condition to all the outputs of the given generator\.1388.1389.IP "" 41390.1391.nf13921393jq \'any\'1394 [true, false]1395=> true13961397jq \'any\'1398 [false, false]1399=> false14001401jq \'any\'1402 []1403=> false1404.1405.fi1406.1407.IP "" 01408.1409.SS "all, all(condition), all(generator; condition)"1410The filter \fBall\fR takes as input an array of boolean values, and produces \fBtrue\fR as output if all of the elements of the array are \fBtrue\fR\.1411.1412.P1413The \fBall(condition)\fR form applies the given condition to the elements of the input array\.1414.1415.P1416The \fBall(generator; condition)\fR form applies the given condition to all the outputs of the given generator\.1417.1418.P1419If the input is an empty array, \fBall\fR returns \fBtrue\fR\.1420.1421.IP "" 41422.1423.nf14241425jq \'all\'1426 [true, false]1427=> false14281429jq \'all\'1430 [true, true]1431=> true14321433jq \'all\'1434 []1435=> true1436.1437.fi1438.1439.IP "" 01440.1441.SS "flatten, flatten(depth)"1442The filter \fBflatten\fR takes as input an array of nested arrays, and produces a flat array in which all arrays inside the original array have been recursively replaced by their values\. You can pass an argument to it to specify how many levels of nesting to flatten\.1443.1444.P1445\fBflatten(2)\fR is like \fBflatten\fR, but going only up to two levels deep\.1446.1447.IP "" 41448.1449.nf14501451jq \'flatten\'1452 [1, [2], [[3]]]1453=> [1, 2, 3]14541455jq \'flatten(1)\'1456 [1, [2], [[3]]]1457=> [1, 2, [3]]14581459jq \'flatten\'1460 [[]]1461=> []14621463jq \'flatten\'1464 [{"foo": "bar"}, [{"foo": "baz"}]]1465=> [{"foo": "bar"}, {"foo": "baz"}]1466.1467.fi1468.1469.IP "" 01470.1471.SS "range(upto), range(from; upto), range(from; upto; by)"1472The \fBrange\fR function produces a range of numbers\. \fBrange(4; 10)\fR produces 6 numbers, from 4 (inclusive) to 10 (exclusive)\. The numbers are produced as separate outputs\. Use \fB[range(4; 10)]\fR to get a range as an array\.1473.1474.P1475The one argument form generates numbers from 0 to the given number, with an increment of 1\.1476.1477.P1478The two argument form generates numbers from \fBfrom\fR to \fBupto\fR with an increment of 1\.1479.1480.P1481The three argument form generates numbers \fBfrom\fR to \fBupto\fR with an increment of \fBby\fR\.1482.1483.IP "" 41484.1485.nf14861487jq \'range(2; 4)\'1488 null1489=> 2, 314901491jq \'[range(2; 4)]\'1492 null1493=> [2,3]14941495jq \'[range(4)]\'1496 null1497=> [0,1,2,3]14981499jq \'[range(0; 10; 3)]\'1500 null1501=> [0,3,6,9]15021503jq \'[range(0; 10; \-1)]\'1504 null1505=> []15061507jq \'[range(0; \-5; \-1)]\'1508 null1509=> [0,\-1,\-2,\-3,\-4]1510.1511.fi1512.1513.IP "" 01514.1515.SS "floor"1516The \fBfloor\fR function returns the floor of its numeric input\.1517.1518.IP "" 41519.1520.nf15211522jq \'floor\'1523 3\.141591524=> 31525.1526.fi1527.1528.IP "" 01529.1530.SS "sqrt"1531The \fBsqrt\fR function returns the square root of its numeric input\.1532.1533.IP "" 41534.1535.nf15361537jq \'sqrt\'1538 91539=> 31540.1541.fi1542.1543.IP "" 01544.1545.SS "tonumber"1546The \fBtonumber\fR function parses its input as a number\. It will convert correctly\-formatted strings to their numeric equivalent, leave numbers alone, and give an error on all other input\.1547.1548.IP "" 41549.1550.nf15511552jq \'\.[] | tonumber\'1553 [1, "1"]1554=> 1, 11555.1556.fi1557.1558.IP "" 01559.1560.SS "toboolean"1561The \fBtoboolean\fR function parses its input as a boolean\. It will convert correctly\-formatted strings to their boolean equivalent, leave booleans alone, and give an error on all other input\.1562.1563.IP "" 41564.1565.nf15661567jq \'\.[] | toboolean\'1568 ["true", "false", true, false]1569=> true, false, true, false1570.1571.fi1572.1573.IP "" 01574.1575.SS "tostring"1576The \fBtostring\fR function prints its input as a string\. Strings are left unchanged, and all other values are JSON\-encoded\.1577.1578.IP "" 41579.1580.nf15811582jq \'\.[] | tostring\'1583 [1, "1", [1]]1584=> "1", "1", "[1]"1585.1586.fi1587.1588.IP "" 01589.1590.SS "type"1591The \fBtype\fR function returns the type of its argument as a string, which is one of null, boolean, number, string, array or object\.1592.1593.IP "" 41594.1595.nf15961597jq \'map(type)\'1598 [0, false, [], {}, null, "hello"]1599=> ["number", "boolean", "array", "object", "null", "string"]1600.1601.fi1602.1603.IP "" 01604.1605.SS "infinite, nan, isinfinite, isnan, isfinite, isnormal"1606Some arithmetic operations can yield infinities and "not a number" (NaN) values\. The \fBisinfinite\fR builtin returns \fBtrue\fR if its input is infinite\. The \fBisnan\fR builtin returns \fBtrue\fR if its input is a NaN\. The \fBinfinite\fR builtin returns a positive infinite value\. The \fBnan\fR builtin returns a NaN\. The \fBisnormal\fR builtin returns true if its input is a normal number\.1607.1608.P1609Note that division by zero raises an error\.1610.1611.P1612Currently most arithmetic operations operating on infinities, NaNs, and sub\-normals do not raise errors\.1613.1614.IP "" 41615.1616.nf16171618jq \'\.[] | (infinite * \.) < 0\'1619 [\-1, 1]1620=> true, false16211622jq \'infinite, nan | type\'1623 null1624=> "number", "number"1625.1626.fi1627.1628.IP "" 01629.1630.SS "sort, sort_by(path_expression)"1631The \fBsort\fR functions sorts its input, which must be an array\. Values are sorted in the following order:1632.1633.IP "\(bu" 41634\fBnull\fR1635.1636.IP "\(bu" 41637\fBfalse\fR1638.1639.IP "\(bu" 41640\fBtrue\fR1641.1642.IP "\(bu" 41643numbers1644.1645.IP "\(bu" 41646strings, in alphabetical order (by unicode codepoint value)1647.1648.IP "\(bu" 41649arrays, in lexical order1650.1651.IP "\(bu" 41652objects1653.1654.IP "" 01655.1656.P1657The ordering for objects is a little complex: first they\'re compared by comparing their sets of keys (as arrays in sorted order), and if their keys are equal then the values are compared key by key\.1658.1659.P1660\fBsort_by\fR may be used to sort by a particular field of an object, or by applying any jq filter\. \fBsort_by(f)\fR compares two elements by comparing the result of \fBf\fR on each element\. When \fBf\fR produces multiple values, it firstly compares the first values, and the second values if the first values are equal, and so on\.1661.1662.IP "" 41663.1664.nf16651666jq \'sort\'1667 [8,3,null,6]1668=> [null,3,6,8]16691670jq \'sort_by(\.foo)\'1671 [{"foo":4, "bar":10}, {"foo":3, "bar":10}, {"foo":2, "bar":1}]1672=> [{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":4, "bar":10}]16731674jq \'sort_by(\.foo, \.bar)\'1675 [{"foo":4, "bar":10}, {"foo":3, "bar":20}, {"foo":2, "bar":1}, {"foo":3, "bar":10}]1676=> [{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":3, "bar":20}, {"foo":4, "bar":10}]1677.1678.fi1679.1680.IP "" 01681.1682.SS "group_by(path_expression)"1683\fBgroup_by(\.foo)\fR takes as input an array, groups the elements having the same \fB\.foo\fR field into separate arrays, and produces all of these arrays as elements of a larger array, sorted by the value of the \fB\.foo\fR field\.1684.1685.P1686Any jq expression, not just a field access, may be used in place of \fB\.foo\fR\. The sorting order is the same as described in the \fBsort\fR function above\.1687.1688.IP "" 41689.1690.nf16911692jq \'group_by(\.foo)\'1693 [{"foo":1, "bar":10}, {"foo":3, "bar":100}, {"foo":1, "bar":1}]1694=> [[{"foo":1, "bar":10}, {"foo":1, "bar":1}], [{"foo":3, "bar":100}]]1695.1696.fi1697.1698.IP "" 01699.1700.SS "min, max, min_by(path_exp), max_by(path_exp)"1701Find the minimum or maximum element of the input array\.1702.1703.P1704The \fBmin_by(path_exp)\fR and \fBmax_by(path_exp)\fR functions allow you to specify a particular field or property to examine, e\.g\. \fBmin_by(\.foo)\fR finds the object with the smallest \fBfoo\fR field\.1705.1706.IP "" 41707.1708.nf17091710jq \'min\'1711 [5,4,2,7]1712=> 217131714jq \'max_by(\.foo)\'1715 [{"foo":1, "bar":14}, {"foo":2, "bar":3}]1716=> {"foo":2, "bar":3}1717.1718.fi1719.1720.IP "" 01721.1722.SS "unique, unique_by(path_exp)"1723The \fBunique\fR function takes as input an array and produces an array of the same elements, in sorted order, with duplicates removed\.1724.1725.P1726The \fBunique_by(path_exp)\fR function will keep only one element for each value obtained by applying the argument\. Think of it as making an array by taking one element out of every group produced by \fBgroup\fR\.1727.1728.IP "" 41729.1730.nf17311732jq \'unique\'1733 [1,2,5,3,5,3,1,3]1734=> [1,2,3,5]17351736jq \'unique_by(\.foo)\'1737 [{"foo": 1, "bar": 2}, {"foo": 1, "bar": 3}, {"foo": 4, "bar": 5}]1738=> [{"foo": 1, "bar": 2}, {"foo": 4, "bar": 5}]17391740jq \'unique_by(length)\'1741 ["chunky", "bacon", "kitten", "cicada", "asparagus"]1742=> ["bacon", "chunky", "asparagus"]1743.1744.fi1745.1746.IP "" 01747.1748.SS "reverse"1749This function reverses an array\.1750.1751.IP "" 41752.1753.nf17541755jq \'reverse\'1756 [1,2,3,4]1757=> [4,3,2,1]1758.1759.fi1760.1761.IP "" 01762.1763.SS "contains(element)"1764The filter \fBcontains(b)\fR will produce true if b is completely contained within the input\. A string B is contained in a string A if B is a substring of A\. An array B is contained in an array A if all elements in B are contained in any element in A\. An object B is contained in object A if all of the values in B are contained in the value in A with the same key\. All other types are assumed to be contained in each other if they are equal\.1765.1766.IP "" 41767.1768.nf17691770jq \'contains("bar")\'1771 "foobar"1772=> true17731774jq \'contains(["baz", "bar"])\'1775 ["foobar", "foobaz", "blarp"]1776=> true17771778jq \'contains(["bazzzzz", "bar"])\'1779 ["foobar", "foobaz", "blarp"]1780=> false17811782jq \'contains({foo: 12, bar: [{barp: 12}]})\'1783 {"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]}1784=> true17851786jq \'contains({foo: 12, bar: [{barp: 15}]})\'1787 {"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]}1788=> false1789.1790.fi1791.1792.IP "" 01793.1794.SS "indices(s)"1795Outputs an array containing the indices in \fB\.\fR where \fBs\fR occurs\. The input may be an array, in which case if \fBs\fR is an array then the indices output will be those where all elements in \fB\.\fR match those of \fBs\fR\.1796.1797.IP "" 41798.1799.nf18001801jq \'indices(", ")\'1802 "a,b, cd, efg, hijk"1803=> [3,7,12]18041805jq \'indices(1)\'1806 [0,1,2,1,3,1,4]1807=> [1,3,5]18081809jq \'indices([1,2])\'1810 [0,1,2,3,1,4,2,5,1,2,6,7]1811=> [1,8]1812.1813.fi1814.1815.IP "" 01816.1817.SS "index(s), rindex(s)"1818Outputs the index of the first (\fBindex\fR) or last (\fBrindex\fR) occurrence of \fBs\fR in the input\.1819.1820.IP "" 41821.1822.nf18231824jq \'index(", ")\'1825 "a,b, cd, efg, hijk"1826=> 318271828jq \'index(1)\'1829 [0,1,2,1,3,1,4]1830=> 118311832jq \'index([1,2])\'1833 [0,1,2,3,1,4,2,5,1,2,6,7]1834=> 118351836jq \'rindex(", ")\'1837 "a,b, cd, efg, hijk"1838=> 1218391840jq \'rindex(1)\'1841 [0,1,2,1,3,1,4]1842=> 518431844jq \'rindex([1,2])\'1845 [0,1,2,3,1,4,2,5,1,2,6,7]1846=> 81847.1848.fi1849.1850.IP "" 01851.1852.SS "inside"1853The filter \fBinside(b)\fR will produce true if the input is completely contained within b\. It is, essentially, an inversed version of \fBcontains\fR\.1854.1855.IP "" 41856.1857.nf18581859jq \'inside("foobar")\'1860 "bar"1861=> true18621863jq \'inside(["foobar", "foobaz", "blarp"])\'1864 ["baz", "bar"]1865=> true18661867jq \'inside(["foobar", "foobaz", "blarp"])\'1868 ["bazzzzz", "bar"]1869=> false18701871jq \'inside({"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]})\'1872 {"foo": 12, "bar": [{"barp": 12}]}1873=> true18741875jq \'inside({"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]})\'1876 {"foo": 12, "bar": [{"barp": 15}]}1877=> false1878.1879.fi1880.1881.IP "" 01882.1883.SS "startswith(str)"1884Outputs \fBtrue\fR if \. starts with the given string argument\.1885.1886.IP "" 41887.1888.nf18891890jq \'[\.[]|startswith("foo")]\'1891 ["fo", "foo", "barfoo", "foobar", "barfoob"]1892=> [false, true, false, true, false]1893.1894.fi1895.1896.IP "" 01897.1898.SS "endswith(str)"1899Outputs \fBtrue\fR if \. ends with the given string argument\.1900.1901.IP "" 41902.1903.nf19041905jq \'[\.[]|endswith("foo")]\'1906 ["foobar", "barfoo"]1907=> [false, true]1908.1909.fi1910.1911.IP "" 01912.1913.SS "combinations, combinations(n)"1914Outputs all combinations of the elements of the arrays in the input array\. If given an argument \fBn\fR, it outputs all combinations of \fBn\fR repetitions of the input array\.1915.1916.IP "" 41917.1918.nf19191920jq \'combinations\'1921 [[1,2], [3, 4]]1922=> [1, 3], [1, 4], [2, 3], [2, 4]19231924jq \'combinations(2)\'1925 [0, 1]1926=> [0, 0], [0, 1], [1, 0], [1, 1]1927.1928.fi1929.1930.IP "" 01931.1932.SS "ltrimstr(str)"1933Outputs its input with the given prefix string removed, if it starts with it\.1934.1935.IP "" 41936.1937.nf19381939jq \'[\.[]|ltrimstr("foo")]\'1940 ["fo", "foo", "barfoo", "foobar", "afoo"]1941=> ["fo","","barfoo","bar","afoo"]1942.1943.fi1944.1945.IP "" 01946.1947.SS "rtrimstr(str)"1948Outputs its input with the given suffix string removed, if it ends with it\.1949.1950.IP "" 41951.1952.nf19531954jq \'[\.[]|rtrimstr("foo")]\'1955 ["fo", "foo", "barfoo", "foobar", "foob"]1956=> ["fo","","bar","foobar","foob"]1957.1958.fi1959.1960.IP "" 01961.1962.SS "trimstr(str)"1963Outputs its input with the given string removed at both ends, if it starts or ends with it\.1964.1965.IP "" 41966.1967.nf19681969jq \'[\.[]|trimstr("foo")]\'1970 ["fo", "foo", "barfoo", "foobarfoo", "foob"]1971=> ["fo","","bar","bar","b"]1972.1973.fi1974.1975.IP "" 01976.1977.SS "trim, ltrim, rtrim"1978\fBtrim\fR trims both leading and trailing whitespace\.1979.1980.P1981\fBltrim\fR trims only leading (left side) whitespace\.1982.1983.P1984\fBrtrim\fR trims only trailing (right side) whitespace\.1985.1986.P1987Whitespace characters are the usual \fB" "\fR, \fB"\en"\fR \fB"\et"\fR, \fB"\er"\fR and also all characters in the Unicode character database with the whitespace property\. Note that what is considered whitespace might change in the future\.1988.1989.IP "" 41990.1991.nf19921993jq \'trim, ltrim, rtrim\'1994 " abc "1995=> "abc", "abc ", " abc"1996.1997.fi1998.1999.IP "" 02000.The first 2,000 of 4,338 lines.