113113
114114## Displaying JSON
115115### 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+
118120```bash
119121bash $ <ab.json jtc
120122{
@@ -194,7 +196,9 @@ bash $ <ab.json jtc
194196}
195197bash $
196198```
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+
198202```bash
199203bash $ <ab.json jtc -t10
200204{
@@ -212,29 +216,34 @@ bash $ <ab.json jtc -t10
212216 ],
213217...
214218```
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.
216220
217221### 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+
219225```bash
220226bash $ <ab.json jtc -r
221227{ "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" } ] }
222228bash $
223229```
224230
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+
227234```bash
228235bash $ <ab.json jtc -rt0
229236{"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 $
231238```
232239
233-
234240### 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+
238247```bash
239248bash $ <ab.json jtc -tc
240249{
@@ -274,54 +283,66 @@ bash $ <ab.json jtc -tc
274283 }
275284 ]
276285}
277- bash $
286+ bash $
278287```
279288
280-
281289### 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+
284295```bash
285296bash $ <ab.json jtc -rz
286297{ "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" } ] }
287298{ "size": 56 }
288- bash $
299+ bash $
289300```
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+
291303```bash
292304bash $ <ab.json jtc -zz
29330556
294- bash $
306+ bash $
295307```
296308
297-
298309### 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
303314jtc json parsing exception (<stdin>:1214): unexpected_end_of_line
304- bash $
315+ bash $
305316```
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+
308321```bash
309322bash $ <ab.json jtc -d
310323.display_opts(), option set[0]: -d (internally imposed: )
311324.init_inputs(), reading json from <stdin>
312325.exception_locus_(), ...e": 80206,| "state": "CO,| "street address": "6213...
313326.exception_spot_(), --------------------------------------->| (offset: 1214)
314327jtc json parsing exception (<stdin>:1214): unexpected_end_of_line
315- bash $
328+ bash $
316329```
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.
321340
322341### 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+
325346```bash
326347bash $ <<<'{ "escaped": "\/", "unescaped": "/" }' jtc
327348{
@@ -330,8 +351,9 @@ bash $ <<<'{ "escaped": "\/", "unescaped": "/" }' jtc
330351}
331352bash $
332353```
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+
335357```bash
336358bash $ <<<'{ "escaped": "\/", "unescaped": "/" }' jtc -q -d
337359.display_opts(), option set[0]: -q -d (internally imposed: )
@@ -343,8 +365,10 @@ bash $
343365```
344366
345367### 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+
348372```bash
349373bash $ jsn='"{ \"JSON\": \"example of an embedded JSON\" }"'
350374bash $ <<<$jsn jtc
@@ -362,16 +386,18 @@ bash $ <<<$jsn jtc -qq | jtc
362386bash $
363387```
364388
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+
366391```bash
367392bash $ <<<'[null, "", true]' jtc -w[:] -qq
368393null
369394true
370395bash $
371396```
372397
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+
375401```bash
376402bash $ <<<'"Unicode char: \u1234"' jtc -qq
377403Unicode char: ሴ
@@ -384,7 +410,6 @@ jtc json exception: invalid_surrogate_code_pair
384410bash $
385411```
386412
387-
388413> NOTE: _the option notation `-qq` will not engulf a single option notation `-q`, if both behaviors are required then both variants have
389414to be spelled (e.g. `jtc -q -qq`, or `jtc -qqq`)_
390415> 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