@port-labs/jq-node-bindings 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/.editorconfig +5 -0
  2. package/.jshintignore +1 -0
  3. package/.jshintrc +23 -0
  4. package/binding.gyp +56 -0
  5. package/configure +26 -0
  6. package/deps/jq/.gitattributes +2 -0
  7. package/deps/jq/.travis.yml +53 -0
  8. package/deps/jq/AUTHORS +73 -0
  9. package/deps/jq/COPYING +70 -0
  10. package/deps/jq/ChangeLog +1349 -0
  11. package/deps/jq/Makefile.am +198 -0
  12. package/deps/jq/NEWS +88 -0
  13. package/deps/jq/README.md +64 -0
  14. package/deps/jq/builtin.c +1684 -0
  15. package/deps/jq/builtin.h +10 -0
  16. package/deps/jq/bytecode.c +161 -0
  17. package/deps/jq/bytecode.h +92 -0
  18. package/deps/jq/compile-ios.sh +102 -0
  19. package/deps/jq/compile.c +1210 -0
  20. package/deps/jq/compile.h +101 -0
  21. package/deps/jq/config/m4/check-math-func.m4 +4 -0
  22. package/deps/jq/config/m4/find-func-no-libs.m4 +8 -0
  23. package/deps/jq/config/m4/find-func-no-libs2.m4 +62 -0
  24. package/deps/jq/config/m4/find-func.m4 +9 -0
  25. package/deps/jq/config/m4/misc.m4 +3 -0
  26. package/deps/jq/configure.ac +221 -0
  27. package/deps/jq/docs/Gemfile +7 -0
  28. package/deps/jq/docs/Gemfile.lock +63 -0
  29. package/deps/jq/docs/README.md +25 -0
  30. package/deps/jq/docs/Rakefile +145 -0
  31. package/deps/jq/docs/content/1.tutorial/default.yml +327 -0
  32. package/deps/jq/docs/content/2.download/default.yml +117 -0
  33. package/deps/jq/docs/content/3.manual/manual.yml +2878 -0
  34. package/deps/jq/docs/content/3.manual/v1.3/manual.yml +1270 -0
  35. package/deps/jq/docs/content/3.manual/v1.4/manual.yml +1672 -0
  36. package/deps/jq/docs/content/index/index.yml +51 -0
  37. package/deps/jq/docs/default_manpage.md +22 -0
  38. package/deps/jq/docs/public/.htaccess +28 -0
  39. package/deps/jq/docs/public/bootstrap/css/bootstrap-responsive.css +1058 -0
  40. package/deps/jq/docs/public/bootstrap/css/bootstrap-responsive.min.css +9 -0
  41. package/deps/jq/docs/public/bootstrap/css/bootstrap.css +5224 -0
  42. package/deps/jq/docs/public/bootstrap/css/bootstrap.min.css +9 -0
  43. package/deps/jq/docs/public/bootstrap/img/glyphicons-halflings-white.png +0 -0
  44. package/deps/jq/docs/public/bootstrap/img/glyphicons-halflings.png +0 -0
  45. package/deps/jq/docs/public/bootstrap/js/bootstrap.js +2027 -0
  46. package/deps/jq/docs/public/bootstrap/js/bootstrap.min.js +6 -0
  47. package/deps/jq/docs/public/css/base.scss +99 -0
  48. package/deps/jq/docs/public/jq.png +0 -0
  49. package/deps/jq/docs/public/robots.txt +2 -0
  50. package/deps/jq/docs/site.yml +18 -0
  51. package/deps/jq/docs/templates/default.liquid +34 -0
  52. package/deps/jq/docs/templates/index.liquid +60 -0
  53. package/deps/jq/docs/templates/manual.liquid +122 -0
  54. package/deps/jq/docs/templates/shared/_footer.liquid +5 -0
  55. package/deps/jq/docs/templates/shared/_head.liquid +12 -0
  56. package/deps/jq/docs/templates/shared/_header.liquid +26 -0
  57. package/deps/jq/exec_stack.h +112 -0
  58. package/deps/jq/execute.c +1155 -0
  59. package/deps/jq/inject_errors.c +112 -0
  60. package/deps/jq/jq.1.default +39 -0
  61. package/deps/jq/jq.1.prebuilt +3075 -0
  62. package/deps/jq/jq.h +60 -0
  63. package/deps/jq/jq.spec +70 -0
  64. package/deps/jq/jq_parser.h +9 -0
  65. package/deps/jq/jq_test.c +346 -0
  66. package/deps/jq/jv.c +1333 -0
  67. package/deps/jq/jv.h +240 -0
  68. package/deps/jq/jv_alloc.c +179 -0
  69. package/deps/jq/jv_alloc.h +27 -0
  70. package/deps/jq/jv_aux.c +619 -0
  71. package/deps/jq/jv_dtoa.c +4275 -0
  72. package/deps/jq/jv_dtoa.h +22 -0
  73. package/deps/jq/jv_file.c +49 -0
  74. package/deps/jq/jv_parse.c +852 -0
  75. package/deps/jq/jv_print.c +348 -0
  76. package/deps/jq/jv_unicode.c +96 -0
  77. package/deps/jq/jv_unicode.h +11 -0
  78. package/deps/jq/jv_utf8_tables.h +37 -0
  79. package/deps/jq/lexer.c +2442 -0
  80. package/deps/jq/lexer.h +362 -0
  81. package/deps/jq/lexer.l +184 -0
  82. package/deps/jq/libm.h +160 -0
  83. package/deps/jq/linker.c +393 -0
  84. package/deps/jq/linker.h +7 -0
  85. package/deps/jq/locfile.c +91 -0
  86. package/deps/jq/locfile.h +29 -0
  87. package/deps/jq/m4/ax_compare_version.m4 +177 -0
  88. package/deps/jq/m4/ax_prog_bison_version.m4 +68 -0
  89. package/deps/jq/main.c +566 -0
  90. package/deps/jq/opcode_list.h +44 -0
  91. package/deps/jq/parser.c +3914 -0
  92. package/deps/jq/parser.h +193 -0
  93. package/deps/jq/parser.y +923 -0
  94. package/deps/jq/scripts/crosscompile +42 -0
  95. package/deps/jq/scripts/gen_utf8_tables.py +32 -0
  96. package/deps/jq/scripts/version +5 -0
  97. package/deps/jq/setup.sh +33 -0
  98. package/deps/jq/tests/jq.test +1235 -0
  99. package/deps/jq/tests/jqtest +5 -0
  100. package/deps/jq/tests/mantest +7 -0
  101. package/deps/jq/tests/modules/.jq +5 -0
  102. package/deps/jq/tests/modules/a.jq +2 -0
  103. package/deps/jq/tests/modules/b/b.jq +2 -0
  104. package/deps/jq/tests/modules/c/c.jq +16 -0
  105. package/deps/jq/tests/modules/c/d.jq +1 -0
  106. package/deps/jq/tests/modules/data.json +4 -0
  107. package/deps/jq/tests/modules/lib/jq/e/e.jq +1 -0
  108. package/deps/jq/tests/modules/lib/jq/f.jq +1 -0
  109. package/deps/jq/tests/modules/syntaxerror/syntaxerror.jq +1 -0
  110. package/deps/jq/tests/modules/test_bind_order.jq +4 -0
  111. package/deps/jq/tests/modules/test_bind_order0.jq +1 -0
  112. package/deps/jq/tests/modules/test_bind_order1.jq +2 -0
  113. package/deps/jq/tests/modules/test_bind_order2.jq +2 -0
  114. package/deps/jq/tests/onig.supp +21 -0
  115. package/deps/jq/tests/onig.test +85 -0
  116. package/deps/jq/tests/onigtest +5 -0
  117. package/deps/jq/tests/setup +36 -0
  118. package/deps/jq/tests/shtest +205 -0
  119. package/deps/jq/tests/torture/input0.json +7 -0
  120. package/deps/jq/util.c +462 -0
  121. package/deps/jq/util.h +64 -0
  122. package/deps/jq.gyp +35 -0
  123. package/index.d.ts +3 -0
  124. package/jest.config.js +10 -0
  125. package/lib/index.js +14 -0
  126. package/package.json +48 -0
  127. package/reports/jest-port-api.xml +35 -0
  128. package/src/binding.cc +177 -0
  129. package/src/binding.h +13 -0
  130. package/test/santiy.test.js +122 -0
  131. package/util/configure.js +27 -0
@@ -0,0 +1,1672 @@
1
+ ---
2
+ headline: jq 1.4 Manual
3
+
4
+ history: |
5
+
6
+ *The manual for the development version of jq can be found
7
+ [here](/jq/manual).*
8
+
9
+ body: |
10
+
11
+ A jq program is a "filter": it takes an input, and produces an
12
+ output. There are a lot of builtin filters for extracting a
13
+ particular field of an object, or converting a number to a string,
14
+ or various other standard tasks.
15
+
16
+ Filters can be combined in various ways - you can pipe the output of
17
+ one filter into another filter, or collect the output of a filter
18
+ into an array.
19
+
20
+ Some filters produce multiple results, for instance there's one that
21
+ produces all the elements of its input array. Piping that filter
22
+ into a second runs the second filter for each element of the
23
+ array. Generally, things that would be done with loops and iteration
24
+ in other languages are just done by gluing filters together in jq.
25
+
26
+ It's important to remember that every filter has an input and an
27
+ output. Even literals like "hello" or 42 are filters - they take an
28
+ input but always produce the same literal as output. Operations that
29
+ combine two filters, like addition, generally feed the same input to
30
+ both and combine the results. So, you can implement an averaging
31
+ filter as `add / length` - feeding the input array both to the `add`
32
+ filter and the `length` filter and dividing the results.
33
+
34
+ But that's getting ahead of ourselves. :) Let's start with something
35
+ simpler:
36
+
37
+ manpage_intro: |
38
+ jq(1) -- Command-line JSON processor
39
+ ====================================
40
+
41
+ ## SYNOPSIS
42
+
43
+ `jq` [<options>...] <filter> [<files>...]
44
+
45
+ `jq` can transform JSON in various ways, by selecting, iterating,
46
+ reducing and otherwise mangling JSON documents. For instance,
47
+ running the command `jq 'map(.price) | add'` will take an array of
48
+ JSON objects as input and return the sum of their "price" fields.
49
+
50
+ By default, `jq` reads a stream of JSON objects (whitespace
51
+ separated) from `stdin`. One or more <files> may be specified, in
52
+ which case `jq` will read input from those instead.
53
+
54
+ The <options> are described in the [INVOKING JQ] section, they
55
+ mostly concern input and output formatting. The <filter> is written
56
+ in the jq language and specifies how to transform the input
57
+ document.
58
+
59
+ ## FILTERS
60
+
61
+ manpage_epilogue: |
62
+ ## BUGS
63
+
64
+ Presumably. Report them or discuss them at:
65
+
66
+ https://github.com/stedolan/jq/issues
67
+
68
+ ## AUTHOR
69
+
70
+ Stephen Dolan `<mu@netsoc.tcd.ie>`
71
+
72
+ sections:
73
+ - title: Invoking jq
74
+ body: |
75
+
76
+ jq filters run on a stream of JSON data. The input to jq is
77
+ parsed as a sequence of whitespace-separated JSON values which
78
+ are passed through the provided filter one at a time. The
79
+ output(s) of the filter are written to standard out, again as a
80
+ sequence of whitespace-separated JSON data.
81
+
82
+ Note: it is important to mind the shell's quoting rules. As a
83
+ general rule it's best to always quote (with single-quote
84
+ characters) the jq program, as too many characters with special
85
+ meaning to jq are also shell meta-characters. For example, `jq
86
+ "foo"` will fail on most Unix shells because that will be the same
87
+ as `jq foo`, which will generally fail because `foo is not
88
+ defined`. When using the Windows command shell (cmd.exe) it's
89
+ best to use double quotes around your jq program when given on the
90
+ command-line (instead of the `-f program-file` option), but then
91
+ double-quotes in the jq program need backslash escaping.
92
+
93
+ You can affect how jq reads and writes its input and output
94
+ using some command-line options:
95
+
96
+ * `--version`:
97
+
98
+ Output the jq version and exit with zero.
99
+
100
+ * `--slurp`/`-s`:
101
+
102
+ Instead of running the filter for each JSON object in the
103
+ input, read the entire input stream into a large array and run
104
+ the filter just once.
105
+
106
+ * `--online-input`/`-I`:
107
+
108
+ When the top-level input value is an array produce its elements
109
+ instead of the array. This allows on-line processing of
110
+ potentially very large top-level arrays' elements.
111
+
112
+ * `--raw-input`/`-R`:
113
+
114
+ Don't parse the input as JSON. Instead, each line of text is
115
+ passed to the filter as a string. If combined with `--slurp`,
116
+ then the entire input is passed to the filter as a single long
117
+ string.
118
+
119
+ * `--null-input`/`-n`:
120
+
121
+ Don't read any input at all! Instead, the filter is run once
122
+ using `null` as the input. This is useful when using jq as a
123
+ simple calculator or to construct JSON data from scratch.
124
+
125
+ * `--compact-output` / `-c`:
126
+
127
+ By default, jq pretty-prints JSON output. Using this option
128
+ will result in more compact output by instead putting each
129
+ JSON object on a single line.
130
+
131
+ * `--colour-output` / `-C` and `--monochrome-output` / `-M`:
132
+
133
+ By default, jq outputs colored JSON if writing to a
134
+ terminal. You can force it to produce color even if writing to
135
+ a pipe or a file using `-C`, and disable color with `-M`.
136
+
137
+ * `--ascii-output` / `-a`:
138
+
139
+ jq usually outputs non-ASCII Unicode codepoints as UTF-8, even
140
+ if the input specified them as escape sequences (like
141
+ "\u03bc"). Using this option, you can force jq to produce pure
142
+ ASCII output with every non-ASCII character replaced with the
143
+ equivalent escape sequence.
144
+
145
+ * `--unbuffered`
146
+
147
+ Flush the output after each JSON object is printed (useful if
148
+ you're piping a slow data source into jq and piping jq's
149
+ output elsewhere).
150
+
151
+ * `--sort-keys` / `-S`:
152
+
153
+ Output the fields of each object with the keys in sorted order.
154
+
155
+ * `--raw-output` / `-r`:
156
+
157
+ With this option, if the filter's result is a string then it
158
+ will be written directly to standard output rather than being
159
+ formatted as a JSON string with quotes. This can be useful for
160
+ making jq filters talk to non-JSON-based systems.
161
+
162
+ * `-f filename` / `--from-file filename`:
163
+
164
+ Read filter from the file rather than from a command line, like
165
+ awk's -f option. You can also use '#' to make comments.
166
+
167
+ * `-e` / `--exit-status`:
168
+
169
+ Sets the exit status of jq to 0 if the last output values was
170
+ neither `false` nor `null`, 1 if the last output value was
171
+ either `false` or `null`, or 4 if no valid result was ever
172
+ produced. Normally jq exits with 2 if there was any usage
173
+ problem or system error, 3 if there was a jq program compile
174
+ error, or 0 if the jq program ran.
175
+
176
+ * `--arg name value`:
177
+
178
+ This option passes a value to the jq program as a predefined
179
+ variable. If you run jq with `--arg foo bar`, then `$foo` is
180
+ available in the program and has the value `"bar"`.
181
+
182
+ * `--argfile name filename`:
183
+
184
+ This option passes the first value from the named file as a
185
+ value to the jq program as a predefined variable. If you run jq
186
+ with `--argfile foo bar`, then `$foo` is available in the
187
+ program and has the value resulting from parsing the content of
188
+ the file named `bar`.
189
+
190
+ - title: Basic filters
191
+ entries:
192
+ - title: "`.`"
193
+ body: |
194
+
195
+ The absolute simplest (and least interesting) filter
196
+ is `.`. This is a filter that takes its input and
197
+ produces it unchanged as output.
198
+
199
+ Since jq by default pretty-prints all output, this trivial
200
+ program can be a useful way of formatting JSON output from,
201
+ say, `curl`.
202
+
203
+ examples:
204
+ - program: '.'
205
+ input: '"Hello, world!"'
206
+ output: ['"Hello, world!"']
207
+
208
+ - title: "`.foo`, `.foo.bar`"
209
+ body: |
210
+
211
+ The simplest *useful* filter is `.foo`. When given a
212
+ JSON object (aka dictionary or hash) as input, it produces
213
+ the value at the key "foo", or null if there's none present.
214
+
215
+ If the key contains special characters, you need to surround
216
+ it with double quotes like this: `."foo$"`.
217
+
218
+ A filter of the form `.foo.bar` is equivalent to `.foo|.bar`.
219
+
220
+ examples:
221
+ - program: '.foo'
222
+ input: '{"foo": 42, "bar": "less interesting data"}'
223
+ output: [42]
224
+ - program: '.foo'
225
+ input: '{"notfoo": true, "alsonotfoo": false}'
226
+ output: ['null']
227
+ - program: '.["foo"]'
228
+ input: '{"foo": 42}'
229
+ output: [42]
230
+
231
+ - title: "`.foo?`"
232
+ body: |
233
+
234
+ Just like `.foo`, but does not output even an error when `.`
235
+ is not an array or an object.
236
+
237
+ examples:
238
+ - program: '.foo?'
239
+ input: '{"foo": 42, "bar": "less interesting data"}'
240
+ output: [42]
241
+ - program: '.foo?'
242
+ input: '{"notfoo": true, "alsonotfoo": false}'
243
+ output: ['null']
244
+ - program: '.["foo"]?'
245
+ input: '{"foo": 42}'
246
+ output: [42]
247
+ - program: '[.foo?]'
248
+ input: '[1,2]'
249
+ output: ['[]']
250
+
251
+ - title: "`.[<string>]`, `.[2]`, `.[10:15]`"
252
+ body: |
253
+
254
+ You can also look up fields of an object using syntax like
255
+ `.["foo"]` (.foo above is a shorthand version of this). This
256
+ one works for arrays as well, if the key is an
257
+ integer. Arrays are zero-based (like javascript), so `.[2]`
258
+ returns the third element of the array.
259
+
260
+ The `.[10:15]` syntax can be used to return a subarray of an
261
+ array or substring of a string. The array returned by
262
+ `.[10:15]` will be of length 5, containing the elements from
263
+ index 10 (inclusive) to index 15 (exclusive). Either index may
264
+ be negative (in which case it counts backwards from the end of
265
+ the array), or omitted (in which case it refers to the start
266
+ or end of the array).
267
+
268
+ The `?` "operator" can also be used with the slice operator,
269
+ as in `.[10:15]?`, which outputs values where the inputs are
270
+ slice-able.
271
+
272
+ examples:
273
+ - program: '.[0]'
274
+ input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
275
+ output: ['{"name":"JSON", "good":true}']
276
+
277
+ - program: '.[2]'
278
+ input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
279
+ output: ['null']
280
+
281
+ - program: '.[2:4]'
282
+ input: '["a","b","c","d","e"]'
283
+ output: ['["c", "d"]']
284
+
285
+ - program: '.[2:4]'
286
+ input: '"abcdefghi"'
287
+ output: ['"cd"']
288
+
289
+ - program: '.[:3]'
290
+ input: '["a","b","c","d","e"]'
291
+ output: ['["a", "b", "c"]']
292
+
293
+ - program: '.[-2:]'
294
+ input: '["a","b","c","d","e"]'
295
+ output: ['["d", "e"]']
296
+
297
+ - title: "`.[]`"
298
+ body: |
299
+
300
+ If you use the `.[index]` syntax, but omit the index
301
+ entirely, it will return *all* of the elements of an
302
+ array. Running `.[]` with the input `[1,2,3]` will produce the
303
+ numbers as three separate results, rather than as a single
304
+ array.
305
+
306
+ You can also use this on an object, and it will return all
307
+ the values of the object.
308
+
309
+ examples:
310
+ - program: '.[]'
311
+ input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
312
+ output:
313
+ - '{"name":"JSON", "good":true}'
314
+ - '{"name":"XML", "good":false}'
315
+
316
+ - program: '.[]'
317
+ input: '[]'
318
+ output: []
319
+
320
+ - program: '.[]'
321
+ input: '{"a": 1, "b": 1}'
322
+ output: ['1', '1']
323
+
324
+ - title: "`.[]?`"
325
+ body: |
326
+
327
+ Like `.[]`, but no errors will be output if . is not an array
328
+ or object.
329
+
330
+ - title: "`,`"
331
+ body: |
332
+
333
+ If two filters are separated by a comma, then the
334
+ input will be fed into both and there will be multiple
335
+ outputs: first, all of the outputs produced by the left
336
+ expression, and then all of the outputs produced by the
337
+ right. For instance, filter `.foo, .bar`, produces
338
+ both the "foo" fields and "bar" fields as separate outputs.
339
+
340
+ examples:
341
+ - program: '.foo, .bar'
342
+ input: '{"foo": 42, "bar": "something else", "baz": true}'
343
+ output: ['42', '"something else"']
344
+
345
+ - program: ".user, .projects[]"
346
+ input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'
347
+ output: ['"stedolan"', '"jq"', '"wikiflow"']
348
+
349
+ - program: '.[4,2]'
350
+ input: '["a","b","c","d","e"]'
351
+ output: ['"e"', '"c"']
352
+
353
+ - title: "`|`"
354
+ body: |
355
+ The | operator combines two filters by feeding the output(s) of
356
+ the one on the left into the input of the one on the right. It's
357
+ pretty much the same as the Unix shell's pipe, if you're used to
358
+ that.
359
+
360
+ If the one on the left produces multiple results, the one on
361
+ the right will be run for each of those results. So, the
362
+ expression `.[] | .foo` retrieves the "foo" field of each
363
+ element of the input array.
364
+
365
+ examples:
366
+ - program: '.[] | .name'
367
+ input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
368
+ output: ['"JSON"', '"XML"']
369
+
370
+ - title: Types and Values
371
+ body: |
372
+
373
+ jq supports the same set of datatypes as JSON - numbers,
374
+ strings, booleans, arrays, objects (which in JSON-speak are
375
+ hashes with only string keys), and "null".
376
+
377
+ Booleans, null, strings and numbers are written the same way as
378
+ in javascript. Just like everything else in jq, these simple
379
+ values take an input and produce an output - `42` is a valid jq
380
+ expression that takes an input, ignores it, and returns 42
381
+ instead.
382
+
383
+ entries:
384
+ - title: Array construction - `[]`
385
+ body: |
386
+
387
+ As in JSON, `[]` is used to construct arrays, as in
388
+ `[1,2,3]`. The elements of the arrays can be any jq
389
+ expression. All of the results produced by all of the
390
+ expressions are collected into one big array. You can use it
391
+ to construct an array out of a known quantity of values (as
392
+ in `[.foo, .bar, .baz]`) or to "collect" all the results of a
393
+ filter into an array (as in `[.items[].name]`)
394
+
395
+ Once you understand the "," operator, you can look at jq's array
396
+ syntax in a different light: the expression `[1,2,3]` is not using a
397
+ built-in syntax for comma-separated arrays, but is instead applying
398
+ the `[]` operator (collect results) to the expression 1,2,3 (which
399
+ produces three different results).
400
+
401
+ If you have a filter `X` that produces four results,
402
+ then the expression `[X]` will produce a single result, an
403
+ array of four elements.
404
+
405
+ examples:
406
+ - program: "[.user, .projects[]]"
407
+ input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'
408
+ output: ['["stedolan", "jq", "wikiflow"]']
409
+ - title: Objects - `{}`
410
+ body: |
411
+
412
+ Like JSON, `{}` is for constructing objects (aka
413
+ dictionaries or hashes), as in: `{"a": 42, "b": 17}`.
414
+
415
+ If the keys are "sensible" (all alphabetic characters), then
416
+ the quotes can be left off. The value can be any expression
417
+ (although you may need to wrap it in parentheses if it's a
418
+ complicated one), which gets applied to the {} expression's
419
+ input (remember, all filters have an input and an
420
+ output).
421
+
422
+ {foo: .bar}
423
+
424
+ will produce the JSON object `{"foo": 42}` if given the JSON
425
+ object `{"bar":42, "baz":43}`. You can use this to select
426
+ particular fields of an object: if the input is an object
427
+ with "user", "title", "id", and "content" fields and you
428
+ just want "user" and "title", you can write
429
+
430
+ {user: .user, title: .title}
431
+
432
+ Because that's so common, there's a shortcut syntax: `{user, title}`.
433
+
434
+ If one of the expressions produces multiple results,
435
+ multiple dictionaries will be produced. If the input's
436
+
437
+ {"user":"stedolan","titles":["JQ Primer", "More JQ"]}
438
+
439
+ then the expression
440
+
441
+ {user, title: .titles[]}
442
+
443
+ will produce two outputs:
444
+
445
+ {"user":"stedolan", "title": "JQ Primer"}
446
+ {"user":"stedolan", "title": "More JQ"}
447
+
448
+ Putting parentheses around the key means it will be evaluated as an
449
+ expression. With the same input as above,
450
+
451
+ {(.user): .titles}
452
+
453
+ produces
454
+
455
+ {"stedolan": ["JQ Primer", "More JQ"]}
456
+
457
+ examples:
458
+ - program: '{user, title: .titles[]}'
459
+ input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
460
+ output:
461
+ - '{"user":"stedolan", "title": "JQ Primer"}'
462
+ - '{"user":"stedolan", "title": "More JQ"}'
463
+ - program: '{(.user): .titles}'
464
+ input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
465
+ output: ['{"stedolan": ["JQ Primer", "More JQ"]}']
466
+
467
+ - title: Builtin operators and functions
468
+ body: |
469
+
470
+ Some jq operator (for instance, `+`) do different things
471
+ depending on the type of their arguments (arrays, numbers,
472
+ etc.). However, jq never does implicit type conversions. If you
473
+ try to add a string to an object you'll get an error message and
474
+ no result.
475
+
476
+ entries:
477
+ - title: Addition - `+`
478
+ body: |
479
+
480
+ The operator `+` takes two filters, applies them both
481
+ to the same input, and adds the results together. What
482
+ "adding" means depends on the types involved:
483
+
484
+ - **Numbers** are added by normal arithmetic.
485
+
486
+ - **Arrays** are added by being concatenated into a larger array.
487
+
488
+ - **Strings** are added by being joined into a larger string.
489
+
490
+ - **Objects** are added by merging, that is, inserting all
491
+ the key-value pairs from both objects into a single
492
+ combined object. If both objects contain a value for the
493
+ same key, the object on the right of the `+` wins. (For
494
+ recursive merge use the `*` operator.)
495
+
496
+ `null` can be added to any value, and returns the other
497
+ value unchanged.
498
+
499
+ examples:
500
+ - program: '.a + 1'
501
+ input: '{"a": 7}'
502
+ output: ['8']
503
+ - program: '.a + .b'
504
+ input: '{"a": [1,2], "b": [3,4]}'
505
+ output: ['[1,2,3,4]']
506
+ - program: '.a + null'
507
+ input: '{"a": 1}'
508
+ output: ['1']
509
+ - program: '.a + 1'
510
+ input: '{}'
511
+ output: ['1']
512
+ - program: '{a: 1} + {b: 2} + {c: 3} + {a: 42}'
513
+ input: 'null'
514
+ output: ['{"a": 42, "b": 2, "c": 3}']
515
+
516
+ - title: Subtraction - `-`
517
+ body: |
518
+
519
+ As well as normal arithmetic subtraction on numbers, the `-`
520
+ operator can be used on arrays to remove all occurences of
521
+ the second array's elements from the first array.
522
+
523
+ examples:
524
+ - program: '4 - .a'
525
+ input: '{"a":3}'
526
+ output: ['1']
527
+ - program: . - ["xml", "yaml"]
528
+ input: '["xml", "yaml", "json"]'
529
+ output: ['["json"]']
530
+
531
+ - title: Multiplication, division, modulo - `*`, `/`, and `%`
532
+ body: |
533
+
534
+ These operators only work on numbers, and do the expected.
535
+
536
+ Multiplying a string by a number produces the concatenation of
537
+ that string that many times.
538
+
539
+ Dividing a string by another splits the first using the second
540
+ as separators.
541
+
542
+ Multiplying two objects will merge them recursively: this works
543
+ like addition but if both objects contain a value for the
544
+ same key, and the values are objects, the two are merged with
545
+ the same strategy.
546
+
547
+ examples:
548
+ - program: '10 / . * 3'
549
+ input: 5
550
+ output: [6]
551
+ - program: '. / ", "'
552
+ input: '"a, b,c,d, e"'
553
+ output: ['["a","b,c,d","e"]']
554
+ - program: '{"k": {"a": 1, "b": 2}} * {"k": {"a": 0,"c": 3}}'
555
+ input: 'null'
556
+ output: ['{"k": {"a": 0, "b": 2, "c": 3}}']
557
+
558
+ - title: "`length`"
559
+ body: |
560
+
561
+ The builtin function `length` gets the length of various
562
+ different types of value:
563
+
564
+ - The length of a **string** is the number of Unicode
565
+ codepoints it contains (which will be the same as its
566
+ JSON-encoded length in bytes if it's pure ASCII).
567
+
568
+ - The length of an **array** is the number of elements.
569
+
570
+ - The length of an **object** is the number of key-value pairs.
571
+
572
+ - The length of **null** is zero.
573
+
574
+ examples:
575
+ - program: '.[] | length'
576
+ input: '[[1,2], "string", {"a":2}, null]'
577
+ output: [2, 6, 1, 0]
578
+
579
+ - title: "`keys`"
580
+ body: |
581
+
582
+ The builtin function `keys`, when given an object, returns
583
+ its keys in an array.
584
+
585
+ The keys are sorted "alphabetically", by unicode codepoint
586
+ order. This is not an order that makes particular sense in
587
+ any particular language, but you can count on it being the
588
+ same for any two objects with the same set of keys,
589
+ regardless of locale settings.
590
+
591
+ When `keys` is given an array, it returns the valid indices
592
+ for that array: the integers from 0 to length-1.
593
+
594
+ examples:
595
+ - program: 'keys'
596
+ input: '{"abc": 1, "abcd": 2, "Foo": 3}'
597
+ output: ['["Foo", "abc", "abcd"]']
598
+ - program: 'keys'
599
+ input: '[42,3,35]'
600
+ output: ['[0,1,2]']
601
+
602
+ - title: "`has`"
603
+ body: |
604
+
605
+ The builtin function `has` returns whether the input object
606
+ has the given key, or the input array has an element at the
607
+ given index.
608
+
609
+ `has($key)` has the same effect as checking whether `$key`
610
+ is a member of the array returned by `keys`, although `has`
611
+ will be faster.
612
+
613
+ examples:
614
+ - program: 'map(has("foo"))'
615
+ input: '[{"foo": 42}, {}]'
616
+ output: ['[true, false]']
617
+ - program: 'map(has(2))'
618
+ input: '[[0,1], ["a","b","c"]]'
619
+ output: ['[false, true]']
620
+
621
+ - title: "`del`"
622
+ body: |
623
+
624
+ The builtin function `del` removes a key and its corresponding
625
+ value from an object.
626
+
627
+ examples:
628
+ - program: 'del(.foo)'
629
+ input: '[{"foo": 42, "bar": 9001, "baz": 42}]'
630
+ output: ['{"bar": 9001, "baz": 42}']
631
+ - program: 'del(.[1, 2])'
632
+ input: '[["foo", "bar", "baz"]]'
633
+ output: ['["foo"]']
634
+
635
+ - title: "`to_entries`, `from_entries`, `with_entries`"
636
+ body: |
637
+
638
+ These functions convert between an object and an array of
639
+ key-value pairs. If `to_entries` is passed an object, then
640
+ for each `k: v` entry in the input, the output array
641
+ includes `{"key": k, "value": v}`.
642
+
643
+ `from_entries` does the opposite conversion, and
644
+ `with_entries(foo)` is a shorthand for `to_entries |
645
+ map(foo) | from_entries`, useful for doing some operation to
646
+ all keys and values of an object.
647
+
648
+ examples:
649
+ - program: 'to_entries'
650
+ input: '{"a": 1, "b": 2}'
651
+ output: ['[{"key":"a", "value":1}, {"key":"b", "value":2}]']
652
+ - program: 'from_entries'
653
+ input: '[{"key":"a", "value":1}, {"key":"b", "value":2}]'
654
+ output: ['{"a": 1, "b": 2}']
655
+ - program: 'with_entries(.key |= "KEY_" + .)'
656
+ input: '{"a": 1, "b": 2}'
657
+ output: ['{"KEY_a": 1, "KEY_b": 2}']
658
+
659
+
660
+ - title: "`select`"
661
+ body: |
662
+
663
+ The function `select(foo)` produces its input unchanged if
664
+ `foo` returns true for that input, and produces no output
665
+ otherwise.
666
+
667
+ It's useful for filtering lists: `[1,2,3] | map(select(. >= 2))`
668
+ will give you `[2,3]`.
669
+
670
+ examples:
671
+ - program: 'map(select(. >= 2))'
672
+ input: '[1,5,3,0,7]'
673
+ output: ['[5,3,7]']
674
+
675
+
676
+ - title: "`arrays`, `objects`, `iterables`, `booleans`, `numbers`, `strings`, `nulls`, `values`, `scalars`"
677
+ body: |
678
+
679
+ These built-ins select only inputs that are arrays, objects,
680
+ iterables (arrays or objects), booleans, numbers, strings,
681
+ null, non-null values, and non-iterables, respectively.
682
+
683
+ examples:
684
+ - program: '.[]|numbers'
685
+ input: '[[],{},1,"foo",null,true,false]'
686
+ output: ['1']
687
+
688
+ - title: "`empty`"
689
+ body: |
690
+
691
+ `empty` returns no results. None at all. Not even `null`.
692
+
693
+ It's useful on occasion. You'll know if you need it :)
694
+
695
+ examples:
696
+ - program: '1, empty, 2'
697
+ input: 'null'
698
+ output: [1, 2]
699
+ - program: '[1,2,empty,3]'
700
+ input: 'null'
701
+ output: ['[1,2,3]']
702
+
703
+ - title: "`map(x)`"
704
+ body: |
705
+
706
+ For any filter `x`, `map(x)` will run that filter for each
707
+ element of the input array, and produce the outputs a new
708
+ array. `map(.+1)` will increment each element of an array of numbers.
709
+
710
+ `map(x)` is equivalent to `[.[] | x]`. In fact, this is how
711
+ it's defined.
712
+
713
+ examples:
714
+ - program: 'map(.+1)'
715
+ input: '[1,2,3]'
716
+ output: ['[2,3,4]']
717
+
718
+ - title: "`paths`"
719
+ body: |
720
+
721
+ Outputs the paths to all the elements in its input (except it
722
+ does not output the empty list, representing . itself).
723
+
724
+ `paths` is equivalent to
725
+
726
+ def paths: path(recurse(if (type|. == "array" or . == "object") then .[] else empty end))|select(length > 0);
727
+
728
+ examples:
729
+ - program: '[paths]'
730
+ input: '[1,[[],{"a":2}]]'
731
+ output: ['[[0],[1],[1,0],[1,1],[1,1,"a"]]']
732
+
733
+ - title: "`leaf_paths`"
734
+ body: |
735
+
736
+ Outputs the paths to all the leaves (non-array, non-object
737
+ elements) in its input.
738
+
739
+ examples:
740
+ - program: '[leaf_paths]'
741
+ input: '[1,[[],{"a":2}]]'
742
+ output: ['[[0],[1,1,"a"]]']
743
+
744
+ - title: "`add`"
745
+ body: |
746
+
747
+ The filter `add` takes as input an array, and produces as
748
+ output the elements of the array added together. This might
749
+ mean summed, concatenated or merged depending on the types
750
+ of the elements of the input array - the rules are the same
751
+ as those for the `+` operator (described above).
752
+
753
+ If the input is an empty array, `add` returns `null`.
754
+
755
+ examples:
756
+ - program: add
757
+ input: '["a","b","c"]'
758
+ output: ['"abc"']
759
+ - program: add
760
+ input: '[1, 2, 3]'
761
+ output: [6]
762
+ - program: add
763
+ input: '[]'
764
+ output: ["null"]
765
+
766
+ - title: "`any`"
767
+ body: |
768
+
769
+ The filter `any` takes as input an array of boolean values,
770
+ and produces `true` as output if any of the the elements of
771
+ the array is `true`.
772
+
773
+ If the input is an empty array, `any` returns `false`.
774
+
775
+ examples:
776
+ - program: any
777
+ input: '[true, false]'
778
+ output: ["true"]
779
+ - program: any
780
+ input: '[false, false]'
781
+ output: ["false"]
782
+ - program: any
783
+ input: '[]'
784
+ output: ["false"]
785
+
786
+ - title: "`all`"
787
+ body: |
788
+
789
+ The filter `all` takes as input an array of boolean values,
790
+ and produces `true` as output if all of the the elements of
791
+ the array are `true`.
792
+
793
+ If the input is an empty array, `all` returns `true`.
794
+
795
+ examples:
796
+ - program: all
797
+ input: '[true, false]'
798
+ output: ["false"]
799
+ - program: all
800
+ input: '[true, true]'
801
+ output: ["true"]
802
+ - program: all
803
+ input: '[]'
804
+ output: ["true"]
805
+
806
+ - title: "`range`"
807
+ body: |
808
+
809
+ The `range` function produces a range of numbers. `range(4;10)`
810
+ produces 6 numbers, from 4 (inclusive) to 10 (exclusive). The numbers
811
+ are produced as separate outputs. Use `[range(4;10)]` to get a range as
812
+ an array.
813
+
814
+ examples:
815
+ - program: 'range(2;4)'
816
+ input: 'null'
817
+ output: ['2', '3']
818
+ - program: '[range(2;4)]'
819
+ input: 'null'
820
+ output: ['[2,3]']
821
+
822
+ - title: "`floor`"
823
+ body: |
824
+
825
+ The `floor` function returns the floor of its numeric input.
826
+
827
+ examples:
828
+ - program: 'floor'
829
+ input: '3.14159'
830
+ output: ['3']
831
+
832
+ - title: "`sqrt`"
833
+ body: |
834
+
835
+ The `sqrt` function returns the square root of its numeric input.
836
+
837
+ examples:
838
+ - program: 'sqrt'
839
+ input: '9'
840
+ output: ['3']
841
+
842
+ - title: "`tonumber`"
843
+ body: |
844
+
845
+ The `tonumber` function parses its input as a number. It
846
+ will convert correctly-formatted strings to their numeric
847
+ equivalent, leave numbers alone, and give an error on all other input.
848
+
849
+ examples:
850
+ - program: '.[] | tonumber'
851
+ input: '[1, "1"]'
852
+ output: [1, 1]
853
+
854
+ - title: "`tostring`"
855
+ body: |
856
+
857
+ The `tostring` function prints its input as a
858
+ string. Strings are left unchanged, and all other values are
859
+ JSON-encoded.
860
+
861
+ examples:
862
+ - program: '.[] | tostring'
863
+ input: '[1, "1", [1]]'
864
+ output: ['"1"', '"1"', '"[1]"']
865
+
866
+ - title: "`type`"
867
+ body: |
868
+
869
+ The `type` function returns the type of its argument as a
870
+ string, which is one of null, boolean, number, string, array
871
+ or object.
872
+
873
+ examples:
874
+ - program: 'map(type)'
875
+ input: '[0, false, [], {}, null, "hello"]'
876
+ output: ['["number", "boolean", "array", "object", "null", "string"]']
877
+
878
+ - title: "`sort, sort_by`"
879
+ body: |
880
+
881
+ The `sort` functions sorts its input, which must be an
882
+ array. Values are sorted in the following order:
883
+
884
+ * `null`
885
+ * `false`
886
+ * `true`
887
+ * numbers
888
+ * strings, in alphabetical order (by unicode codepoint value)
889
+ * arrays, in lexical order
890
+ * objects
891
+
892
+ The ordering for objects is a little complex: first they're
893
+ compared by comparing their sets of keys (as arrays in
894
+ sorted order), and if their keys are equal then the values
895
+ are compared key by key.
896
+
897
+ `sort_by` may be used to sort by a particular field of an
898
+ object, or by applying any jq filter. `sort_by(foo)`
899
+ compares two elements by comparing the result of `foo` on
900
+ each element.
901
+
902
+ examples:
903
+ - program: 'sort'
904
+ input: '[8,3,null,6]'
905
+ output: ['[null,3,6,8]']
906
+ - program: 'sort_by(.foo)'
907
+ input: '[{"foo":4, "bar":10}, {"foo":3, "bar":100}, {"foo":2, "bar":1}]'
908
+ output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":100}, {"foo":4, "bar":10}]']
909
+
910
+ - title: "`group_by`"
911
+ body: |
912
+
913
+ `group_by(.foo)` takes as input an array, groups the
914
+ elements having the same `.foo` field into separate arrays,
915
+ and produces all of these arrays as elements of a larger
916
+ array, sorted by the value of the `.foo` field.
917
+
918
+ Any jq expression, not just a field access, may be used in
919
+ place of `.foo`. The sorting order is the same as described
920
+ in the `sort` function above.
921
+
922
+ examples:
923
+ - program: 'group_by(.foo)'
924
+ input: '[{"foo":1, "bar":10}, {"foo":3, "bar":100}, {"foo":1, "bar":1}]'
925
+ output: ['[[{"foo":1, "bar":10}, {"foo":1, "bar":1}], [{"foo":3, "bar":100}]]']
926
+
927
+ - title: "`min`, `max`, `min_by`, `max_by`"
928
+ body: |
929
+
930
+ Find the minimum or maximum element of the input array. The
931
+ `_by` versions allow you to specify a particular field or
932
+ property to examine, e.g. `min_by(.foo)` finds the object
933
+ with the smallest `foo` field.
934
+
935
+ examples:
936
+ - program: 'min'
937
+ input: '[5,4,2,7]'
938
+ output: ['2']
939
+ - program: 'max_by(.foo)'
940
+ input: '[{"foo":1, "bar":14}, {"foo":2, "bar":3}]'
941
+ output: ['{"foo":2, "bar":3}']
942
+
943
+ - title: "`unique`"
944
+ body: |
945
+
946
+ The `unique` function takes as input an array and produces
947
+ an array of the same elements, in sorted order, with
948
+ duplicates removed.
949
+
950
+ examples:
951
+ - program: 'unique'
952
+ input: '[1,2,5,3,5,3,1,3]'
953
+ output: ['[1,2,3,5]']
954
+
955
+ - title: "`unique_by`"
956
+ body: |
957
+
958
+ The `unique_by(.foo)` function takes as input an array and produces
959
+ an array of the same elements, in sorted order, with
960
+ elqements with a duplicate `.foo` field removed. Think of it as making
961
+ an array by taking one element out of every group produced by
962
+ `group_by`.
963
+
964
+ examples:
965
+ - program: 'unique_by(.foo)'
966
+ input: '[{"foo": 1, "bar": 2}, {"foo": 1, "bar": 3}, {"foo": 4, "bar": 5}]'
967
+ output: ['[{"foo": 1, "bar": 2}, {"foo": 4, "bar": 5}]']
968
+ - program: 'unique_by(length)'
969
+ input: '["chunky", "bacon", "kitten", "cicada", "asparagus"]'
970
+ output: ['["chunky", "bacon", "asparagus"]']
971
+
972
+
973
+ - title: "`reverse`"
974
+ body: |
975
+
976
+ This function reverses an array.
977
+
978
+ examples:
979
+ - program: 'reverse'
980
+ input: '[1,2,3,4]'
981
+ output: ['[4,3,2,1]']
982
+
983
+ - title: "`contains`"
984
+ body: |
985
+
986
+ The filter `contains(b)` will produce true if b is
987
+ completely contained within the input. A string B is
988
+ contained in a string A if B is a substring of A. An array B
989
+ is contained in an array A is all elements in B are
990
+ contained in any element in A. An object B is contained in
991
+ object A if all of the values in B are contained in the
992
+ value in A with the same key. All other types are assumed to
993
+ be contained in each other if they are equal.
994
+
995
+ examples:
996
+ - program: 'contains("bar")'
997
+ input: '"foobar"'
998
+ output: ['true']
999
+ - program: 'contains(["baz", "bar"])'
1000
+ input: '["foobar", "foobaz", "blarp"]'
1001
+ output: ['true']
1002
+ - program: 'contains(["bazzzzz", "bar"])'
1003
+ input: '["foobar", "foobaz", "blarp"]'
1004
+ output: ['false']
1005
+ - program: 'contains({foo: 12, bar: [{barp: 12}]})'
1006
+ input: '{"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]}'
1007
+ output: ['true']
1008
+ - program: 'contains({foo: 12, bar: [{barp: 15}]})'
1009
+ input: '{"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]}'
1010
+ output: ['false']
1011
+
1012
+ - title: "`indices(s)`"
1013
+ body: |
1014
+
1015
+ Outputs an array containing the indices in `.` where `s`
1016
+ occurs. The input may be an array, in which case if `s` is an
1017
+ array then the indices output will be those where all elements
1018
+ in `.` match those of `s`.
1019
+
1020
+ examples:
1021
+ - program: 'indices(", ")'
1022
+ input: '"a,b, cd, efg, hijk"'
1023
+ output: ['[3,7,12]']
1024
+ - program: 'indices(1)'
1025
+ input: '[0,1,2,1,3,1,4]'
1026
+ output: ['[1,3,5]']
1027
+ - program: 'indices([1,2])'
1028
+ input: '[0,1,2,3,1,4,2,5,1,2,6,7]'
1029
+ output: ['[1,8]']
1030
+
1031
+ - title: "`index(s)`, `rindex(s)`"
1032
+ body: |
1033
+
1034
+ Outputs the index of the first (`index`) or last (`rindex`)
1035
+ occurrence of `s` in the input.
1036
+
1037
+ examples:
1038
+ - program: 'index(", ")'
1039
+ input: '"a,b, cd, efg, hijk"'
1040
+ output: ['3']
1041
+ - program: 'rindex(", ")]'
1042
+ input: '"a,b, cd, efg, hijk"'
1043
+ output: ['12']
1044
+
1045
+ - title: "`startswith`"
1046
+ body: |
1047
+
1048
+ Outputs `true` if . starts with the given string argument.
1049
+
1050
+ examples:
1051
+ - program: '[.[]|startswith("foo")]'
1052
+ input: '["fo", "foo", "barfoo", "foobar", "barfoob"]'
1053
+ output: ['[false, true, false, true, false]']
1054
+
1055
+ - title: "`endswith`"
1056
+ body: |
1057
+
1058
+ Outputs `true` if . ends with the given string argument.
1059
+
1060
+ examples:
1061
+ - program: '[.[]|endswith("foo")]'
1062
+ input: '["foobar", "barfoo"]'
1063
+ output: ['[false, true, true, false, false]']
1064
+
1065
+ - title: "`ltrimstr`"
1066
+ body: |
1067
+
1068
+ Outputs its input with the given prefix string removed, if it
1069
+ starts with it.
1070
+
1071
+ examples:
1072
+ - program: '[.[]|ltrimstr("foo")]'
1073
+ input: '["fo", "foo", "barfoo", "foobar", "afoo"]'
1074
+ output: ['["fo","","barfoo","bar","afoo"]']
1075
+
1076
+ - title: "`rtrimstr`"
1077
+ body: |
1078
+
1079
+ Outputs its input with the given suffix string removed, if it
1080
+ starts with it.
1081
+
1082
+ examples:
1083
+ - program: '[.[]|rtrimstr("foo")]'
1084
+ input: '["fo", "foo", "barfoo", "foobar", "foob"]'
1085
+ output: ['["fo","","bar","foobar","foob"]']
1086
+
1087
+ - title: "`explode`"
1088
+ body: |
1089
+
1090
+ Converts an input string into an array of the string's
1091
+ codepoint numbers.
1092
+
1093
+ examples:
1094
+ - program: 'explode'
1095
+ input: '"foobar"'
1096
+ output: ['[102,111,111,98,97,114]']
1097
+
1098
+ - title: "`implode`"
1099
+ body: |
1100
+
1101
+ The inverse of explode.
1102
+
1103
+ examples:
1104
+ - program: 'implode'
1105
+ input: '[65, 66, 67]'
1106
+ output: ['"ABC"']
1107
+
1108
+ - title: "`split`"
1109
+ body: |
1110
+
1111
+ Splits an input string on the separator argument.
1112
+
1113
+ examples:
1114
+ - program: 'split(", ")'
1115
+ input: '"a, b,c,d, e"'
1116
+ output: ['["a","b,c,d","e"]']
1117
+
1118
+ - title: "`join`"
1119
+ body: |
1120
+
1121
+ Joins the array of elements given as input, using the
1122
+ argument as separator. It is the inverse of `split`: that is,
1123
+ running `split("foo") | join("foo")` over any input string
1124
+ returns said input string.
1125
+
1126
+ examples:
1127
+ - program: 'join(", ")'
1128
+ input: '["a","b,c,d","e"]'
1129
+ output: ['"a, b,c,d, e"']
1130
+
1131
+
1132
+ - title: "`recurse`"
1133
+ body: |
1134
+
1135
+ The `recurse` function allows you to search through a
1136
+ recursive structure, and extract interesting data from all
1137
+ levels. Suppose your input represents a filesystem:
1138
+
1139
+ {"name": "/", "children": [
1140
+ {"name": "/bin", "children": [
1141
+ {"name": "/bin/ls", "children": []},
1142
+ {"name": "/bin/sh", "children": []}]},
1143
+ {"name": "/home", "children": [
1144
+ {"name": "/home/stephen", "children": [
1145
+ {"name": "/home/stephen/jq", "children": []}]}]}]}
1146
+
1147
+ Now suppose you want to extract all of the filenames
1148
+ present. You need to retrieve `.name`, `.children[].name`,
1149
+ `.children[].children[].name`, and so on. You can do this
1150
+ with:
1151
+
1152
+ recurse(.children[]) | .name
1153
+
1154
+ examples:
1155
+ - program: 'recurse(.foo[])'
1156
+ input: '{"foo":[{"foo": []}, {"foo":[{"foo":[]}]}]}'
1157
+ output:
1158
+ - '{"foo":[{"foo":[]},{"foo":[{"foo":[]}]}]}'
1159
+ - '{"foo":[]}'
1160
+ - '{"foo":[{"foo":[]}]}'
1161
+ - '{"foo":[]}'
1162
+
1163
+ - title: "`recurse_down`"
1164
+ body: |
1165
+
1166
+ A quieter version of `recurse(.[])`, equivalent to:
1167
+
1168
+ def recurse_down: recurse(.[]?);
1169
+
1170
+ - title: "`..`"
1171
+ body: |
1172
+
1173
+ Short-hand for `recurse_down`. This is intended to resemble
1174
+ the XPath `//` operator. Note that `..a` does not work; use
1175
+ `..|a` instead.
1176
+
1177
+ examples:
1178
+ - program: '..|.a?'
1179
+ input: '[[{"a":1}]]'
1180
+ output: ['1']
1181
+
1182
+ - title: "String interpolation - `\\(foo)`"
1183
+ body: |
1184
+
1185
+ Inside a string, you can put an expression inside parens
1186
+ after a backslash. Whatever the expression returns will be
1187
+ interpolated into the string.
1188
+
1189
+ examples:
1190
+ - program: '"The input was \(.), which is one less than \(.+1)"'
1191
+ input: '42'
1192
+ output: ['"The input was 42, which is one less than 43"']
1193
+
1194
+ - title: "Convert to/from JSON"
1195
+ body: |
1196
+
1197
+ The `tojson` and `fromjson` builtins dump values as JSON texts
1198
+ or parse JSON texts into values, respectively. The tojson
1199
+ builtin differs from tostring in that tostring returns strings
1200
+ unmodified, while tojson encodes strings as JSON strings.
1201
+
1202
+ examples:
1203
+ - program: '[.[]|tostring]'
1204
+ input: '[1, "foo", ["foo"]]'
1205
+ output: ['["1","foo","[\"foo\"]"]']
1206
+ - program: '[.[]|tojson]'
1207
+ input: '[1, "foo", ["foo"]]'
1208
+ output: ['["1","\"foo\"","[\"foo\"]"]']
1209
+ - program: '[.[]|tojson|fromjson]'
1210
+ input: '[1, "foo", ["foo"]]'
1211
+ output: ['[1,"foo",["foo"]]']
1212
+
1213
+ - title: "Format strings and escaping"
1214
+ body: |
1215
+
1216
+ The `@foo` syntax is used to format and escape strings,
1217
+ which is useful for building URLs, documents in a language
1218
+ like HTML or XML, and so forth. `@foo` can be used as a
1219
+ filter on its own, the possible escapings are:
1220
+
1221
+ * `@text`:
1222
+
1223
+ Calls `tostring`, see that function for details.
1224
+
1225
+ * `@json`:
1226
+
1227
+ Serialises the input as JSON.
1228
+
1229
+ * `@html`:
1230
+
1231
+ Applies HTML/XML escaping, by mapping the characters
1232
+ `<>&'"` to their entity equivalents `&lt;`, `&gt;`,
1233
+ `&amp;`, `&apos;`, `&quot;`.
1234
+
1235
+ * `@uri`:
1236
+
1237
+ Applies percent-encoding, by mapping all reserved URI
1238
+ characters to a `%xx` sequence.
1239
+
1240
+ * `@csv`:
1241
+
1242
+ The input must be an array, and it is rendered as CSV
1243
+ with double quotes for strings, and quotes escaped by
1244
+ repetition.
1245
+
1246
+ * `@sh`:
1247
+
1248
+ The input is escaped suitable for use in a command-line
1249
+ for a POSIX shell. If the input is an array, the output
1250
+ will be a series of space-separated strings.
1251
+
1252
+ * `@base64`:
1253
+
1254
+ The input is converted to base64 as specified by RFC 4648.
1255
+
1256
+ This syntax can be combined with string interpolation in a
1257
+ useful way. You can follow a `@foo` token with a string
1258
+ literal. The contents of the string literal will *not* be
1259
+ escaped. However, all interpolations made inside that string
1260
+ literal will be escaped. For instance,
1261
+
1262
+ @uri "https://www.google.com/search?q=\(.search)"
1263
+
1264
+ will produce the following output for the input
1265
+ `{"search":"what is jq?"}`:
1266
+
1267
+ "https://www.google.com/search?q=what%20is%20jq%3f"
1268
+
1269
+ Note that the slashes, question mark, etc. in the URL are
1270
+ not escaped, as they were part of the string literal.
1271
+
1272
+ examples:
1273
+ - program: '@html'
1274
+ input: '"This works if x < y"'
1275
+ output: ['"This works if x &lt; y"']
1276
+
1277
+ # - 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>"]
1280
+
1281
+ - program: '@sh "echo \(.)"'
1282
+ input: "\"O'Hara's Ale\""
1283
+ output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]
1284
+
1285
+ - title: Conditionals and Comparisons
1286
+ entries:
1287
+ - title: "`==`, `!=`"
1288
+ body: |
1289
+
1290
+ The expression 'a == b' will produce 'true' if the result of a and b
1291
+ are equal (that is, if they represent equivalent JSON documents) and
1292
+ 'false' otherwise. In particular, strings are never considered equal
1293
+ to numbers. If you're coming from Javascript, jq's == is like
1294
+ Javascript's === - considering values equal only when they have the
1295
+ same type as well as the same value.
1296
+
1297
+ != is "not equal", and 'a != b' returns the opposite value of 'a == b'
1298
+
1299
+ examples:
1300
+ - program: '.[] == 1'
1301
+ input: '[1, 1.0, "1", "banana"]'
1302
+ output: ['true', 'true', 'false', 'false']
1303
+ - title: if-then-else
1304
+ body: |
1305
+
1306
+ `if A then B else C end` will act the same as `B` if `A`
1307
+ produces a value other than false or null, but act the same
1308
+ as `C` otherwise.
1309
+
1310
+ Checking for false or null is a simpler notion of
1311
+ "truthiness" than is found in Javascript or Python, but it
1312
+ means that you'll sometimes have to be more explicit about
1313
+ the condition you want: you can't test whether, e.g. a
1314
+ string is empty using `if .name then A else B end`, you'll
1315
+ need something more like `if (.name | length) > 0 then A else
1316
+ B end` instead.
1317
+
1318
+ If the condition A produces multiple results, it is
1319
+ considered "true" if any of those results is not false or
1320
+ null. If it produces zero results, it's considered false.
1321
+
1322
+ More cases can be added to an if using `elif A then B` syntax.
1323
+
1324
+ examples:
1325
+ - program: |-
1326
+ if . == 0 then
1327
+ "zero"
1328
+ elif . == 1 then
1329
+ "one"
1330
+ else
1331
+ "many"
1332
+ end
1333
+ input: 2
1334
+ output: ['"many"']
1335
+
1336
+ - title: "`>, >=, <=, <`"
1337
+ body: |
1338
+
1339
+ The comparison operators `>`, `>=`, `<=`, `<` return whether
1340
+ their left argument is greater than, greater than or equal
1341
+ to, less than or equal to or less than their right argument
1342
+ (respectively).
1343
+
1344
+ The ordering is the same as that described for `sort`, above.
1345
+
1346
+ examples:
1347
+ - program: '. < 5'
1348
+ input: 2
1349
+ output: ['true']
1350
+
1351
+ - title: and/or/not
1352
+ body: |
1353
+
1354
+ jq supports the normal Boolean operators and/or/not. They have the
1355
+ same standard of truth as if expressions - false and null are
1356
+ considered "false values", and anything else is a "true value".
1357
+
1358
+ If an operand of one of these operators produces multiple
1359
+ results, the operator itself will produce a result for each input.
1360
+
1361
+ `not` is in fact a builtin function rather than an operator,
1362
+ so it is called as a filter to which things can be piped
1363
+ rather than with special syntax, as in `.foo and .bar |
1364
+ not`.
1365
+
1366
+ These three only produce the values "true" and "false", and
1367
+ so are only useful for genuine Boolean operations, rather
1368
+ than the common Perl/Python/Ruby idiom of
1369
+ "value_that_may_be_null or default". If you want to use this
1370
+ form of "or", picking between two values rather than
1371
+ evaluating a condition, see the "//" operator below.
1372
+
1373
+ examples:
1374
+ - program: '42 and "a string"'
1375
+ input: 'null'
1376
+ output: ['true']
1377
+ - program: '(true, false) or false'
1378
+ input: 'null'
1379
+ output: ['true', 'false']
1380
+ # - program: '(true, false) and (true, false)'
1381
+ # input: 'null'
1382
+ # output: ['true', 'false', 'false', 'false']
1383
+ - program: '(true, true) and (true, false)'
1384
+ input: 'null'
1385
+ output: ['true', 'false', 'true', 'false']
1386
+ - program: '[true, false | not]'
1387
+ input: 'null'
1388
+ output: ['[false, true]']
1389
+
1390
+ - title: Alternative operator - `//`
1391
+ body: |
1392
+
1393
+ A filter of the form `a // b` produces the same
1394
+ results as `a`, if `a` produces results other than `false`
1395
+ and `null`. Otherwise, `a // b` produces the same results as `b`.
1396
+
1397
+ This is useful for providing defaults: `.foo // 1` will
1398
+ evaluate to `1` if there's no `.foo` element in the
1399
+ input. It's similar to how `or` is sometimes used in Python
1400
+ (jq's `or` operator is reserved for strictly Boolean
1401
+ operations).
1402
+
1403
+ examples:
1404
+ - program: '.foo // 42'
1405
+ input: '{"foo": 19}'
1406
+ output: [19]
1407
+ - program: '.foo // 42'
1408
+ input: '{}'
1409
+ output: [42]
1410
+
1411
+ - title: Advanced features
1412
+ body: |
1413
+ Variables are an absolute necessity in most programming languages, but
1414
+ they're relegated to an "advanced feature" in jq.
1415
+
1416
+ In most languages, variables are the only means of passing around
1417
+ data. If you calculate a value, and you want to use it more than once,
1418
+ you'll need to store it in a variable. To pass a value to another part
1419
+ of the program, you'll need that part of the program to define a
1420
+ variable (as a function parameter, object member, or whatever) in
1421
+ which to place the data.
1422
+
1423
+ It is also possible to define functions in jq, although this is
1424
+ is a feature whose biggest use is defining jq's standard library
1425
+ (many jq functions such as `map` and `find` are in fact written
1426
+ in jq).
1427
+
1428
+ Finally, jq has a `reduce` operation, which is very powerful but a
1429
+ bit tricky. Again, it's mostly used internally, to define some
1430
+ useful bits of jq's standard library.
1431
+
1432
+ entries:
1433
+ - title: Variables
1434
+ body: |
1435
+
1436
+ In jq, all filters have an input and an output, so manual
1437
+ plumbing is not necessary to pass a value from one part of a program
1438
+ to the next. Many expressions, for instance `a + b`, pass their input
1439
+ to two distinct subexpressions (here `a` and `b` are both passed the
1440
+ same input), so variables aren't usually necessary in order to use a
1441
+ value twice.
1442
+
1443
+ For instance, calculating the average value of an array of numbers
1444
+ requires a few variables in most languages - at least one to hold the
1445
+ array, perhaps one for each element or for a loop counter. In jq, it's
1446
+ simply `add / length` - the `add` expression is given the array and
1447
+ produces its sum, and the `length` expression is given the array and
1448
+ produces its length.
1449
+
1450
+ So, there's generally a cleaner way to solve most problems in jq than
1451
+ defining variables. Still, sometimes they do make things easier, so jq
1452
+ lets you define variables using `expression as $variable`. All
1453
+ variable names start with `$`. Here's a slightly uglier version of the
1454
+ array-averaging example:
1455
+
1456
+ length as $array_length | add / $array_length
1457
+
1458
+ We'll need a more complicated problem to find a situation where using
1459
+ variables actually makes our lives easier.
1460
+
1461
+
1462
+ Suppose we have an array of blog posts, with "author" and "title"
1463
+ fields, and another object which is used to map author usernames to
1464
+ real names. Our input looks like:
1465
+
1466
+ {"posts": [{"title": "Frist psot", "author": "anon"},
1467
+ {"title": "A well-written article", "author": "person1"}],
1468
+ "realnames": {"anon": "Anonymous Coward",
1469
+ "person1": "Person McPherson"}}
1470
+
1471
+ We want to produce the posts with the author field containing a real
1472
+ name, as in:
1473
+
1474
+ {"title": "Frist psot", "author": "Anonymous Coward"}
1475
+ {"title": "A well-written article", "author": "Person McPherson"}
1476
+
1477
+ We use a variable, $names, to store the realnames object, so that we
1478
+ can refer to it later when looking up author usernames:
1479
+
1480
+ .realnames as $names | .posts[] | {title, author: $names[.author]}
1481
+
1482
+ The expression `exp as $x | ...` means: for each value of expression
1483
+ `exp`, run the rest of the pipeline with the entire original input, and
1484
+ with `$x` set to that value. Thus `as` functions as something of a
1485
+ foreach loop.
1486
+
1487
+ Variables are scoped over the rest of the expression that defines
1488
+ them, so
1489
+
1490
+ .realnames as $names | (.posts[] | {title, author: $names[.author]})
1491
+
1492
+ will work, but
1493
+
1494
+ (.realnames as $names | .posts[]) | {title, author: $names[.author]}
1495
+
1496
+ won't.
1497
+
1498
+ examples:
1499
+ - program: '.bar as $x | .foo | . + $x'
1500
+ input: '{"foo":10, "bar":200}'
1501
+ output: ['210']
1502
+
1503
+ - title: 'Defining Functions'
1504
+ body: |
1505
+
1506
+ You can give a filter a name using "def" syntax:
1507
+
1508
+ def increment: . + 1;
1509
+
1510
+ From then on, `increment` is usable as a filter just like a
1511
+ builtin function (in fact, this is how some of the builtins
1512
+ are defined). A function may take arguments:
1513
+
1514
+ def map(f): [.[] | f];
1515
+
1516
+ Arguments are passed as filters, not as values. The
1517
+ same argument may be referenced multiple times with
1518
+ different inputs (here `f` is run for each element of the
1519
+ input array). Arguments to a function work more like
1520
+ callbacks than like value arguments.
1521
+
1522
+ If you want the value-argument behaviour for defining simple
1523
+ functions, you can just use a variable:
1524
+
1525
+ def addvalue(f): f as $value | map(. + $value);
1526
+
1527
+ With that definition, `addvalue(.foo)` will add the current
1528
+ input's `.foo` field to each element of the array.
1529
+
1530
+ examples:
1531
+ - program: 'def addvalue(f): . + [f]; map(addvalue(.[0]))'
1532
+ input: '[[1,2],[10,20]]'
1533
+ output: ['[[1,2,1], [10,20,10]]']
1534
+ - program: 'def addvalue(f): f as $x | map(. + $x); addvalue(.[0])'
1535
+ input: '[[1,2],[10,20]]'
1536
+ output: ['[[1,2,1,2], [10,20,1,2]]']
1537
+
1538
+ - title: Reduce
1539
+ body: |
1540
+
1541
+ The `reduce` syntax in jq allows you to combine all of the
1542
+ results of an expression by accumulating them into a single
1543
+ answer. As an example, we'll pass `[3,2,1]` to this expression:
1544
+
1545
+ reduce .[] as $item (0; . + $item)
1546
+
1547
+ For each result that `.[]` produces, `. + $item` is run to
1548
+ accumulate a running total, starting from 0. In this
1549
+ example, `.[]` produces the results 3, 2, and 1, so the
1550
+ effect is similar to running something like this:
1551
+
1552
+ 0 | (3 as $item | . + $item) |
1553
+ (2 as $item | . + $item) |
1554
+ (1 as $item | . + $item)
1555
+
1556
+ examples:
1557
+ - program: 'reduce .[] as $item (0; . + $item)'
1558
+ input: '[10,2,5,3]'
1559
+ output: ['20']
1560
+
1561
+
1562
+ - title: Assignment
1563
+ body: |
1564
+
1565
+ Assignment works a little differently in jq than in most
1566
+ programming languages. jq doesn't distinguish between references
1567
+ to and copies of something - two objects or arrays are either
1568
+ equal or not equal, without any further notion of being "the
1569
+ same object" or "not the same object".
1570
+
1571
+ If an object has two fields which are arrays, `.foo` and `.bar`,
1572
+ and you append something to `.foo`, then `.bar` will not get
1573
+ bigger. Even if you've just set `.bar = .foo`. If you're used to
1574
+ programming in languages like Python, Java, Ruby, Javascript,
1575
+ etc. then you can think of it as though jq does a full deep copy
1576
+ of every object before it does the assignment (for performance,
1577
+ it doesn't actually do that, but that's the general idea).
1578
+
1579
+ entries:
1580
+ - title: "`=`"
1581
+ body: |
1582
+
1583
+ The filter `.foo = 1` will take as input an object
1584
+ and produce as output an object with the "foo" field set to
1585
+ 1. There is no notion of "modifying" or "changing" something
1586
+ in jq - all jq values are immutable. For instance,
1587
+
1588
+ .foo = .bar | .foo.baz = 1
1589
+
1590
+ will not have the side-effect of setting .bar.baz to be set
1591
+ to 1, as the similar-looking program in Javascript, Python,
1592
+ Ruby or other languages would. Unlike these languages (but
1593
+ like Haskell and some other functional languages), there is
1594
+ no notion of two arrays or objects being "the same array" or
1595
+ "the same object". They can be equal, or not equal, but if
1596
+ we change one of them in no circumstances will the other
1597
+ change behind our backs.
1598
+
1599
+ This means that it's impossible to build circular values in
1600
+ jq (such as an array whose first element is itself). This is
1601
+ quite intentional, and ensures that anything a jq program
1602
+ can produce can be represented in JSON.
1603
+
1604
+ - title: "`|=`"
1605
+ body: |
1606
+ As well as the assignment operator '=', jq provides the "update"
1607
+ operator '|=', which takes a filter on the right-hand side and
1608
+ works out the new value for the property being assigned to by running
1609
+ the old value through this expression. For instance, .foo |= .+1 will
1610
+ build an object with the "foo" field set to the input's "foo" plus 1.
1611
+
1612
+ This example should show the difference between '=' and '|=':
1613
+
1614
+ Provide input '{"a": {"b": 10}, "b": 20}' to the programs:
1615
+
1616
+ .a = .b
1617
+ .a |= .b
1618
+
1619
+ The former will set the "a" field of the input to the "b" field of the
1620
+ input, and produce the output {"a": 20}. The latter will set the "a"
1621
+ field of the input to the "a" field's "b" field, producing {"a": 10}.
1622
+
1623
+ - title: "`+=`, `-=`, `*=`, `/=`, `%=`, `//=`"
1624
+ body: |
1625
+
1626
+ jq has a few operators of the form `a op= b`, which are all
1627
+ equivalent to `a |= . op b`. So, `+= 1` can be used to increment values.
1628
+
1629
+ examples:
1630
+ - program: .foo += 1
1631
+ input: '{"foo": 42}'
1632
+ output: ['{"foo": 43}']
1633
+
1634
+ - title: Complex assignments
1635
+ body: |
1636
+ Lots more things are allowed on the left-hand side of a jq assignment
1637
+ than in most langauges. We've already seen simple field accesses on
1638
+ the left hand side, and it's no surprise that array accesses work just
1639
+ as well:
1640
+
1641
+ .posts[0].title = "JQ Manual"
1642
+
1643
+ What may come as a surprise is that the expression on the left may
1644
+ produce multiple results, referring to different points in the input
1645
+ document:
1646
+
1647
+ .posts[].comments |= . + ["this is great"]
1648
+
1649
+ That example appends the string "this is great" to the "comments"
1650
+ array of each post in the input (where the input is an object with a
1651
+ field "posts" which is an array of posts).
1652
+
1653
+ When jq encounters an assignment like 'a = b', it records the "path"
1654
+ taken to select a part of the input document while executing a. This
1655
+ path is then used to find which part of the input to change while
1656
+ executing the assignment. Any filter may be used on the
1657
+ left-hand side of an equals - whichever paths it selects from the
1658
+ input will be where the assignment is performed.
1659
+
1660
+ This is a very powerful operation. Suppose we wanted to add a comment
1661
+ to blog posts, using the same "blog" input above. This time, we only
1662
+ want to comment on the posts written by "stedolan". We can find those
1663
+ posts using the "select" function described earlier:
1664
+
1665
+ .posts[] | select(.author == "stedolan")
1666
+
1667
+ The paths provided by this operation point to each of the posts that
1668
+ "stedolan" wrote, and we can comment on each of them in the same way
1669
+ that we did before:
1670
+
1671
+ (.posts[] | select(.author == "stedolan") | .comments) |=
1672
+ . + ["terrible."]