113
113
114
114
## Displaying JSON
115
115
### Pretty printing
116
- If no argument given, `jtc` will expect an input JSON from the `<stdin>`, otherwise JSON is read from the file(s) pointed by the
117
- argument(s). `jtc` will parse and validate input JSON and upon a successful validation will output:
116
+ If no argument is given, `jtc` will read JSON from `<stdin>`, otherwise JSON
117
+ will be read from the files specified. `jtc` will parse and validate input JSON
118
+ and upon successful validation the JSON will be output:
119
+
118
120
```bash
119
121
bash $ <ab.json jtc
120
122
{
@@ -194,7 +196,9 @@ bash $ <ab.json jtc
194
196
}
195
197
bash $
196
198
```
197
- option `-t` controls the indentation of the pretty-printing format (default is 3 white spaces):
199
+
200
+ option `-t` specifies pretty-printing spacing (default - 3 spaces):
201
+
198
202
```bash
199
203
bash $ <ab.json jtc -t10
200
204
{
@@ -212,29 +216,34 @@ bash $ <ab.json jtc -t10
212
216
],
213
217
...
214
218
```
215
- Majority of the examples and explanations in this document are based on the above simplified version of the above address book JSON model .
219
+ The majority of the examples and explanations here use the above address book JSON.
216
220
217
221
### Compact printing
218
- Option `-r` will instruct to display JSON in a compact (single row) format:
222
+
223
+ Option `-r` will instruct to display JSON in compact (single row) format:
224
+
219
225
```bash
220
226
bash $ <ab.json jtc -r
221
227
{ "Directory": [ { "address": { "city": "New York", "postal code": 10012, "state": "NY", "street address": "599 Lafayette St" }, "age": 25, "children": [ "Olivia" ], "name": "John", "phone": [ { "number": "112-555-1234", "type": "mobile" }, { "number": "113-123-2368", "type": "mobile" } ], "spouse": "Martha" }, { "address": { "city": "Seattle", "postal code": 98104, "state": "WA", "street address": "5423 Madison St" }, "age": 31, "children": [], "name": "Ivan", "phone": [ { "number": "273-923-6483", "type": "home" }, { "number": "223-283-0372", "type": "mobile" } ], "spouse": null }, { "address": { "city": "Denver", "postal code": 80206, "state": "CO", "street address": "6213 E Colfax Ave" }, "age": 25, "children": [ "Robert", "Lila" ], "name": "Jane", "phone": [ { "number": "358-303-0373", "type": "office" }, { "number": "333-638-0238", "type": "home" } ], "spouse": "Chuck" } ] }
222
228
bash $
223
229
```
224
230
225
- By default, the compact printing view will use a single spacer between all tokens, that also could be controlled if `-r` and `-t`
226
- used together, e.g., to print the above JSON w/o spacer:
231
+ By default, compact printing view will use a single space between all tokens.
232
+ By combining `-r` and `-t` the results can be printed without spaces:
233
+
227
234
```bash
228
235
bash $ <ab.json jtc -rt0
229
236
{"Directory":[{"address":{"city":"New York","postal code":10012,"state":"NY","street address":"599 Lafayette St"},"age":25,"children":["Olivia"],"name":"John","phone":[{"number":"112-555-1234","type":"mobile"},{"number":"113-123-2368","type":"mobile"}],"spouse":"Martha"},{"address":{"city":"Seattle","postal code":98104,"state":"WA","street address":"5423 Madison St"},"age":31,"children":[],"name":"Ivan","phone":[{"number":"273-923-6483","type":"home"},{"number":"223-283-0372","type":"mobile"}],"spouse":null},{"address":{"city":"Denver","postal code":80206,"state":"CO","street address":"6213 E Colfax Ave"},"age":25,"children":["Robert","Lila"],"name":"Jane","phone":[{"number":"358-303-0373","type":"office"},{"number":"333-638-0238","type":"home"}],"spouse":"Chuck"}]}
230
- bash $
237
+ bash $
231
238
```
232
239
233
-
234
240
### Semi-compact printing
235
- A semi-compact view is a middle ground between pretty and compact views. The semi-compact view is engaged with the suffix -`c` appended
236
- to the indent value in `-t` option (e.g.: `-t5c`) . In the semi-compact view all _JSON iterables_ made of only _atomic values_ and/or
237
- empty iterables (`[]`, `{}`) will be printed in a single line, the rest if pretty-printed, compare:
241
+
242
+ Semi-compact output can be obtained by combining the `-c` and `-t` flags, for
243
+ example `-t5c`. All _JSON iterables_ made of only _atomic values_ or empty
244
+ iterables (`[]`, `{}`) will be printed in a single line, the rest will be
245
+ pretty-printed:
246
+
238
247
```bash
239
248
bash $ <ab.json jtc -tc
240
249
{
@@ -274,54 +283,66 @@ bash $ <ab.json jtc -tc
274
283
}
275
284
]
276
285
}
277
- bash $
286
+ bash $
278
287
```
279
288
280
-
281
289
### Printing JSON size
282
- JSON size is the total number of the JSON elements found within JSON, it could be printed using `-z`, the size appears after input JSON
283
- is printed (starting from version 1.75b the size is printed in a JSON format):
290
+
291
+ `-z` prints the total number of JSON elements found, the total will appear
292
+ after the input JSON has been printed. From version 1.75b onwards the size is
293
+ printed in JSON format.
294
+
284
295
```bash
285
296
bash $ <ab.json jtc -rz
286
297
{ "Directory": [ { "address": { "city": "New York", "postal code": 10012, "state": "NY", "street address": "599 Lafayette St" }, "age": 25, "children": [ "Olivia" ], "name": "John", "phone": [ { "number": "112-555-1234", "type": "mobile" }, { "number": "113-123-2368", "type": "mobile" } ], "spouse": "Martha" }, { "address": { "city": "Seattle", "postal code": 98104, "state": "WA", "street address": "5423 Madison St" }, "age": 31, "children": [], "name": "Ivan", "phone": [ { "number": "273-923-6483", "type": "home" }, { "number": "223-283-0372", "type": "mobile" } ], "spouse": null }, { "address": { "city": "Denver", "postal code": 80206, "state": "CO", "street address": "6213 E Colfax Ave" }, "age": 25, "children": [ "Robert", "Lila" ], "name": "Jane", "phone": [ { "number": "358-303-0373", "type": "office" }, { "number": "333-638-0238", "type": "home" } ], "spouse": "Chuck" } ] }
287
298
{ "size": 56 }
288
- bash $
299
+ bash $
289
300
```
290
- if size only required (i.e., w/o printing the input JSON), then use `-zz` option:
301
+ To print the total with printing the input JSON, use `-zz`:
302
+
291
303
```bash
292
304
bash $ <ab.json jtc -zz
293
305
56
294
- bash $
306
+ bash $
295
307
```
296
308
297
-
298
309
### Validating JSON
299
- When JSON is read (from a file, or from `stdin`), it get parsed and validated. If an invalid JSON is detected, a short exception
300
- message will be displayed, e.g,:
301
- ```bash
302
- bash $ <ab.json jtc
310
+
311
+ When JSON is read (from a file or `stdin`), it gets parsed and validated. If
312
+ invalid JSON is detected, a short exception message will be displayed:
313
+ ```bash bash $ <ab.json jtc
303
314
jtc json parsing exception (<stdin>:1214): unexpected_end_of_line
304
- bash $
315
+ bash $
305
316
```
306
- and though the message lets us knowing that there's a problem with the input JSON, it not very informative with regards whereabouts the
307
- the problem. To visualize the spot where the problem is, as well as its locus pass a single debug option (`-d`):
317
+
318
+ The error messsage doesn't explain where the problem is, the `-d` flag (single
319
+ debug) will help:
320
+
308
321
```bash
309
322
bash $ <ab.json jtc -d
310
323
.display_opts(), option set[0]: -d (internally imposed: )
311
324
.init_inputs(), reading json from <stdin>
312
325
.exception_locus_(), ...e": 80206,| "state": "CO,| "street address": "6213...
313
326
.exception_spot_(), --------------------------------------->| (offset: 1214)
314
327
jtc json parsing exception (<stdin>:1214): unexpected_end_of_line
315
- bash $
328
+ bash $
316
329
```
317
- the vertical pipe symbol `|` in the debug showing JSON locus replaces new lines, thus it becomes easy to spot the problem.
318
- The offset (`1214` in the example) is given in _unicode UTF-8_ characters from the beginning of the input/file/stream.
319
- In that particular failure instance, `jtc` found the end of a line, while _JSON string_ `"Co,` is still open (JSON standard does not
320
- permit multi-line strings). To fix that, the missing quotation mark to be added
330
+
331
+ The vertical pipe symbol `|` in the debug output shows the location
332
+ (`exception_locus .. offset:1214`) of the problem.
333
+
334
+ The offset (`1214` in the example) is given in _unicode UTF-8_ characters from
335
+ the beginning of the input/file/stream. In this example `jtc` found an
336
+ unexpected end of line in `"CO,` (the JSON standard doesn't permit multi-line
337
+ strings). To fix this, the missing quotation mark must be added.
338
+
339
+ Multiple debug flags (`-dd`, `-ddd`) can be used to gain greater insight.
321
340
322
341
### Forcing strict solidus parsing
323
- JSON specification allows escaping solidus (`/`) optionally. By default, `jtc` is relaxed w.r.t. parsing solidus notation - it admits
324
- both unescaped and escaped appearances:
342
+
343
+ The JSON specification allows optional escaping of solidus (`/`). By default,
344
+ `jtc` is looser and allows both unescaped and escaped input:
345
+
325
346
```bash
326
347
bash $ <<<'{ "escaped": "\/", "unescaped": "/" }' jtc
327
348
{
@@ -330,8 +351,9 @@ bash $ <<<'{ "escaped": "\/", "unescaped": "/" }' jtc
330
351
}
331
352
bash $
332
353
```
333
- If there's a need for a strict solidus parsing, option `-q` facilitates the need. It also will throw an exception upon facing
334
- a non-escaped notation:
354
+
355
+ The `-q` flag will enforce strict solidus parsing:
356
+
335
357
```bash
336
358
bash $ <<<'{ "escaped": "\/", "unescaped": "/" }' jtc -q -d
337
359
.display_opts(), option set[0]: -q -d (internally imposed: )
@@ -343,8 +365,10 @@ bash $
343
365
```
344
366
345
367
### Unquoting JSON strings
346
- If a JSON itself (or a result from walking JSON) is a single JSON string, then sometimes there's a need to unquote it
347
- (especially it comes handy if the string itself is an embedded JSON). `-qq` allows unquoting it, here are a few examples:
368
+
369
+ Sometimes JSON needs unquoting (for example with embedded JSON), the `-qq` flag
370
+ supports this:
371
+
348
372
```bash
349
373
bash $ jsn='"{ \"JSON\": \"example of an embedded JSON\" }"'
350
374
bash $ <<<$jsn jtc
@@ -362,16 +386,18 @@ bash $ <<<$jsn jtc -qq | jtc
362
386
bash $
363
387
```
364
388
365
- When unquoting empty _JSON strings_ (`""`) the resulted blank lines are not even printed:
389
+ When unquoting empty _JSON strings_ (`""`) the resulting blank lines aren't printed:
390
+
366
391
```bash
367
392
bash $ <<<'[null, "", true]' jtc -w[:] -qq
368
393
null
369
394
true
370
395
bash $
371
396
```
372
397
373
- If the source string contains Unicode code points, those will be correctly translated into
374
- respective UTF-8 characters:
398
+ If the source string contains Unicode code points, it will be correctly
399
+ translated to UTF-8 characters:
400
+
375
401
```bash
376
402
bash $ <<<'"Unicode char: \u1234"' jtc -qq
377
403
Unicode char: ሴ
@@ -384,7 +410,6 @@ jtc json exception: invalid_surrogate_code_pair
384
410
bash $
385
411
```
386
412
387
-
388
413
> NOTE: _the option notation `-qq` will not engulf a single option notation `-q`, if both behaviors are required then both variants have
389
414
to be spelled (e.g. `jtc -q -qq`, or `jtc -qqq`)_
390
415
> Also, `-qq` is incompatible with `-j`, `-J` options, because of a risk of forming an ill-formed JSON, thus, when sighted together
0 commit comments