@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,1270 @@
1
+ ---
2
+ headline: jq 1.3 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
+ You can affect how jq reads and writes its input and output
83
+ using some command-line options:
84
+
85
+ * `--slurp`/`-s`:
86
+
87
+ Instead of running the filter for each JSON object in the
88
+ input, read the entire input stream into a large array and run
89
+ the filter just once.
90
+
91
+ * `--raw-input`/`-R`:
92
+
93
+ Don't parse the input as JSON. Instead, each line of text is
94
+ passed to the filter as a string. If combined with `--slurp`,
95
+ then the entire input is passed to the filter as a single long
96
+ string.
97
+
98
+ * `--null-input`/`-n`:
99
+
100
+ Don't read any input at all! Instead, the filter is run once
101
+ using `null` as the input. This is useful when using jq as a
102
+ simple calculator or to construct JSON data from scratch.
103
+
104
+ * `--compact-output` / `-c`:
105
+
106
+ By default, jq pretty-prints JSON output. Using this option
107
+ will result in more compact output by instead putting each
108
+ JSON object on a single line.
109
+
110
+ * `--colour-output` / `-C` and `--monochrome-output` / `-M`:
111
+
112
+ By default, jq outputs colored JSON if writing to a
113
+ terminal. You can force it to produce color even if writing to
114
+ a pipe or a file using `-C`, and disable color with `-M`.
115
+
116
+ * `--ascii-output` / `-a`:
117
+
118
+ jq usually outputs non-ASCII Unicode codepoints as UTF-8, even
119
+ if the input specified them as escape sequences (like
120
+ "\u03bc"). Using this option, you can force jq to produce pure
121
+ ASCII output with every non-ASCII character replaced with the
122
+ equivalent escape sequence.
123
+
124
+ * `--raw-output` / `-r`:
125
+
126
+ With this option, if the filter's result is a string then it
127
+ will be written directly to standard output rather than being
128
+ formatted as a JSON string with quotes. This can be useful for
129
+ making jq filters talk to non-JSON-based systems.
130
+
131
+ * `--arg name value`:
132
+
133
+ This option passes a value to the jq program as a predefined
134
+ variable. If you run jq with `--arg foo bar`, then `$foo` is
135
+ available in the program and has the value `"bar"`.
136
+
137
+ - title: Basic filters
138
+ entries:
139
+ - title: "`.`"
140
+ body: |
141
+
142
+ The absolute simplest (and least interesting) filter
143
+ is `.`. This is a filter that takes its input and
144
+ produces it unchanged as output.
145
+
146
+ Since jq by default pretty-prints all output, this trivial
147
+ program can be a useful way of formatting JSON output from,
148
+ say, `curl`.
149
+
150
+ examples:
151
+ - program: '.'
152
+ input: '"Hello, world!"'
153
+ output: ['"Hello, world!"']
154
+
155
+ - title: "`.foo`"
156
+ body: |
157
+
158
+ The simplest *useful* filter is .foo. When given a
159
+ JSON object (aka dictionary or hash) as input, it produces
160
+ the value at the key "foo", or null if there's none present.
161
+
162
+ examples:
163
+ - program: '.foo'
164
+ input: '{"foo": 42, "bar": "less interesting data"}'
165
+ output: [42]
166
+ - program: '.foo'
167
+ input: '{"notfoo": true, "alsonotfoo": false}'
168
+ output: ['null']
169
+
170
+ - title: "`.[foo]`, `.[2]`, `.[10:15]`"
171
+ body: |
172
+
173
+ You can also look up fields of an object using syntax like
174
+ `.["foo"]` (.foo above is a shorthand version of this). This
175
+ one works for arrays as well, if the key is an
176
+ integer. Arrays are zero-based (like javascript), so `.[2]`
177
+ returns the third element of the array.
178
+
179
+ The `.[10:15]` syntax can be used to return a subarray of an
180
+ array. The array returned by `.[10:15]` will be of length 5,
181
+ containing the elements from index 10 (inclusive) to index
182
+ 15 (exclusive). Either index may be negative (in which case
183
+ it counts backwards from the end of the array), or omitted
184
+ (in which case it refers to the start or end of the array).
185
+
186
+ examples:
187
+ - program: '.[0]'
188
+ input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
189
+ output: ['{"name":"JSON", "good":true}']
190
+
191
+ - program: '.[2]'
192
+ input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
193
+ output: ['null']
194
+
195
+ - program: '.[2:4]'
196
+ input: '["a","b","c","d","e"]'
197
+ output: ['["c", "d"]']
198
+
199
+ - program: '.[:3]'
200
+ input: '["a","b","c","d","e"]'
201
+ output: ['["a", "b", "c"]']
202
+
203
+ - program: '.[-2:]'
204
+ input: '["a","b","c","d","e"]'
205
+ output: ['["d", "e"]']
206
+
207
+ - title: "`.[]`"
208
+ body: |
209
+
210
+ If you use the `.[foo]` syntax, but omit the index
211
+ entirely, it will return *all* of the elements of an
212
+ array. Running `.[]` with the input `[1,2,3]` will produce the
213
+ numbers as three separate results, rather than as a single
214
+ array.
215
+
216
+ You can also use this on an object, and it will return all
217
+ the values of the object.
218
+
219
+ examples:
220
+ - program: '.[]'
221
+ input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
222
+ output:
223
+ - '{"name":"JSON", "good":true}'
224
+ - '{"name":"XML", "good":false}'
225
+
226
+ - program: '.[]'
227
+ input: '[]'
228
+ output: []
229
+
230
+ - program: '.[]'
231
+ input: '{"a": 1, "b": 1}'
232
+ output: ['1', '1']
233
+
234
+ - title: "`,`"
235
+ body: |
236
+
237
+ If two filters are separated by a comma, then the
238
+ input will be fed into both and there will be multiple
239
+ outputs: first, all of the outputs produced by the left
240
+ expression, and then all of the outputs produced by the
241
+ right. For instance, filter `.foo, .bar`, produces
242
+ both the "foo" fields and "bar" fields as separate outputs.
243
+
244
+ examples:
245
+ - program: '.foo, .bar'
246
+ input: '{"foo": 42, "bar": "something else", "baz": true}'
247
+ output: ['42', '"something else"']
248
+
249
+ - program: ".user, .projects[]"
250
+ input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'
251
+ output: ['"stedolan"', '"jq"', '"wikiflow"']
252
+
253
+ - program: '.[4,2]'
254
+ input: '["a","b","c","d","e"]'
255
+ output: ['"e"', '"c"']
256
+
257
+ - title: "`|`"
258
+ body: |
259
+ The | operator combines two filters by feeding the output(s) of
260
+ the one on the left into the input of the one on the right. It's
261
+ pretty much the same as the Unix shell's pipe, if you're used to
262
+ that.
263
+
264
+ If the one on the left produces multiple results, the one on
265
+ the right will be run for each of those results. So, the
266
+ expression `.[] | .foo` retrieves the "foo" field of each
267
+ element of the input array.
268
+
269
+ examples:
270
+ - program: '.[] | .name'
271
+ input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
272
+ output: ['"JSON"', '"XML"']
273
+
274
+ - title: Types and Values
275
+ body: |
276
+
277
+ jq supports the same set of datatypes as JSON - numbers,
278
+ strings, booleans, arrays, objects (which in JSON-speak are
279
+ hashes with only string keys), and "null".
280
+
281
+ Booleans, null, strings and numbers are written the same way as
282
+ in javascript. Just like everything else in jq, these simple
283
+ values take an input and produce an output - `42` is a valid jq
284
+ expression that takes an input, ignores it, and returns 42
285
+ instead.
286
+
287
+ entries:
288
+ - title: Array construction - `[]`
289
+ body: |
290
+
291
+ As in JSON, `[]` is used to construct arrays, as in
292
+ `[1,2,3]`. The elements of the arrays can be any jq
293
+ expression. All of the results produced by all of the
294
+ expressions are collected into one big array. You can use it
295
+ to construct an array out of a known quantity of values (as
296
+ in `[.foo, .bar, .baz]`) or to "collect" all the results of a
297
+ filter into an array (as in `[.items[].name]`)
298
+
299
+ Once you understand the "," operator, you can look at jq's array
300
+ syntax in a different light: the expression `[1,2,3]` is not using a
301
+ built-in syntax for comma-separated arrays, but is instead applying
302
+ the `[]` operator (collect results) to the expression 1,2,3 (which
303
+ produces three different results).
304
+
305
+ If you have a filter `X` that produces four results,
306
+ then the expression `[X]` will produce a single result, an
307
+ array of four elements.
308
+
309
+ examples:
310
+ - program: "[.user, .projects[]]"
311
+ input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'
312
+ output: ['["stedolan", "jq", "wikiflow"]']
313
+ - title: Objects - `{}`
314
+ body: |
315
+
316
+ Like JSON, `{}` is for constructing objects (aka
317
+ dictionaries or hashes), as in: `{"a": 42, "b": 17}`.
318
+
319
+ If the keys are "sensible" (all alphabetic characters), then
320
+ the quotes can be left off. The value can be any expression
321
+ (although you may need to wrap it in parentheses if it's a
322
+ complicated one), which gets applied to the {} expression's
323
+ input (remember, all filters have an input and an
324
+ output).
325
+
326
+ {foo: .bar}
327
+
328
+ will produce the JSON object `{"foo": 42}` if given the JSON
329
+ object `{"bar":42, "baz":43}`. You can use this to select
330
+ particular fields of an object: if the input is an object
331
+ with "user", "title", "id", and "content" fields and you
332
+ just want "user" and "title", you can write
333
+
334
+ {user: .user, title: .title}
335
+
336
+ Because that's so common, there's a shortcut syntax: `{user, title}`.
337
+
338
+ If one of the expressions produces multiple results,
339
+ multiple dictionaries will be produced. If the input's
340
+
341
+ {"user":"stedolan","titles":["JQ Primer", "More JQ"]}
342
+
343
+ then the expression
344
+
345
+ {user, title: .titles[]}
346
+
347
+ will produce two outputs:
348
+
349
+ {"user":"stedolan", "title": "JQ Primer"}
350
+ {"user":"stedolan", "title": "More JQ"}
351
+
352
+ Putting parentheses around the key means it will be evaluated as an
353
+ expression. With the same input as above,
354
+
355
+ {(.user): .titles}
356
+
357
+ produces
358
+
359
+ {"stedolan": ["JQ Primer", "More JQ"]}
360
+
361
+ examples:
362
+ - program: '{user, title: .titles[]}'
363
+ input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
364
+ output:
365
+ - '{"user":"stedolan", "title": "JQ Primer"}'
366
+ - '{"user":"stedolan", "title": "More JQ"}'
367
+ - program: '{(.user): .titles}'
368
+ input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
369
+ output: ['{"stedolan": ["JQ Primer", "More JQ"]}']
370
+
371
+ - title: Builtin operators and functions
372
+ body: |
373
+
374
+ Some jq operator (for instance, `+`) do different things
375
+ depending on the type of their arguments (arrays, numbers,
376
+ etc.). However, jq never does implicit type conversions. If you
377
+ try to add a string to an object you'll get an error message and
378
+ no result.
379
+
380
+ entries:
381
+ - title: Addition - `+`
382
+ body: |
383
+
384
+ The operator `+` takes two filters, applies them both
385
+ to the same input, and adds the results together. What
386
+ "adding" means depends on the types involved:
387
+
388
+ - **Numbers** are added by normal arithmetic.
389
+
390
+ - **Arrays** are added by being concatenated into a larger array.
391
+
392
+ - **Strings** are added by being joined into a larger string.
393
+
394
+ - **Objects** are added by merging, that is, inserting all
395
+ the key-value pairs from both objects into a single
396
+ combined object. If both objects contain a value for the
397
+ same key, the object on the right of the `+` wins.
398
+
399
+ `null` can be added to any value, and returns the other
400
+ value unchanged.
401
+
402
+ examples:
403
+ - program: '.a + 1'
404
+ input: '{"a": 7}'
405
+ output: ['8']
406
+ - program: '.a + .b'
407
+ input: '{"a": [1,2], "b": [3,4]}'
408
+ output: ['[1,2,3,4]']
409
+ - program: '.a + null'
410
+ input: '{"a": 1}'
411
+ output: ['1']
412
+ - program: '.a + 1'
413
+ input: '{}'
414
+ output: ['1']
415
+ - program: '{a: 1} + {b: 2} + {c: 3} + {a: 42}'
416
+ input: 'null'
417
+ output: ['{"a": 42, "b": 2, "c": 3}']
418
+
419
+ - title: Subtraction - `-`
420
+ body: |
421
+
422
+ As well as normal arithmetic subtraction on numbers, the `-`
423
+ operator can be used on arrays to remove all occurences of
424
+ the second array's elements from the first array.
425
+
426
+ examples:
427
+ - program: '4 - .a'
428
+ input: '{"a":3}'
429
+ output: ['1']
430
+ - program: . - ["xml", "yaml"]
431
+ input: '["xml", "yaml", "json"]'
432
+ output: ['["json"]']
433
+
434
+ - title: Multiplication, division - `*` and `/`
435
+ body: |
436
+
437
+ These operators only work on numbers, and do the expected.
438
+
439
+ examples:
440
+ - program: '10 / . * 3'
441
+ input: 5
442
+ output: [6]
443
+
444
+ - title: `length`
445
+ body: |
446
+
447
+ The builtin function `length` gets the length of various
448
+ different types of value:
449
+
450
+ - The length of a **string** is the number of Unicode
451
+ codepoints it contains (which will be the same as its
452
+ JSON-encoded length in bytes if it's pure ASCII).
453
+
454
+ - The length of an **array** is the number of elements.
455
+
456
+ - The length of an **object** is the number of key-value pairs.
457
+
458
+ - The length of **null** is zero.
459
+
460
+ examples:
461
+ - program: '.[] | length'
462
+ input: '[[1,2], "string", {"a":2}, null]'
463
+ output: [2, 6, 1, 0]
464
+
465
+ - title: `keys`
466
+ body: |
467
+
468
+ The builtin function `keys`, when given an object, returns
469
+ its keys in an array.
470
+
471
+ The keys are sorted "alphabetically", by unicode codepoint
472
+ order. This is not an order that makes particular sense in
473
+ any particular language, but you can count on it being the
474
+ same for any two objects with the same set of keys,
475
+ regardless of locale settings.
476
+
477
+ When `keys` is given an array, it returns the valid indices
478
+ for that array: the integers from 0 to length-1.
479
+
480
+ examples:
481
+ - program: 'keys'
482
+ input: '{"abc": 1, "abcd": 2, "Foo": 3}'
483
+ output: ['["Foo", "abc", "abcd"]']
484
+ - program: 'keys'
485
+ input: '[42,3,35]'
486
+ output: ['[0,1,2]']
487
+
488
+ - title: `has`
489
+ body: |
490
+
491
+ The builtin function `has` returns whether the input object
492
+ has the given key, or the input array has an element at the
493
+ given index.
494
+
495
+ `has($key)` has the same effect as checking whether `$key`
496
+ is a member of the array returned by `keys`, although `has`
497
+ will be faster.
498
+
499
+ examples:
500
+ - program: 'map(has("foo"))'
501
+ input: '[{"foo": 42}, {}]'
502
+ output: ['[true, false]']
503
+ - program: 'map(has(2))'
504
+ input: '[[0,1], ["a","b","c"]]'
505
+ output: ['[false, true]']
506
+
507
+ - title: `to_entries`, `from_entries`, `with_entries`
508
+ body: |
509
+
510
+ These functions convert between an object and an array of
511
+ key-value pairs. If `to_entries` is passed an object, then
512
+ for each `k: v` entry in the input, the output array
513
+ includes `{"key": k, "value": v}`.
514
+
515
+ `from_entries` does the opposite conversion, and
516
+ `with_entries(foo)` is a shorthand for `to_entries |
517
+ map(foo) | from_entries`, useful for doing some operation to
518
+ all keys and values of an object.
519
+
520
+ examples:
521
+ - program: 'to_entries'
522
+ input: '{"a": 1, "b": 2}'
523
+ output: ['[{"key":"a", "value":1}, {"key":"b", "value":2}]']
524
+ - program: 'from_entries'
525
+ input: '[{"key":"a", "value":1}, {"key":"b", "value":2}]'
526
+ output: ['{"a": 1, "b": 2}']
527
+ - program: 'with_entries(.key |= "KEY_" + .)'
528
+ input: '{"a": 1, "b": 2}'
529
+ output: ['{"KEY_a": 1, "KEY_b": 2}']
530
+
531
+
532
+ - title: `select`
533
+ body: |
534
+
535
+ The function `select(foo)` produces its input unchanged if
536
+ `foo` returns true for that input, and produces no output
537
+ otherwise.
538
+
539
+ It's useful for filtering lists: `[1,2,3] | map(select(. >= 2))`
540
+ will give you `[3]`.
541
+
542
+ examples:
543
+ - program: 'map(select(. >= 2))'
544
+ input: '[1,5,3,0,7]'
545
+ output: ['[5,3,7]']
546
+
547
+ - title: `empty`
548
+ body: |
549
+
550
+ `empty` returns no results. None at all. Not even `null`.
551
+
552
+ It's useful on occasion. You'll know if you need it :)
553
+
554
+ examples:
555
+ - program: '1, empty, 2'
556
+ input: 'null'
557
+ output: [1, 2]
558
+ - program: '[1,2,empty,3]'
559
+ input: 'null'
560
+ output: ['[1,2,3]']
561
+
562
+ - title: `map(x)`
563
+ body: |
564
+
565
+ For any filter `x`, `map(x)` will run that filter for each
566
+ element of the input array, and produce the outputs a new
567
+ array. `map(.+1)` will increment each element of an array of numbers.
568
+
569
+ `map(x)` is equivalent to `[.[] | x]`. In fact, this is how
570
+ it's defined.
571
+
572
+ examples:
573
+ - program: 'map(.+1)'
574
+ input: '[1,2,3]'
575
+ output: ['[2,3,4]']
576
+
577
+ - title: `add`
578
+ body: |
579
+
580
+ The filter `add` takes as input an array, and produces as
581
+ output the elements of the array added together. This might
582
+ mean summed, concatenated or merged depending on the types
583
+ of the elements of the input array - the rules are the same
584
+ as those for the `+` operator (described above).
585
+
586
+ If the input is an empty array, `add` returns `null`.
587
+
588
+ examples:
589
+ - program: add
590
+ input: '["a","b","c"]'
591
+ output: ['"abc"']
592
+ - program: add
593
+ input: '[1, 2, 3]'
594
+ output: [6]
595
+ - program: add
596
+ input: '[]'
597
+ output: ["null"]
598
+
599
+ - title: `range`
600
+ body: |
601
+
602
+ The `range` function produces a range of numbers. `range(4;10)`
603
+ produces 6 numbers, from 4 (inclusive) to 10 (exclusive). The numbers
604
+ are produced as separate outputs. Use `[range(4;10)]` to get a range as
605
+ an array.
606
+
607
+ examples:
608
+ - program: 'range(2;4)'
609
+ input: 'null'
610
+ output: ['2', '3']
611
+ - program: '[range(2;4)]'
612
+ input: 'null'
613
+ output: ['[2,3]']
614
+
615
+ - title: `tonumber`
616
+ body: |
617
+
618
+ The `tonumber` function parses its input as a number. It
619
+ will convert correctly-formatted strings to their numeric
620
+ equivalent, leave numbers alone, and give an error on all other input.
621
+
622
+ examples:
623
+ - program: '.[] | tonumber'
624
+ input: '[1, "1"]'
625
+ output: [1, 1]
626
+
627
+ - title: `tostring`
628
+ body: |
629
+
630
+ The `tostring` function prints its input as a
631
+ string. Strings are left unchanged, and all other values are
632
+ JSON-encoded.
633
+
634
+ examples:
635
+ - program: '.[] | tostring'
636
+ input: '[1, "1", [1]]'
637
+ output: ['"1"', '"1"', '"[1]"']
638
+
639
+ - title: `type`
640
+ body: |
641
+
642
+ The `type` function returns the type of its argument as a
643
+ string, which is one of null, boolean, number, string, array
644
+ or object.
645
+
646
+ examples:
647
+ - program: 'map(type)'
648
+ input: '[0, false, [], {}, null, "hello"]'
649
+ output: ['["number", "boolean", "array", "object", "null", "string"]']
650
+
651
+ - title: `sort, sort_by`
652
+ body: |
653
+
654
+ The `sort` functions sorts its input, which must be an
655
+ array. Values are sorted in the following order:
656
+
657
+ * `null`
658
+ * `false`
659
+ * `true`
660
+ * numbers
661
+ * strings, in alphabetical order (by unicode codepoint value)
662
+ * arrays, in lexical order
663
+ * objects
664
+
665
+ The ordering for objects is a little complex: first they're
666
+ compared by comparing their sets of keys (as arrays in
667
+ sorted order), and if their keys are equal then the values
668
+ are compared key by key.
669
+
670
+ `sort_by` may be used to sort by a particular field of an
671
+ object, or by applying any jq filter. `sort_by(foo)`
672
+ compares two elements by comparing the result of `foo` on
673
+ each element.
674
+
675
+ examples:
676
+ - program: 'sort'
677
+ input: '[8,3,null,6]'
678
+ output: ['[null,3,6,8]']
679
+ - program: 'sort_by(.foo)'
680
+ input: '[{"foo":4, "bar":10}, {"foo":3, "bar":100}, {"foo":2, "bar":1}]'
681
+ output: ['[{"foo":2, "bar":1}, {"foo":3, "bar":100}, {"foo":4, "bar":10}]']
682
+
683
+ - title: `group_by`
684
+ body: |
685
+
686
+ `group_by(.foo)` takes as input an array, groups the
687
+ elements having the same `.foo` field into separate arrays,
688
+ and produces all of these arrays as elements of a larger
689
+ array, sorted by the value of the `.foo` field.
690
+
691
+ Any jq expression, not just a field access, may be used in
692
+ place of `.foo`. The sorting order is the same as described
693
+ in the `sort` function above.
694
+
695
+ examples:
696
+ - program: 'group_by(.foo)'
697
+ input: '[{"foo":1, "bar":10}, {"foo":3, "bar":100}, {"foo":1, "bar":1}]'
698
+ output: ['[[{"foo":1, "bar":10}, {"foo":1, "bar":1}], [{"foo":3, "bar":100}]]']
699
+
700
+ - title: `min`, `max`, `min_by`, `max_by`
701
+ body: |
702
+
703
+ Find the minimum or maximum element of the input array. The
704
+ `_by` versions allow you to specify a particular field or
705
+ property to examine, e.g. `min_by(.foo)` finds the object
706
+ with the smallest `foo` field.
707
+
708
+ examples:
709
+ - program: 'min'
710
+ input: '[5,4,2,7]'
711
+ output: ['2']
712
+ - program: 'max_by(.foo)'
713
+ input: '[{"foo":1, "bar":14}, {"foo":2, "bar":3}]'
714
+ output: ['{"foo":2, "bar":3}']
715
+
716
+ - title: `unique`
717
+ body: |
718
+
719
+ The `unique` function takes as input an array and produces
720
+ an array of the same elements, in sorted order, with
721
+ duplicates removed.
722
+
723
+ examples:
724
+ - program: 'unique'
725
+ input: '[1,2,5,3,5,3,1,3]'
726
+ output: ['[1,2,3,5]']
727
+
728
+ - title: `reverse`
729
+ body: |
730
+
731
+ This function reverses an array.
732
+
733
+ examples:
734
+ - program: 'reverse'
735
+ input: '[1,2,3,4]'
736
+ output: ['[4,3,2,1]']
737
+
738
+ - title: `contains`
739
+ body: |
740
+
741
+ The filter `contains(b)` will produce true if b is
742
+ completely contained within the input. A string B is
743
+ contained in a string A if B is a substring of A. An array B
744
+ is contained in an array A is all elements in B are
745
+ contained in any element in A. An object B is contained in
746
+ object A if all of the values in B are contained in the
747
+ value in A with the same key. All other types are assumed to
748
+ be contained in each other if they are equal.
749
+
750
+ examples:
751
+ - program: 'contains("bar")'
752
+ input: '"foobar"'
753
+ output: ['true']
754
+ - program: 'contains(["baz", "bar"])'
755
+ input: '["foobar", "foobaz", "blarp"]'
756
+ output: ['true']
757
+ - program: 'contains(["bazzzzz", "bar"])'
758
+ input: '["foobar", "foobaz", "blarp"]'
759
+ output: ['false']
760
+ - program: 'contains({foo: 12, bar: [{barp: 12}]})'
761
+ input: '{"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]}'
762
+ output: ['true']
763
+ - program: 'contains({foo: 12, bar: [{barp: 15}]})'
764
+ input: '{"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]}'
765
+ output: ['false']
766
+
767
+ - title: `recurse`
768
+ body: |
769
+
770
+ The `recurse` function allows you to search through a
771
+ recursive structure, and extract interesting data from all
772
+ levels. Suppose your input represents a filesystem:
773
+
774
+ {"name": "/", "children": [
775
+ {"name": "/bin", "children": [
776
+ {"name": "/bin/ls", "children": []},
777
+ {"name": "/bin/sh", "children": []}]},
778
+ {"name": "/home", "children": [
779
+ {"name": "/home/stephen", "children": [
780
+ {"name": "/home/stephen/jq", "children": []}]}]}]}
781
+
782
+ Now suppose you want to extract all of the filenames
783
+ present. You need to retrieve `.name`, `.children[].name`,
784
+ `.children[].children[].name`, and so on. You can do this
785
+ with:
786
+
787
+ recurse(.children[]) | .name
788
+
789
+ examples:
790
+ - program: 'recurse(.foo[])'
791
+ input: '{"foo":[{"foo": []}, {"foo":[{"foo":[]}]}]}'
792
+ output:
793
+ - '{"foo":[{"foo":[]},{"foo":[{"foo":[]}]}]}'
794
+ - '{"foo":[]}'
795
+ - '{"foo":[{"foo":[]}]}'
796
+ - '{"foo":[]}'
797
+
798
+
799
+ - title: "String interpolation - `\(foo)`"
800
+ body: |
801
+
802
+ Inside a string, you can put an expression inside parens
803
+ after a backslash. Whatever the expression returns will be
804
+ interpolated into the string.
805
+
806
+ examples:
807
+ - program: '"The input was \(.), which is one less than \(.+1)"'
808
+ input: '42'
809
+ output: ['"The input was 42, which is one less than 43"']
810
+
811
+ - title: "Format strings and escaping"
812
+ body: |
813
+
814
+ The `@foo` syntax is used to format and escape strings,
815
+ which is useful for building URLs, documents in a language
816
+ like HTML or XML, and so forth. `@foo` can be used as a
817
+ filter on its own, the possible escapings are:
818
+
819
+ * `@text`:
820
+
821
+ Calls `tostring`, see that function for details.
822
+
823
+ * `@json`:
824
+
825
+ Serialises the input as JSON.
826
+
827
+ * `@html`:
828
+
829
+ Applies HTML/XML escaping, by mapping the characters
830
+ `<>&'"` to their entity equivalents `&lt;`, `&gt;`,
831
+ `&amp;`, `&apos;`, `&quot;`.
832
+
833
+ * `@uri`:
834
+
835
+ Applies percent-encoding, by mapping all reserved URI
836
+ characters to a `%xx` sequence.
837
+
838
+ * `@csv`:
839
+
840
+ The input must be an array, and it is rendered as CSV
841
+ with double quotes for strings, and quotes escaped by
842
+ repetition.
843
+
844
+ * `@sh`:
845
+
846
+ The input is escaped suitable for use in a command-line
847
+ for a POSIX shell. If the input is an array, the output
848
+ will be a series of space-separated strings.
849
+
850
+ * `@base64`:
851
+
852
+ The input is converted to base64 as specified by RFC 4648.
853
+
854
+ This syntax can be combined with string interpolation in a
855
+ useful way. You can follow a `@foo` token with a string
856
+ literal. The contents of the string literal will *not* be
857
+ escaped. However, all interpolations made inside that string
858
+ literal will be escaped. For instance,
859
+
860
+ @uri "https://www.google.com/search?q=\(.search)"
861
+
862
+ will produce the following output for the input
863
+ `{"search":"jq!"}`:
864
+
865
+ https://www.google.com/search?q=jq%21
866
+
867
+ Note that the slashes, question mark, etc. in the URL are
868
+ not escaped, as they were part of the string literal.
869
+
870
+ examples:
871
+ - program: '@html'
872
+ input: '"This works if x < y"'
873
+ output: ['"This works if x &lt; y"']
874
+
875
+ # - program: '@html "<span>Anonymous said: \(.)</span>"'
876
+ # input: '"<script>alert(\"lol hax\");</script>"'
877
+ # output: ["<span>Anonymous said: &lt;script&gt;alert(&quot;lol hax&quot;);&lt;/script&gt;</span>"]
878
+
879
+ - program: '@sh "echo \(.)"'
880
+ input: "\"O'Hara's Ale\""
881
+ output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]
882
+
883
+ - title: Conditionals and Comparisons
884
+ entries:
885
+ - title: `==`, `!=`
886
+ body: |
887
+
888
+ The expression 'a == b' will produce 'true' if the result of a and b
889
+ are equal (that is, if they represent equivalent JSON documents) and
890
+ 'false' otherwise. In particular, strings are never considered equal
891
+ to numbers. If you're coming from Javascript, jq's == is like
892
+ Javascript's === - considering values equal only when they have the
893
+ same type as well as the same value.
894
+
895
+ != is "not equal", and 'a != b' returns the opposite value of 'a == b'
896
+
897
+ examples:
898
+ - program: '.[] == 1'
899
+ input: '[1, 1.0, "1", "banana"]'
900
+ output: ['true', 'true', 'false', 'false']
901
+ - title: if-then-else
902
+ body: |
903
+
904
+ `if A then B else C end` will act the same as `B` if `A`
905
+ produces a value other than false or null, but act the same
906
+ as `C` otherwise.
907
+
908
+ Checking for false or null is a simpler notion of
909
+ "truthiness" than is found in Javascript or Python, but it
910
+ means that you'll sometimes have to be more explicit about
911
+ the condition you want: you can't test whether, e.g. a
912
+ string is empty using `if .name then A else B end`, you'll
913
+ need something more like `if (.name | count) > 0 then A else
914
+ B end` instead.
915
+
916
+ If the condition A produces multiple results, it is
917
+ considered "true" if any of those results is not false or
918
+ null. If it produces zero results, it's considered false.
919
+
920
+ More cases can be added to an if using `elif A then B` syntax.
921
+
922
+ examples:
923
+ - program: |-
924
+ if . == 0 then
925
+ "zero"
926
+ elif . == 1 then
927
+ "one"
928
+ else
929
+ "many"
930
+ end
931
+ input: 2
932
+ output: ['"many"']
933
+
934
+ - title: `>, >=, <=, <`
935
+ body: |
936
+
937
+ The comparison operators `>`, `>=`, `<=`, `<` return whether
938
+ their left argument is greater than, greater than or equal
939
+ to, less than or equal to or less than their right argument
940
+ (respectively).
941
+
942
+ The ordering is the same as that described for `sort`, above.
943
+
944
+ examples:
945
+ - program: '. < 5'
946
+ input: 2
947
+ output: ['true']
948
+
949
+ - title: and/or/not
950
+ body: |
951
+
952
+ jq supports the normal Boolean operators and/or/not. They have the
953
+ same standard of truth as if expressions - false and null are
954
+ considered "false values", and anything else is a "true value".
955
+
956
+ If an operand of one of these operators produces multiple
957
+ results, the operator itself will produce a result for each input.
958
+
959
+ `not` is in fact a builtin function rather than an operator,
960
+ so it is called as a filter to which things can be piped
961
+ rather than with special syntax, as in `.foo and .bar |
962
+ not`.
963
+
964
+ These three only produce the values "true" and "false", and
965
+ so are only useful for genuine Boolean operations, rather
966
+ than the common Perl/Python/Ruby idiom of
967
+ "value_that_may_be_null or default". If you want to use this
968
+ form of "or", picking between two values rather than
969
+ evaluating a condition, see the "//" operator below.
970
+
971
+ examples:
972
+ - program: '42 and "a string"'
973
+ input: 'null'
974
+ output: ['true']
975
+ - program: '(true, false) or false'
976
+ input: 'null'
977
+ output: ['true', 'false']
978
+ # - program: '(true, false) and (true, false)'
979
+ # input: 'null'
980
+ # output: ['true', 'false', 'false', 'false']
981
+ - program: '(true, true) and (true, false)'
982
+ input: 'null'
983
+ output: ['true', 'false', 'true', 'false']
984
+ - program: '[true, false | not]'
985
+ input: 'null'
986
+ output: ['[false, true]']
987
+
988
+ - title: Alternative operator - `//`
989
+ body: |
990
+
991
+ A filter of the form `a // b` produces the same
992
+ results as `a`, if `a` produces results other than `false`
993
+ and `null`. Otherwise, `a // b` produces the same results as `b`.
994
+
995
+ This is useful for providing defaults: `.foo // 1` will
996
+ evaluate to `1` if there's no `.foo` element in the
997
+ input. It's similar to how `or` is sometimes used in Python
998
+ (jq's `or` operator is reserved for strictly Boolean
999
+ operations).
1000
+
1001
+ examples:
1002
+ - program: '.foo // 42'
1003
+ input: '{"foo": 19}'
1004
+ output: [19]
1005
+ - program: '.foo // 42'
1006
+ input: '{}'
1007
+ output: [42]
1008
+
1009
+ - title: Advanced features
1010
+ body: |
1011
+ Variables are an absolute necessity in most programming languages, but
1012
+ they're relegated to an "advanced feature" in jq.
1013
+
1014
+ In most languages, variables are the only means of passing around
1015
+ data. If you calculate a value, and you want to use it more than once,
1016
+ you'll need to store it in a variable. To pass a value to another part
1017
+ of the program, you'll need that part of the program to define a
1018
+ variable (as a function parameter, object member, or whatever) in
1019
+ which to place the data.
1020
+
1021
+ It is also possible to define functions in jq, although this is
1022
+ is a feature whose biggest use is defining jq's standard library
1023
+ (many jq functions such as `map` and `find` are in fact written
1024
+ in jq).
1025
+
1026
+ Finally, jq has a `reduce` operation, which is very powerful but a
1027
+ bit tricky. Again, it's mostly used internally, to define some
1028
+ useful bits of jq's standard library.
1029
+
1030
+ entries:
1031
+ - title: Variables
1032
+ body: |
1033
+
1034
+ In jq, all filters have an input and an output, so manual
1035
+ plumbing is not necessary to pass a value from one part of a program
1036
+ to the next. Many expressions, for instance `a + b`, pass their input
1037
+ to two distinct subexpressions (here `a` and `b` are both passed the
1038
+ same input), so variables aren't usually necessary in order to use a
1039
+ value twice.
1040
+
1041
+ For instance, calculating the average value of an array of numbers
1042
+ requires a few variables in most languages - at least one to hold the
1043
+ array, perhaps one for each element or for a loop counter. In jq, it's
1044
+ simply `add / length` - the `add` expression is given the array and
1045
+ produces its sum, and the `length` expression is given the array and
1046
+ produces its length.
1047
+
1048
+ So, there's generally a cleaner way to solve most problems in jq that
1049
+ defining variables. Still, sometimes they do make things easier, so jq
1050
+ lets you define variables using `expression as $variable`. All
1051
+ variable names start with `$`. Here's a slightly uglier version of the
1052
+ array-averaging example:
1053
+
1054
+ length as $array_length | add / $array_length
1055
+
1056
+ We'll need a more complicated problem to find a situation where using
1057
+ variables actually makes our lives easier.
1058
+
1059
+
1060
+ Suppose we have an array of blog posts, with "author" and "title"
1061
+ fields, and another object which is used to map author usernames to
1062
+ real names. Our input looks like:
1063
+
1064
+ {"posts": [{"title": "Frist psot", "author": "anon"},
1065
+ {"title": "A well-written article", "author": "person1"}],
1066
+ "realnames": {"anon": "Anonymous Coward",
1067
+ "person1": "Person McPherson"}}
1068
+
1069
+ We want to produce the posts with the author field containing a real
1070
+ name, as in:
1071
+
1072
+ {"title": "Frist psot", "author": "Anonymous Coward"}
1073
+ {"title": "A well-written article", "author": "Person McPherson"}
1074
+
1075
+ We use a variable, $names, to store the realnames object, so that we
1076
+ can refer to it later when looking up author usernames:
1077
+
1078
+ .realnames as $names | .posts[] | {title, author: $names[.author]}
1079
+
1080
+ The expression `exp as $x | ...` means: for each value of expression
1081
+ `exp`, run the rest of the pipeline with the entire original input, and
1082
+ with `$x` set to that value. Thus `as` functions as something of a
1083
+ foreach loop.
1084
+
1085
+ Variables are scoped over the rest of the expression that defines
1086
+ them, so
1087
+
1088
+ .realnames as $names | (.posts[] | {title, author: $names[.author]})
1089
+
1090
+ will work, but
1091
+
1092
+ (.realnames as $names | .posts[]) | {title, author: $names[.author]}
1093
+
1094
+ won't.
1095
+
1096
+ examples:
1097
+ - program: '.bar as $x | .foo | . + $x'
1098
+ input: '{"foo":10, "bar":200}'
1099
+ output: ['210']
1100
+
1101
+ - title: 'Defining Functions'
1102
+ body: |
1103
+
1104
+ You can give a filter a name using "def" syntax:
1105
+
1106
+ def increment: . + 1;
1107
+
1108
+ From then on, `increment` is usable as a filter just like a
1109
+ builtin function (in fact, this is how some of the builtins
1110
+ are defined). A function may take arguments:
1111
+
1112
+ def map(f): [.[] | f];
1113
+
1114
+ Arguments are passed as filters, not as values. The
1115
+ same argument may be referenced multiple times with
1116
+ different inputs (here `f` is run for each element of the
1117
+ input array). Arguments to a function work more like
1118
+ callbacks than like value arguments.
1119
+
1120
+ If you want the value-argument behaviour for defining simple
1121
+ functions, you can just use a variable:
1122
+
1123
+ def addvalue(f): f as $value | map(. + $value);
1124
+
1125
+ With that definition, `addvalue(.foo)` will add the current
1126
+ input's `.foo` field to each element of the array.
1127
+
1128
+ examples:
1129
+ - program: 'def addvalue(f): . + [f]; map(addvalue(.[0]))'
1130
+ input: '[[1,2],[10,20]]'
1131
+ output: ['[[1,2,1], [10,20,10]]']
1132
+ - program: 'def addvalue(f): f as $x | map(. + $x); addvalue(.[0])'
1133
+ input: '[[1,2],[10,20]]'
1134
+ output: ['[[1,2,1,2], [10,20,1,2]]']
1135
+
1136
+ - title: Reduce
1137
+ body: |
1138
+
1139
+ The `reduce` syntax in jq allows you to combine all of the
1140
+ results of an expression by accumulating them into a single
1141
+ answer. As an example, we'll pass `[3,2,1]` to this expression:
1142
+
1143
+ reduce .[] as $item (0; . + $item)
1144
+
1145
+ For each result that `.[]` produces, `. + $item` is run to
1146
+ accumulate a running total, starting from 0. In this
1147
+ example, `.[]` produces the results 3, 2, and 1, so the
1148
+ effect is similar to running something like this:
1149
+
1150
+ 0 | (3 as $item | . + $item) |
1151
+ (2 as $item | . + $item) |
1152
+ (1 as $item | . + $item)
1153
+
1154
+ examples:
1155
+ - program: 'reduce .[] as $item (0; . + $item)'
1156
+ input: '[10,2,5,3]'
1157
+ output: ['20']
1158
+
1159
+
1160
+ - title: Assignment
1161
+ body: |
1162
+
1163
+ Assignment works a little differently in jq than in most
1164
+ programming languages. jq doesn't distinguish between references
1165
+ to and copies of something - two objects or arrays are either
1166
+ equal or not equal, without any further notion of being "the
1167
+ same object" or "not the same object".
1168
+
1169
+ If an object has two fields which are arrays, `.foo` and `.bar`,
1170
+ and you append something to `.foo`, then `.bar` will not get
1171
+ bigger. Even if you've just set `.bar = .foo`. If you're used to
1172
+ programming in languages like Python, Java, Ruby, Javascript,
1173
+ etc. then you can think of it as though jq does a full deep copy
1174
+ of every object before it does the assignment (for performance,
1175
+ it doesn't actually do that, but that's the general idea).
1176
+
1177
+ entries:
1178
+ - title: "`=`"
1179
+ body: |
1180
+
1181
+ The filter `.foo = 1` will take as input an object
1182
+ and produce as output an object with the "foo" field set to
1183
+ 1. There is no notion of "modifying" or "changing" something
1184
+ in jq - all jq values are immutable. For instance,
1185
+
1186
+ .foo = .bar | .foo.baz = 1
1187
+
1188
+ will not have the side-effect of setting .bar.baz to be set
1189
+ to 1, as the similar-looking program in Javascript, Python,
1190
+ Ruby or other languages would. Unlike these languages (but
1191
+ like Haskell and some other functional languages), there is
1192
+ no notion of two arrays or objects being "the same array" or
1193
+ "the same object". They can be equal, or not equal, but if
1194
+ we change one of them in no circumstances will the other
1195
+ change behind our backs.
1196
+
1197
+ This means that it's impossible to build circular values in
1198
+ jq (such as an array whose first element is itself). This is
1199
+ quite intentional, and ensures that anything a jq program
1200
+ can produce can be represented in JSON.
1201
+
1202
+ - title: "`|=`"
1203
+ body: |
1204
+ As well as the assignment operator '=', jq provides the "update"
1205
+ operator '|=', which takes a filter on the right-hand side and
1206
+ works out the new value for the property being assigned to by running
1207
+ the old value through this expression. For instance, .foo |= .+1 will
1208
+ build an object with the "foo" field set to the input's "foo" plus 1.
1209
+
1210
+ This example should show the difference between '=' and '|=':
1211
+
1212
+ Provide input '{"a": {"b": 10}, "b": 20}' to the programs:
1213
+
1214
+ .a = .b
1215
+ .a |= .b
1216
+
1217
+ The former will set the "a" field of the input to the "b" field of the
1218
+ input, and produce the output {"a": 20}. The latter will set the "a"
1219
+ field of the input to the "a" field's "b" field, producing {"a": 10}.
1220
+
1221
+ - title: "`+=`, `-=`, `*=`, `/=`, `//=`"
1222
+ body: |
1223
+
1224
+ jq has a few operators of the form `a op= b`, which are all
1225
+ equivalent to `a |= . op b`. So, `+= 1` can be used to increment values.
1226
+
1227
+ examples:
1228
+ - program: .foo += 1
1229
+ input: '{"foo": 42}'
1230
+ output: ['{"foo": 43}']
1231
+
1232
+ - title: Complex assignments
1233
+ body: |
1234
+ Lots more things are allowed on the left-hand side of a jq assignment
1235
+ than in most langauges. We've already seen simple field accesses on
1236
+ the left hand side, and it's no surprise that array accesses work just
1237
+ as well:
1238
+
1239
+ .posts[0].title = "JQ Manual"
1240
+
1241
+ What may come as a surprise is that the expression on the left may
1242
+ produce multiple results, referring to different points in the input
1243
+ document:
1244
+
1245
+ .posts[].comments |= . + ["this is great"]
1246
+
1247
+ That example appends the string "this is great" to the "comments"
1248
+ array of each post in the input (where the input is an object with a
1249
+ field "posts" which is an array of posts).
1250
+
1251
+ When jq encounters an assignment like 'a = b', it records the "path"
1252
+ taken to select a part of the input document while executing a. This
1253
+ path is then used to find which part of the input to change while
1254
+ executing the assignment. Any filter may be used on the
1255
+ left-hand side of an equals - whichever paths it selects from the
1256
+ input will be where the assignment is performed.
1257
+
1258
+ This is a very powerful operation. Suppose we wanted to add a comment
1259
+ to blog posts, using the same "blog" input above. This time, we only
1260
+ want to comment on the posts written by "stedolan". We can find those
1261
+ posts using the "select" function described earlier:
1262
+
1263
+ .posts[] | select(.author == "stedolan")
1264
+
1265
+ The paths provided by this operation point to each of the posts that
1266
+ "stedolan" wrote, and we can comment on each of them in the same way
1267
+ that we did before:
1268
+
1269
+ (.posts[] | select(.author == "stedolan") | .comments) |=
1270
+ . + ["terrible."]