-
Notifications
You must be signed in to change notification settings - Fork 11
/
jt.1.html
755 lines (619 loc) · 23 KB
/
jt.1.html
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='content-type' value='text/html;charset=utf8'>
<meta name='generator' value='Ronn/v0.7.3 (http://github.com/rtomayko/ronn/tree/0.7.3)'>
<title>jt(1) - transform JSON data to tabular format</title>
<style type='text/css' media='all'>
/* style: man */
body#manpage {margin:0}
.mp {max-width:100ex;padding:0 9ex 1ex 4ex}
.mp p,.mp pre,.mp ul,.mp ol,.mp dl {margin:0 0 20px 0}
.mp h2 {margin:10px 0 0 0}
.mp > p,.mp > pre,.mp > ul,.mp > ol,.mp > dl {margin-left:8ex}
.mp h3 {margin:0 0 0 4ex}
.mp dt {margin:0;clear:left}
.mp dt.flush {float:left;width:8ex}
.mp dd {margin:0 0 0 9ex}
.mp h1,.mp h2,.mp h3,.mp h4 {clear:left}
.mp pre {margin-bottom:20px}
.mp pre+h2,.mp pre+h3 {margin-top:22px}
.mp h2+pre,.mp h3+pre {margin-top:5px}
.mp img {display:block;margin:auto}
.mp h1.man-title {display:none}
.mp,.mp code,.mp pre,.mp tt,.mp kbd,.mp samp,.mp h3,.mp h4 {font-family:monospace;font-size:14px;line-height:1.42857142857143}
.mp h2 {font-size:16px;line-height:1.25}
.mp h1 {font-size:20px;line-height:2}
.mp {text-align:justify;background:#fff}
.mp,.mp code,.mp pre,.mp pre code,.mp tt,.mp kbd,.mp samp {color:#131211}
.mp h1,.mp h2,.mp h3,.mp h4 {color:#030201}
.mp u {text-decoration:underline}
.mp code,.mp strong,.mp b {font-weight:bold;color:#131211}
.mp em,.mp var {font-style:italic;color:#232221;text-decoration:none}
.mp a,.mp a:link,.mp a:hover,.mp a code,.mp a pre,.mp a tt,.mp a kbd,.mp a samp {color:#0000ff}
.mp b.man-ref {font-weight:normal;color:#434241}
.mp pre {padding:0 4ex}
.mp pre code {font-weight:normal;color:#434241}
.mp h2+pre,h3+pre {padding-left:0}
ol.man-decor,ol.man-decor li {margin:3px 0 10px 0;padding:0;float:left;width:33%;list-style-type:none;text-transform:uppercase;color:#999;letter-spacing:1px}
ol.man-decor {width:100%}
ol.man-decor li.tl {text-align:left}
ol.man-decor li.tc {text-align:center;letter-spacing:4px}
ol.man-decor li.tr {text-align:right;float:right}
</style>
<style type='text/css' media='all'>
/* style: jt.1 */
.mp {margin-left: auto;margin-right: auto}
</style>
</head>
<!--
The following styles are deprecated and will be removed at some point:
div#man, div#man ol.man, div#man ol.head, div#man ol.man.
The .man-page, .man-decor, .man-head, .man-foot, .man-title, and
.man-navigation should be used instead.
-->
<body id='manpage'>
<div class='mp' id='man'>
<div class='man-navigation' style='display:none'>
<a href="#NAME">NAME</a>
<a href="#SYNOPSIS">SYNOPSIS</a>
<a href="#DESCRIPTION">DESCRIPTION</a>
<a href="#OPTIONS">OPTIONS</a>
<a href="#OPERATION">OPERATION</a>
<a href="#COMMANDS">COMMANDS</a>
<a href="#OUTPUT-FORMAT">OUTPUT FORMAT</a>
<a href="#EXIT-STATUS">EXIT STATUS</a>
<a href="#EXAMPLES">EXAMPLES</a>
<a href="#SEE-ALSO">SEE ALSO</a>
<a href="#BUGS">BUGS</a>
<a href="#COPYRIGHT">COPYRIGHT</a>
</div>
<ol class='man-decor man-head man head'>
<li class='tl'>jt(1)</li>
<li class='tc'>JT MANUAL</li>
<li class='tr'>jt(1)</li>
</ol>
<h2 id="NAME">NAME</h2>
<p class="man-name">
<code>jt</code> - <span class="man-whatis">transform JSON data to tabular format</span>
</p>
<h2 id="SYNOPSIS">SYNOPSIS</h2>
<p><code>jt</code> <code>[-hV]</code><br />
<code>jt</code> <code>-u</code> <var>string</var><br />
<code>jt</code> [<code>-acj</code>] [<code>COMMAND</code> ...]</p>
<h2 id="DESCRIPTION">DESCRIPTION</h2>
<p><strong>Jt</strong> reads UTF-8 encoded JSON forms from <var>stdin</var> and writes tab separated
values (or CSV) to <var>stdout</var>. A simple stack-based programming language is used
to extract values from the JSON input for printing.</p>
<h2 id="OPTIONS">OPTIONS</h2>
<dl>
<dt class="flush"><code>-h</code></dt><dd><p>Print usage info and exit.</p></dd>
<dt class="flush"><code>-V</code></dt><dd><p>Print version and license info and exit.</p></dd>
<dt class="flush"><code>-a</code></dt><dd><p>Explicit iteration mode: require the <code>.</code> command to iterate over arrays
instead of iterating automatically.</p></dd>
<dt class="flush"><code>-c</code></dt><dd><p>CSV output mode: write RFC 4180 compliant CSV records.</p></dd>
<dt class="flush"><code>-j</code></dt><dd><p>Inner join mode: discard rows with missing columns.</p></dd>
<dt class="flush"><code>-s</code></dt><dd><p>A no-op, included for compatibility with earlier versions.</p></dd>
<dt><code>-u</code> <var>string</var></dt><dd><p>Unescape JSON <var>string</var>, print it, and exit. Double-quotes around <var>string</var>
are optional.</p></dd>
</dl>
<h2 id="OPERATION">OPERATION</h2>
<p>Non-option arguments are words (commands) in a stack-based programming language.
Commands operate on the stacks provided by the <strong>jt</strong> runtime:</p>
<ul>
<li><p>The <code>DATA</code> stack contains JSON values commands will operate on.</p></li>
<li><p>The <code>GOSUB</code> stack contains pointers into the data stack.</p></li>
<li><p>The <code>INDEX</code> stack contains pointers to iterators during iteration.</p></li>
<li><p>The <code>LOOP</code> stack contains pointers to iterators between iterations.</p></li>
<li><p>The <code>OUTPUT</code> stack contains values to print after each iteration.</p></li>
</ul>
<p>When <strong>jt</strong> starts, the following happens:</p>
<ol>
<li><p>One JSON form is read from <var>stdin</var>, parsed, and pushed onto the data stack.</p></li>
<li><p>If the top of the data stack is a JSON array the next item in the array is
pushed onto the data stack based on state previously saved on the loop stack.
The loop stack is then popped and pushed onto the index stack.</p></li>
<li><p>The next command is executed.</p></li>
<li><p>Steps 2 and 3 are repeated until no commands are left to execute.</p></li>
<li><p>Values in the output stack are printed, separated by tabs and followed by a
newline (see <strong>OUTPUT FORMAT</strong> below). Data, output, and gosub stacks are
reset to their states as they were after step 1.</p></li>
<li><p>Loop variables are popped off of the index stack. They are then incremented
and pushed onto the loop stack as necessary.</p></li>
<li><p>Steps 2 through 6 are repeated until the loop stack is empty (i.e. there are
no more iterations left).</p></li>
</ol>
<p>This process is repeated until there is no more JSON to read.</p>
<h2 id="COMMANDS">COMMANDS</h2>
<p><strong>Jt</strong> provides the following commands:</p>
<dl>
<dt class="flush"><code>[</code></dt><dd><p>Save the state of the data stack: the current data stack pointer is pushed
onto the gosub stack.</p></dd>
<dt class="flush"><code>]</code></dt><dd><p>Restore the data stack to a saved state: pop the gosub stack and restore
the data stack pointer to that state.</p></dd>
<dt class="flush"><code>%</code></dt><dd><p>Push the value at the top of the data stack onto the output stack.</p></dd>
<dt class="flush"><code>%=</code><var>NAME</var></dt><dd><p>Like <code>%</code>, but also sets the heading for this column to <var>NAME</var>. Headers
will be printed as the first line of output when this command has been used.</p></dd>
<dt class="flush"><code>^</code></dt><dd><p>Push the value at the top of the index stack onto the output stack. Note
that the index stack will always have at least one item: the index of the
last JSON object read from stdin.</p></dd>
<dt class="flush"><code>^=</code><var>NAME</var></dt><dd><p>Like <code>^</code>, but also sets the heading for this column to <var>NAME</var>. Headers
will be printed as the first line of output when this command has been used.</p></dd>
<dt class="flush"><code>@</code></dt><dd><p>Print the keys of the object at the top of the data stack and exit.</p>
<p>If the value at the top of the data stack is an array the first item in the
array is pushed onto the data stack and the command is reevaluated.</p>
<p>If the value at the top of the data stack is not an object or an array then
its type is printed in brackets, eg. <var>[array]</var>, <var>[string]</var>, <var>[number]</var>,
<var>[boolean]</var>, or <var>[null]</var>. If there is no value then <var>[none]</var> is printed.</p></dd>
<dt class="flush"><code>+</code></dt><dd><p>Parse embedded JSON: if the item at the top of the data stack is a string,
parse it and push the result onto the data stack.</p>
<p>If the item at the top of the data stack is not a string or if there is an
error parsing the JSON then nothing is done (the operation is a no-op).</p></dd>
<dt class="flush"><code>.</code></dt><dd><p>Iterate over the values of the object at the top of the data stack. The
current value will be pushed onto the data stack and the current key will
be pushed onto the index stack.</p></dd>
<dt class="flush"><code>[</code><var>KEY</var><code>]</code></dt><dd><p>Drill down: get the value of the <var>KEY</var> property of the object at the top
of the data stack and push that value onto the data stack.</p>
<p>If the item at the top of the data stack is not an object or if the object
has no <var>KEY</var> property a blank field is printed, unless the <code>-j</code> option was
specified in which case the entire row is removed from the output.</p>
<p>If the <var>KEY</var> property of the object is an array subsequent commands will
operate on one of the items in the array, chosen automatically by <code>jt</code>.
The array index will be available to subsequent commands via the index
stack.</p></dd>
<dt class="flush"><var>KEY</var></dt><dd><p>See <code>[</code><var>KEY</var><code>]</code> above — the <code>[</code> and <code>]</code> may be omitted if the
property name <var>KEY</var> does not conflict with any <code>jt</code> command.</p></dd>
</dl>
<h2 id="OUTPUT-FORMAT">OUTPUT FORMAT</h2>
<p>The format of printed output from <strong>jt</strong> (see <code>%</code> in <strong>COMMANDS</strong>, above)
depends on the type of thing being printed:</p>
<h3 id="Primitives">Primitives</h3>
<p>JSON primitives (i.e. <code>null</code>, <code>true</code>, and <code>false</code>) and numbers are printed
verbatim. <strong>Jt</strong> does not process them in any way. Numbers can be of arbitrary
precision, as long as they conform to the JSON parsing grammar.</p>
<p>If special formatting is required the <code>printf</code> program is your friend:</p>
<pre><code>$ printf %.0f 2.99792458e9
2997924580
</code></pre>
<h3 id="Strings">Strings</h3>
<p>Strings are printed verbatim, minus the enclosing double quotes. No unescaping
is performed because tabs or newlines in JSON strings would break the tabular
output format.</p>
<p>If unescaped values are desired either use the <code>-c</code> flag to enable CSV output
(see <strong>CSV</strong> below) or use the <code>-u</code> option and pass the string as an argument:</p>
<pre><code>$ jt -u 'i love music \u266A'
i love music ♪
</code></pre>
<h3 id="Collections">Collections</h3>
<p>Objects and arrays are printed as JSON with whitespace removed. Note that it
is only possible to print arrays when the <code>-a</code> option is specified (see the
<strong>Iteration (Arrays)</strong> and <strong>Explicit Iteration</strong> sections below).</p>
<h3 id="CSV">CSV</h3>
<p>The <code>-c</code> flag enables CSV output conforming to RFC 4180. This format supports
strings containing tabs, newlines, etc.:</p>
<pre><code>$ jt -c [ foo % ] [ bar % ] baz % <<EOT
- {
- "foo": "one\ttwo",
- "bar": "three\nfour",
- "baz": "music \"\u266A\""
- }
- EOT
"one two","three
four","music ""♪"""
</code></pre>
<h2 id="EXIT-STATUS">EXIT STATUS</h2>
<p><strong>Jt</strong> will exit with a status of 1 if an error occurred, or 0 otherwise.</p>
<h2 id="EXAMPLES">EXAMPLES</h2>
<p>Below are a number of examples demonstrating how to use <strong>jt</strong> commands to do
some simple exploration and extraction of data from JSON and JSON streams.</p>
<h3 id="Explore">Explore</h3>
<p>The <code>@</code> command prints information about the item at the top of the data stack.
When the item is an object <code>@</code> prints its keys:</p>
<pre><code class="bash">$ jt @ <<EOT
- {
- "foo": 100,
- "bar": 200,
- "baz": 300
- }
- EOT
foo
bar
baz
</code></pre>
<p>When the top item is an array <code>@</code> prints information about the first item in
the array:</p>
<pre><code class="bash">$ jt @ <<EOT
- [
- {"foo": 100, "bar": 200},
- {"baz": 300, "baf": 400}
- ]
- EOT
foo
bar
</code></pre>
<p>Otherwise, <code>@</code> prints the type of the item, or <var>[none]</var> if there is no value:</p>
<pre><code class="bash">$ echo '["hello", "world"]' | jt @
[string]
</code></pre>
<pre><code class="bash">$ echo '[1, 2]' | jt @
[number]
</code></pre>
<pre><code class="bash">$ echo '[true, false]' | jt @
[boolean]
</code></pre>
<pre><code class="bash">$ echo '[null, null]' | jt @
[null]
</code></pre>
<pre><code class="bash">$ echo '[]' | jt @
[none]
</code></pre>
<h3 id="Drill-Down">Drill Down</h3>
<p>Property names are also commands. Use <code>foo</code> here as a command to drill down
into the <var>foo</var> property and then use <code>@</code> to print its keys:</p>
<pre><code class="bash">$ jt foo @ <<EOT
- {
- "foo": {
- "bar": 100,
- "baz": 200
- }
- }
- EOT
bar
baz
</code></pre>
<p>A property name that conflicts with a <strong>jt</strong> command may be wrapped with square
brackets to drill down:</p>
<pre><code class="bash">$ jt [@] @ <<EOT
- {
- "@": {
- "bar": 100,
- "baz": 200
- }
- }
- EOT
bar
baz
</code></pre>
<p><strong>Note:</strong> The property name must be a JSON escaped string, so if the property
name in the JSON input contains eg. tab characters they must be escaped in the
command, like this:</p>
<pre><code class="bash">$ jt 'foo\tbar' @ <<EOT
- {
- "foo\tbar": {
- "bar": 100,
- "baz": 200
- }
- }
- EOT
bar
baz
</code></pre>
<h3 id="Extract">Extract</h3>
<p>The <code>%</code> command prints the item at the top of the data stack. Note that when
the top item is a collection it is printed as JSON (insiginificant whitespace
removed):</p>
<pre><code class="bash">$ jt % <<EOT
- {
- "foo": 100,
- "bar": 200
- }
- EOT
{"foo":100,"bar":200}
</code></pre>
<p>Drill down and print:</p>
<pre><code class="bash">$ jt foo bar % <<EOT
- {
- "foo": {
- "bar": 100
- }
- }
- EOT
100
</code></pre>
<p>The <code>%</code> command can be used multiple times. The printed values will be separated
by tabs:</p>
<pre><code class="bash">$ jt % foo % bar % <<EOT
- {
- "foo": {
- "bar": 100
- }
- }
- EOT
{"foo":{"bar":100}} {"bar":100} 100
</code></pre>
<h3 id="Save-Restore">Save / Restore</h3>
<p>The <code>[</code> and <code>]</code> commands provide a way to save the data stack's state before
performing some operations and restore it afterwards. This makes it possible
to drill down into one part of an object and then "rewind" to drill down into
another part of the same object.</p>
<p>The <code>[</code> command saves the state of the data stack and the <code>]</code> command restores
the data stack to the corresponding saved state. It is an error to use the <code>]</code>
command without a corresponding <code>[</code> command.</p>
<pre><code class="bash">$ jt [ foo % ] bar % <<EOT
- {
- "foo": 100,
- "bar": 200
- }
- EOT
100 200
</code></pre>
<p>The <code>[</code> and <code>]</code> commands can be nested:</p>
<pre><code class="bash">$ jt [ foo [ bar % ] [ baz % ] ] baf % <<EOT
- {
- "foo": {
- "bar": 100,
- "baz": 200
- },
- "baf": "quux"
- }
- EOT
100 200 quux
</code></pre>
<h3 id="Iteration-Arrays-">Iteration (Arrays)</h3>
<p><strong>Jt</strong> automatically iterates over arrays (unless this behavior is disabled
— see <strong>Explicit Iteration</strong> below), producing one tab-delimited record
per iteration, records separated by newlines:</p>
<pre><code class="bash">$ jt [ foo % ] bar baz % <<EOT
- {
- "foo": 100,
- "bar": [
- {"baz": 200},
- {"baz": 300},
- {"baz": 400}
- ]
- }
- EOT
100 200
100 300
100 400
</code></pre>
<p>The <code>^</code> command includes the array index as a column in the result:</p>
<pre><code class="bash">$ jt [ foo % ] bar ^ baz % <<EOT
- {
- "foo": 100,
- "bar": [
- {"baz": 200},
- {"baz": 300},
- {"baz": 400}
- ]
- }
- EOT
100 0 200
100 1 300
100 2 400
</code></pre>
<p>The <code>^</code> command is scoped to the innermost enclosing loop:</p>
<pre><code class="bash">$ jt foo ^ bar ^ % <<EOT
- {
- "foo": [
- {"bar": [100, 200]},
- {"bar": [300, 400]}
- ]
- }
- EOT
0 0 100
0 1 200
1 0 300
1 1 400
</code></pre>
<h3 id="Iteration-Objects-">Iteration (Objects)</h3>
<p>The <code>.</code> command iterates over the values of an object:</p>
<pre><code class="bash">$ jt . % <<EOT
- {
- "foo": 100,
- "bar": 200,
- "baz": 300
- }
- EOT
100
200
300
</code></pre>
<p>When iterating over an object the <code>^</code> command prints the name of the current
property:</p>
<pre><code class="bash">$ jt . ^ % <<EOT
- {
- "foo": 100,
- "bar": 200,
- "baz": 300
- }
- EOT
foo 100
bar 200
baz 300
</code></pre>
<h3 id="JSON-Streams">JSON Streams</h3>
<p><strong>Jt</strong> automatically iterates over entities in a JSON stream (optionally
delimited by whitespace):</p>
<pre><code class="bash">$ jt [ foo % ] bar % <<EOT
- {"foo": 100, "bar": 200}
- {"foo": 200, "bar": 300}
- {"foo": 300, "bar": 400}
- EOT
100 200
200 300
300 400
</code></pre>
<p>The <code>^</code> command prints the current stream index:</p>
<pre><code class="bash">$ jt ^ [ foo % ] bar % <<EOT
- {"foo": 100, "bar": 200}
- {"foo": 200, "bar": 300}
- {"foo": 300, "bar": 400}
- EOT
0 100 200
1 200 300
2 300 400
</code></pre>
<h3 id="Nested-JSON">Nested JSON</h3>
<p>The <code>+</code> command parses JSON embedded in strings:</p>
<pre><code class="bash">$ jt [ foo + bar % ] baz % <<EOT
- {"foo":"{\"bar\":100}","baz":200}
- {"foo":"{\"bar\":200}","baz":300}
- {"foo":"{\"bar\":300}","baz":400}
- EOT
100 200
200 300
300 400
</code></pre>
<p><strong>Note:</strong> The <code>+</code> command does not modify the original JSON:</p>
<pre><code class="bash">$ jt [ foo + bar % ] % <<EOT
- {"foo":"{\"bar\":100}","baz":200}
- {"foo":"{\"bar\":200}","baz":300}
- {"foo":"{\"bar\":300}","baz":400}
- EOT
100 {"foo":"{\"bar\":100}","baz":200}
200 {"foo":"{\"bar\":200}","baz":300}
300 {"foo":"{\"bar\":300}","baz":400}
</code></pre>
<h3 id="Column-Headings">Column Headings</h3>
<p>The <code>%=</code><var>NAME</var> and <code>^=</code><var>NAME</var> commands are like <code>%</code> and <code>^</code> above, but they
also assign column headings, which are printed as the first line of output:</p>
<pre><code class="bash">$ jt [ foo %=Foo ] bar ^=Bar baz %=Baz <<EOT
- {
- "foo": 100,
- "bar": [
- {"baz": 200},
- {"baz": 300},
- {"baz": 400}
- ]
- }
- EOT
Foo Bar Baz
100 0 200
100 1 300
100 2 400
</code></pre>
<p><strong>Note:</strong> The heading <var>NAME</var> must be a JSON escaped string (eg. to have a
column heading with embedded tab characters use <code>%=foo\tbar</code> instead of
<code>"%=foo bar"</code>).</p>
<h3 id="Joins">Joins</h3>
<p>Notice the empty column — some objects don't have the <var>bar</var> key:</p>
<pre><code class="bash">$ jt [ foo % ] bar % <<EOT
- {"foo":100,"bar":1000}
- {"foo":200}
- {"foo":300,"bar":3000}
- EOT
100 1000
200
300 3000
</code></pre>
<p>Enable inner join mode with the <code>-j</code> flag. This removes output rows when a key
in the traversal path doesn't exist:</p>
<pre><code class="bash">$ jt -j [ foo % ] bar % <<EOT
- {"foo":100,"bar":1000}
- {"foo":200}
- {"foo":300,"bar":3000}
- EOT
100 1000
300 3000
</code></pre>
<p>Note that this does not remove rows when the key exists and the value is empty:</p>
<pre><code class="bash">$ jt -j [ foo % ] bar % <<EOT
- {"foo":100,"bar":1000}
- {"foo":200,"bar":""}
- {"foo":300,"bar":3000}
- EOT
100 1000
200
300 3000
</code></pre>
<h3 id="Explicit-Iteration">Explicit Iteration</h3>
<p>Sometimes the implicit iteration over arrays is awkward:</p>
<pre><code class="bash">$ jt . ^ . ^ % <<EOT
- {
- "foo": [
- {"bar":100},
- {"bar":200}
- ]
- }
- EOT
0 bar 100
1 bar 200
</code></pre>
<p>Should the first <code>^</code> be printing the array index of the implicit iteration
(which it does, in this case) or the object key (i.e. <var>foo</var>) of the explicit
iteration of the <code>.</code> command?</p>
<p>Another awkward case is printing arrays:</p>
<pre><code class="bash">$ jt foo % <<EOT
- {
- "foo": [
- {"bar":100},
- {"bar":200}
- ]
- }
- EOT
{"bar":100}
{"bar":200}
</code></pre>
<p>The array can not be printed with the <code>%</code> command because it is being iterated
over implicitly. Instead, the items in the array are printed, which may not be
the desired behavior.</p>
<p>The <code>-a</code> flag eliminates the ambiguity by enabling explicit iteration. In this
mode the <code>.</code> command must be used to iterate over both objects and arrays —
arrays are not automatically iterated over.</p>
<p>Now the array can be printed:</p>
<pre><code class="bash">$ jt -a foo % <<EOT
- {
- "foo": [
- {"bar":100},
- {"bar":200}
- ]
- }
- EOT
[{"bar":100},{"bar":200}]
</code></pre>
<p>Or the first <code>^</code> can print the array index, as before:</p>
<pre><code class="bash">$ jt -a . . ^ . ^ % <<EOT
- {
- "foo": [
- {"bar":100},
- {"bar":200}
- ]
- }
- EOT
0 bar 100
1 bar 200
</code></pre>
<p>Or it can print the object key:</p>
<pre><code class="bash">$ jt -a . ^ . . ^ % <<EOT
- {
- "foo": [
- {"bar":100},
- {"bar":200}
- ]
- }
- EOT
foo bar 100
foo bar 200
</code></pre>
<p>Or, with the addition of one more <code>^</code> command, it can print both:</p>
<pre><code class="bash">$ jt -a . ^ . ^ . ^ % <<EOT
- {
- "foo": [
- {"bar":100},
- {"bar":200}
- ]
- }
- EOT
foo 0 bar 100
foo 1 bar 200
</code></pre>
<h2 id="SEE-ALSO">SEE ALSO</h2>
<p><strong>Jt</strong> is based on ideas from the excellent <strong>jshon</strong> tool, which can be found
at <a href="http://kmkeen.com/jshon/" data-bare-link="true">http://kmkeen.com/jshon/</a>. Source code for <strong>jt</strong> is available at
<a href="https://github.com/micha/json-table" data-bare-link="true">https://github.com/micha/json-table</a>.</p>
<h2 id="BUGS">BUGS</h2>
<p>Please open an issue here: <a href="https://github.com/micha/json-table/issues" data-bare-link="true">https://github.com/micha/json-table/issues</a>.</p>
<h2 id="COPYRIGHT">COPYRIGHT</h2>
<p>Copyright © 2017 Micha Niskin <code><micha.niskin@gmail.com></code>, distributed under
the Eclipse Public License, version 1.0. This is free software: you are free
to change and redistribute it. There is NO WARRANTY, to the extent permitted
by law.</p>
<ol class='man-decor man-foot man foot'>
<li class='tl'></li>
<li class='tc'>July 2017</li>
<li class='tr'>jt(1)</li>
</ol>
</div>
</body>
</html>