-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.html
More file actions
724 lines (712 loc) · 25.1 KB
/
json.html
File metadata and controls
724 lines (712 loc) · 25.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<link rel="icon" type="image/gif" href="/favicon.gif"/>
<link rel="apple-touch-icon" sizes="120x120" href="touch-icon-iphone-retina.png" />
<link rel="apple-touch-icon" sizes="152x152" href="touch-icon-ipad-retina.png" />
<title>Lorem Ipsum</title>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<style type="text/css" id="internal-style">
@import url(hyperpolyglot.css);
</style>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<meta http-equiv="content-language" content="en"/>
</head>
<body>
<div id="container-wrap-wrap">
<div id="container-wrap">
<div id="container">
<div id="header">
<h1><a href="/"><span>Hyperpolyglot</span></a></h1>
</div>
<div id="content-wrap">
<div id="main-content">
<div id="page-title">Lorem Ipsum</div>
<div id="page-content">
<p><a name="top"></a><a href="#grammar-invocation">grammar and invocation</a> | <a href="#var-expr">variables and expressions</a> | <a href="#arith-logic">arithmetic and logic</a> | <a href="#strings">strings</a> | <a href="#regex">regular expressions</a> | <a href="#dates-time">dates and time</a> | <a href="#arrays">arrays</a> | <a href="#dictionaries">dictionaries</a> | <a href="#functions">functions</a> | <a href="#exec-control">execution control</a> | <a href="#exceptions">exceptions</a> | <a href="#streams">streams</a> | <a href="#file-fmt">file formats</a> | <a href="#processes-env">processes and environment</a> | <a href="#lib-namespaces">libraries and namespaces</a> | <a href="#reflection">reflection</a></p>
<table class="wiki-content-table">
<tr>
<th colspan="2"><a name="version"></a>version</th>
</tr>
<tr>
<th></th>
<th><a href="#jq">jq</a></th>
</tr>
<tr>
<td><a name="version-used"></a><a href="#version-used-note">version used</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>1.5</em></span></td>
</tr>
<tr>
<td><a name="show-version"></a><a href="#show-version-note">show version</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ jq <span style="white-space: pre-wrap;">--</span>version</td>
</tr>
<tr>
<th colspan="2"><a name="grammar-invocation"></a>grammar and execution</th>
</tr>
<tr>
<th></th>
<th><a href="#jq">jq</a></th>
</tr>
<tr>
<td><a name="interpreter"></a><a href="#interpreter-note">interpreter</a></td>
<td>$ cat > hello.jq<br />
"Hello, World!"<br />
<br />
<span style="color: gray"># -n: use single "null" as input<br />
# -r: write string content instead of quoted string literal to stdout</span><br />
$ jq -nr -f hello.jq<br />
Hello, World!</td>
</tr>
<tr>
<td><a name="cmd-line-program"></a><a href="#cmd-line-program-note">command line program</a></td>
<td>$ echo '1 2 3' | jq '. * .'<br />
<br />
<span style="color: gray"># no stdin:</span><br />
$ jq -n '1 + 1' </td>
</tr>
<tr>
<td><a name="stmt-separator"></a><a href="#stmt-separator-note">statement separator</a></td>
<td><span style="color: gray">A program consists of filter expressions separated by commas and pipes: , |<br />
<br />
A filter expressions acts on its input and produces output.<br />
<br />
The pipe separator makes the output of the left expression the input of the right expression.<br />
<br />
The comma separator, which concatenates the output of two filters, has higher precedence than the pipe separator.<br />
<br />
A function definition, include statement, or import statement is terminated by a semicolon: ;</span></td>
</tr>
<tr>
<td><a name="block-delimiters"></a><a href="#block-delimiters-note">block delimiters</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"># Use parens to change precedence:</span><br />
$ jq -n '2, (3 | . * .)'<br />
$ jq -n '2, 3 | . * .'</td>
</tr>
<tr>
<td><a name="eol-comment"></a><a href="#eol-comment-note">end of line comment</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"># comment</span></td>
</tr>
<tr>
<th colspan="2"><a name="var-expr"></a>variables and expressions</th>
</tr>
<tr>
<th></th>
<th>jq</th>
</tr>
<tr>
<td><a name="assignment"></a><a href="#assignment-note">assignment</a></td>
<td>$ echo '[1,2,3]' | jq 'length as $x | map(. * $x)'<br />
<br />
<span style="color: gray">"length as $x" assigns the value 3 to $x.<br />
<br />
The following filter receives the same input the assignment filter got.</span></td>
</tr>
<tr>
<td><a name="compound-assignment"></a><a href="#compound-assignment-note">compound assignment</a></td>
<td><span style="white-space: pre-wrap;">|= += -= *= /= %= //=</span><br />
<br />
<span style="color: gray">Compound operators are used with arrays and dictionaries. A copy of the array or dictionary is returned, with fields specified on the LHS replaced by new values. If the compound assignment expression is "LHS OP= RHS", then the new value is "LHS OP RHS".</span><br />
<br />
<span style="color: gray"># {"t": 2, "f": 0}:</span><br />
$ echo '{"t": 1, "f": 0}' | jq '.t += 1'<br />
<br />
<span style="color: gray">More than one field can be updated.<br />
<br />
# {"t": 2, "f": 1}:</span><br />
$ echo '{"t": 1, "f": 0}' | jq '.[] += 1'</td>
</tr>
<tr>
<td><a name="null"></a><a href="#null-note">null</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>null</td>
</tr>
<tr>
<td><a name="null-test"></a><a href="#null-test-note">null test</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ echo '{"name": "Ed"}' | jq '.age == null'</td>
</tr>
<tr>
<td><a name="coalesce"></a><a href="#coalesce-note">coalesce</a></td>
<td><span style="color: gray"># also replaces false:</span><br />
$ echo '{}' | jq '.age // 0'</td>
</tr>
<tr>
<td><a name="conditional-expr"></a><a href="#conditional-expr-note">conditional expression</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ echo '-3' | jq 'if . < 0 then -. else . end'</td>
</tr>
<tr>
<th colspan="2"><a name="arith-logic"></a>arithmetic and logic</th>
</tr>
<tr>
<th></th>
<th>jq</th>
</tr>
<tr>
<td><a name="true-false"></a><a href="#true-false-note">true and false</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>true false</td>
</tr>
<tr>
<td><a name="falsehoods"></a><a href="#falsehoods-note">falsehoods</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>null false</td>
</tr>
<tr>
<td><a name="logical-op"></a><a href="#logical-op-note">logical operators</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>and or not</td>
</tr>
<tr>
<td><a name="relational-op"></a><a href="#relational-op-note">relational operators</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="white-space: pre-wrap;">==</span> != < > <= >=</td>
</tr>
<tr>
<td><a name="min-max"></a><a href="#min-max-note">min and max</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ echo '[1,2,3]' | jq 'min'<br />
$ echo '[1,2,3]' | jq 'max'</td>
</tr>
<tr>
<td><a name="arith-op"></a><a href="#arith-op-note">arithmetic operators</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>+ - * / %</td>
</tr>
<tr>
<td><a name="int-division"></a><a href="#int-division-note">integer division</a></td>
<td><span style="color: gray"># floor is new in 1.4:</span><br />
$ jq -n '-13 / 5 | floor'</td>
</tr>
<tr>
<td><a name="division-by-zero"></a><a href="#division-by-zero-note">division by zero</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>error</em></span></td>
</tr>
<tr>
<td><a name="sqrt"></a><a href="#sqrt-note">sqrt</a></td>
<td><span style="color: gray"># new in 1.4:</span><br />
$ jq -n '2 | sqrt'</td>
</tr>
<tr>
<td><a name="sqrt-negative-one"></a><a href="#sqrt-negative-one-note">sqrt -1</a></td>
<td><span style="color: gray"># null:</span><br />
$ jq -n '-1 | sqrt'</td>
</tr>
<tr>
<td><a name="transcendental-func"></a><a href="#transcendental-func-note">transcendental functions</a></td>
<td><span style="color: gray"># new in 1.5:</span><br />
exp log<br />
sin cos tan<br />
asin acos atan</td>
</tr>
<tr>
<td><a name="float-trunc"></a><a href="#float-trunc-note">float truncation</a></td>
<td><span style="color: gray"># floor is new in 1.4:</span><br />
$ jq '1.1 | floor'</td>
</tr>
<tr>
<th colspan="2"><a name="strings"></a>strings</th>
</tr>
<tr>
<th></th>
<th>jq</th>
</tr>
<tr>
<td><a name="str-literal"></a><a href="#str-literal-note">literal</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>"lorem ipsum"</td>
</tr>
<tr>
<td><a name="newline-in-literal"></a><a href="#new-line-in-literal-note">newline in literal</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>not allowed; use</em> \n <em>escape sequence</em></span></td>
</tr>
<tr>
<td><a name="str-esc"></a><a href="#str-esc-note">string escapes</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>\\ \" \/ \b \f \n \r \r \t \u<span style="color: gray"><em>hhhh</em></span></td>
</tr>
<tr>
<td><a name="var-interpolation"></a><a href="#var-interpolation-note">variable interpolation</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ echo '{"foo": 1, "bar": 2}' | jq '"\(.foo):\(.bar)"'</td>
</tr>
<tr>
<td><a name="concat"></a><a href="#concat-note">concatenate</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>"foo" + "bar"</td>
</tr>
<tr>
<td><a name="translate-case"></a><a href="#translate-case-note">translate case</a></td>
<td>$ echo '"foo"' | jq 'ascii_upcase<br />
$ echo '"FOO"' | jq 'ascii_downcase'</td>
</tr>
<tr>
<td><a name="num-to-str"></a><a href="#num-to-str-note">number to string</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ echo '7' | jq 'tostring'</td>
</tr>
<tr>
<td><a name="str-to-num"></a><a href="#str-to-num-note">string to number</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ echo '"7"' | jq 'tonumber'</td>
</tr>
<tr>
<td><a name="str-join"></a><a href="#str-join-note">string join</a></td>
<td><span style="color: gray"># join is new in 1.4:</span><br />
$ echo '["do", "re", "mi"]' | jq 'join(" ")'</td>
</tr>
<tr>
<td><a name="split"></a><a href="#split-note">split</a></td>
<td><span style="color: gray"># split is new in 1.4:</span><br />
$ echo '"do re mi"' | jq 'split(" ")'<br />
<br />
<span style="color: gray"># remove two empty strings:</span><br />
$ echo '" do re mi"' | jq 'split(" ") | map(select(length > 0))'</td>
</tr>
<tr>
<td><a name="prefix-suffix-test"></a><a href="#prefix-suffix-test-note">prefix and suffix test</a></td>
<td>$ echo '"foobar"' | jq 'startswith("foo")'<br />
$ echo '"foobar"' | jq 'endswith("bar")'</td>
</tr>
<tr>
<td><a name="str-len"></a><a href="#str-len-note">string length</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ jq -R 'length' /etc/hosts</td>
</tr>
<tr>
<td><a name="index-substr"></a><a href="#index-substr-note">index of substring</a><br />
<br />
<span style="color: gray"><em>first, last</em></span></td>
<td><span style="color: gray"># 3:</span><br />
$ echo '"do re re"' | jq 'index("re")'<br />
<br />
<span style="color: gray"># 6:</span><br />
$ echo '"do re re"' | jq 'rindex("re")'<br />
<br />
<span style="color: gray"># null:</span><br />
$ echo '"do re re"' | jq 'rindex("mi")'</td>
</tr>
<tr>
<th colspan="2"><a name="regex"></a>regular expressions</th>
</tr>
<tr>
<th></th>
<th>jq</th>
</tr>
<tr>
<td><a name="regex-literal"></a><a href="#regex-literal-note">literal</a></td>
<td><span style="color: gray"># regexes are new in 1.5:</span><br />
"lorem|ipsum"</td>
</tr>
<tr>
<td><a name="char-class-abbrev"></a><a href="#char-class-abbrev-note">character class abbrevations</a></td>
<td><span style="color: gray"><em>but backslashes must be doubled inside double quotes:</em></span><br />
. \d \D \h \H \s \S \w \W</td>
</tr>
<tr>
<td><a name="anchors"></a><a href="#anchors-note">anchors</a></td>
<td><span style="color: gray"><em>but backslashes must be doubled inside double quotes:</em></span><br />
^ $ \A \b \B \z \Z</td>
</tr>
<tr>
<td><a name="match-test"></a><a href="#match-test-note">match test</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ echo $'"It\'s 1999!"' | jq '. | test("1999")'</td>
</tr>
<tr>
<td><a name="case-insensitive-match-test"></a><a href="#case-insensitive-match-test-note">case insensitive match test</a></td>
<td>$ echo $'"FOO BAR"' | jq 'test("foo"; "i")'<br />
$ echo $'"FOO BAR"' | jq 'test(["foo", "i"])'<br />
$ echo $'"FOO BAR"' | jq 'test("(?i)foo")'</td>
</tr>
<tr>
<td><a name="subst"></a><a href="#subst-note">substitution</a><br />
<br />
<span style="color: gray"><em>first occurrence, all occurrences</em></span></td>
<td>$ echo '"do re mi mi mi"' | jq 'sub("mi"; "ma")'<br />
$ echo '"do re mi mi mi"' | jq 'gsub("mi"; "ma")'</td>
</tr>
<tr>
<td><a name="named-group-capture"></a><a href="#named-group-capture-note">named group capture</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ echo '"foo.txt"' | jq 'capture("^(?<root>.*)\\.(?<suffix>.*)$") | .root'</td>
</tr>
<tr>
<th colspan="2"><a name="dates-time"></a>dates and time</th>
</tr>
<tr>
<th></th>
<th>jq</th>
</tr>
<tr>
<td><a name="current-datetime"></a><a href="#current-datetime-note">current datetime</a></td>
<td><span style="color: gray"># date/time functions are new in 1.5</span><br />
<br />
<span style="color: gray"># array of broken-down datetime values:</span><br />
$ jq -n 'now | gmtime' <br />
<br />
<span style="color: gray"># ISO 8601 format:</span><br />
$ jq -n 'now | gmtime | todate'</td>
</tr>
<tr>
<td><a name="current-unix-epoch"></a><a href="#current-unix-epoch-note">current unix epoch</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ jq -n 'now'</td>
</tr>
<tr>
<td><a name="broken-down-datetime-to-unix-epoch"></a><a href="#broken-down-datetime-to-unix-epoch-note">broken-down datetime to unix epoch</a></td>
<td>$ echo '[2016,11,15,11,30,0,4,349]' | jq 'mktime'</td>
</tr>
<tr>
<td><a name="unix-epoch-to-broken-down-datetime"></a><a href="#unix-epoch-to-broken-down-datetime-note">unix epoch to broken-down datetime</a></td>
<td>$ echo 1481801400 | jq 'gmtime'</td>
</tr>
<tr>
<td><a name="fmt-datetime"></a><a href="#fmt-datetime-note">format datetime</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ jq -n 'now | gmtime | strftime("%Y-%m-%d %H:%M:%S")'</td>
</tr>
<tr>
<td><a name="parse-datetime"></a><a href="#parse-datetime-note">parse datetime</a></td>
<td><span style="color: gray"># parses to array of broken-down datetime values:</span><br />
$ echo '"2016-12-15 11:30:00"' | jq 'strptime("%Y-%m-%d %H:%M:%S")'</td>
</tr>
<tr>
<th colspan="2"><a name="arrays"></a>arrays</th>
</tr>
<tr>
<th></th>
<th>jq</th>
</tr>
<tr>
<td>lookup</td>
<td><span style="color: gray"># 6:</span><br />
$ echo '[6, 7, 8, 9'] | jq '.[0]'</td>
</tr>
<tr>
<td>not-an-array behavior</td>
<td><span style="color: gray"># error:</span><br />
$ echo 1 | jq '.[0]'<br />
<br />
<span style="color: gray"># no error and no output:</span><br />
$ echo 1 | jq '.[0]?'</td>
</tr>
<tr>
<td>length</td>
<td>$ echo '[6, 7, 8, 9'] | jq 'length'</td>
</tr>
<tr>
<td>slice</td>
<td><span style="color: gray"># [7, 8]:</span><br />
$ echo '[6,7,8,9]' | jq '.[1:3]'</td>
</tr>
<tr>
<td>slice from beginning</td>
<td>$ jq -c '.[:2]'</td>
</tr>
<tr>
<td>slice to end</td>
<td><span style="color: gray"><em>from element 3 on:</em></span><br />
$ jq -c '.[2:]'<br />
<br />
<span style="color: gray"><em>last two elements:</em></span><br />
$ jq -c '.[-2:]'</td>
</tr>
<tr>
<td>indices</td>
<td>$ jq -c 'keys'</td>
</tr>
<tr>
<td>reverse</td>
<td>$ echo '[1,2,3]' | jq 'reverse'</td>
</tr>
<tr>
<td>sort</td>
<td>$ echo '[3,1,4,2]' | jq 'sort' </td>
</tr>
<tr>
<td>dedupe</td>
<td>$ echo '[1,1,2,3]' | jq 'unique'</td>
</tr>
<tr>
<td>subset test</td>
<td>$ echo '[1,2,3]' | jq 'contains([1])'<br />
$ echo '[1]' | jq 'inside([1,2,3])' </td>
</tr>
<tr>
<td>map</td>
<td>$ echo '[1,2,3]' | jq '.[] | . * .'<br />
$ echo '[1,2,3]' | jq 'map(. * .)'</td>
</tr>
<tr>
<td>filter</td>
<td>$ echo '[1,2,3]' | jq 'map(select(. > 2))'</td>
</tr>
<tr>
<td>reduce</td>
<td>$ echo '[1,2,3,4]'| jq 'reduce .[] as $item (0; .+$item)'</td>
</tr>
<tr>
<td>universal and existential test</td>
<td>$ echo '[1,2,3]' | jq 'all(. > 2)'<br />
$ echo '[1,2,3]' | jq 'any(. > 2)'</td>
</tr>
<tr>
<td>flatten<br />
<span style="color: gray"><em>one level, completely</em></span></td>
<td><span style="color: gray"># flatten is new in 1.5:</span><br />
$ echo '[1,[2,[3]]]' | jq -c 'flatten(1)'<br />
$ echo '[1,[2,[3]]]' | jq -c 'flatten'</td>
</tr>
<tr>
<th colspan="2"><a name="dictionaries"></a>dictionaries</th>
</tr>
<tr>
<th></th>
<th>jq</th>
</tr>
<tr>
<td>literal</td>
<td>{"t": 1, "f": 0}</td>
</tr>
<tr>
<td>size</td>
<td>$ echo '{"t": 1, "f": 0}' | jq 'length'</td>
</tr>
<tr>
<td>lookup</td>
<td>$ echo '{"t": 1, "f": 0}' | jq '.t'<br />
$ echo '{"t": 1, "f": 0}' | jq '.["t"]'</td>
</tr>
<tr>
<td>update</td>
<td>$ echo '{"t": 1, "f": 0}' | jq '.f = -1'</td>
</tr>
<tr>
<td>is key present</td>
<td>$ echo '{"t": 1, "f": 0}' | jq 'has("t")'<br />
$ echo '"t"' | jq 'in({"t": 1, "f": 0})'</td>
</tr>
<tr>
<td>missing key behavior</td>
<td><span style="color: gray"># null:</span><br />
$ echo '{"t": 1, "f": 0}' | jq '.m' </td>
</tr>
<tr>
<td>not a dictionary behavior</td>
<td><span style="color: gray"># error:</span><br />
$ echo 1 | jq '.foo'<br />
<br />
<span style="color: gray"># no error and no output:</span><br />
$ echo 1 | jq '.foo?'</td>
</tr>
<tr>
<td>delete</td>
<td>$ echo '{"t": 1, "f": 0}' | jq 'del(.t)'</td>
</tr>
<tr>
<td>keys and values as arrays</td>
<td>$ echo '{"do": 1, "re": 2}' | jq 'keys'<br />
$ echo '{"do": 1, "re": 2}' | jq 'to_entries | map(.value)'</td>
</tr>
<tr>
<th colspan="2"><a name="functions"></a>functions</th>
</tr>
<tr>
<th></th>
<th>jq</th>
</tr>
<tr>
<td>define</td>
<td>$ echo '1 2 3 4' | jq 'def double: 2 * .;'</td>
</tr>
<tr>
<td>call</td>
<td>$ echo '1 2 3 4' | jq 'def double: 2 * .; double'</td>
</tr>
<tr>
<td>function with argument</td>
<td>$ echo '1 2 3 4' | jq 'def multiply($x): . * $x; multiply(7)'<br />
<br />
<span style="color: gray"># without $, argument is treated as a filter:</span><br />
echo '1 2 3 4' | jq 'def apply(f): f; apply(. * 7)'</td>
</tr>
<tr>
<th colspan="2"><a name="exec-control"></a>execution control</th>
</tr>
<tr>
<th></th>
<th>jq</th>
</tr>
<tr>
<td>if</td>
<td>$ echo '-3' | jq 'if . < 0 then -. else . end'</td>
</tr>
<tr>
<td>while</td>
<td>$ jq -n '1 | while(. < 10; . + 1)' </td>
</tr>
<tr>
<td>break</td>
<td>$ jq -n '1 | label $out | while(true; if . > 10 then break $out else. + 1 end)'</td>
</tr>
<tr>
<th colspan="2"><a name="exceptions"></a>exceptions</th>
</tr>
<tr>
<th></th>
<th>jq</th>
</tr>
<tr>
<td>raise exception</td>
<td>$ jq -n 'error("bam!")'</td>
</tr>
<tr>
<td>handle exception</td>
<td><span style="color: gray"># set output to null:</span><br />
$ jq -n 'try error("bam!") catch null'<br />
<br />
<span style="color: gray"># two ways to handle with no output:</span><br />
$ jq -n 'try error("bam!") catch empty'<br />
$ jq -n 'error("bam!")?'</td>
</tr>
<tr>
<th colspan="2"><a name="streams"></a>streams</th>
</tr>
<tr>
<th></th>
<th>jq</th>
</tr>
<tr>
<td>write to stderr</td>
<td><span style="color: gray"># input to debug is also passed on to subsequent filters:</span><br />
$ jq -n '"foo" | debug'</td>
</tr>
<tr>
<td>read input as array of strings</td>
<td>$ jq -R 'length' /etc/hosts</td>
</tr>
<tr>
<td>array to stream</td>
<td>$ echo '[1,2,3]' | jq '.[]'</td>
</tr>
<tr>
<td>stream to array</td>
<td>$ echo '1 2 3' | jq -s '.'</td>
</tr>
<tr>
<th colspan="2"><a name="file-fmt"></a>file formats</th>
</tr>
<tr>
<th></th>
<th>jq</th>
</tr>
<tr>
<th colspan="2"><a name="processes-env"></a>processes and environment</th>
</tr>
<tr>
<th></th>
<th>jq</th>
</tr>
<tr>
<td>environment variable</td>
<td>$ jq -n 'env.HOME' <br />
$ jq -n 'env["HOME"]'</td>
</tr>
<tr>
<th colspan="2"><a name="lib-namespaces"></a>libraries and namespaces</th>
</tr>
<tr>
<th></th>
<th>jq</th>
</tr>
<tr>
<td>load library</td>
<td>$ cat > multiples.jq <br />
def double: 2 * .;<br />
def triple: 3 * .;<br />
def quadruple; 4 * .;<br />
<br />
$ echo 3 | jq 'include "multiples"; double'</td>
</tr>
<tr>
<td>library path command line option</td>
<td>$ mkdir ~/.jq<br />
$ cat > ~/.jq/multiples.jq<br />
def double: 2 * .;<br />
def triple: 3 * .;<br />
def quadruple; 4 * .;<br />
<br />
$ echo 3 | jq -L ~/.jq 'include "multiples"; double'</td>
</tr>
<tr>
<td>load definitions in namespace</td>
<td>$ cat > multiples.jq <br />
def double: 2 * .;<br />
def triple: 3 * .;<br />
def quadruple; 4 * .;<br />
<br />
$ echo 3 | jq 'import "multiples" as mult; mult::double'</td>
</tr>
<tr>
<th colspan="2"><a name="reflection"></a>reflection</th>
</tr>
<tr>
<th></th>
<th>jq</th>
</tr>
<tr>
<td><a name="inspect-type"></a><a href="#inspect-type-note">inspect type</a></td>
<td>$ echo '{"foo": 1}' | jq '.foo | type'</td>
</tr>
<tr>
<td><a name="basic-types"></a><a href="#basic-types-note">basic types</a></td>
<td>"number"<br />
"boolean"<br />
"array"<br />
"object"<br />
"null"<br />
"string"</td>
</tr>
<tr>
<th></th>
<th><span style="color: #efefef"><span style="white-space: pre-wrap;">___________________________________________________________________</span></span></th>
</tr>
</table>
<p><a name="jq"></a></p>
<h1 id="toc0"><span>Jq</span></h1>
<p><a href="http://manpages.ubuntu.com/manpages/xenial/man1/jq.1.html">jq man page</a></p>
</div>
</div>
</div>
<div id="license-area" class="license-area">
<a href="https://github.com/clarkgrubb/hyperpolyglot/issues">issue tracker</a> |
content of this page licensed under
<a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">
creative commons attribution-sharealike 3.0</a>
<br>
</div>
</div>
</div>
</div>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-17129977-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>