8.18
top
← prev up next →

Pretty Printing JSONπŸ”— i

Want JSON that’s easier for humans to read instead of the compact form produced by write-json and jsexpr->string ? Look no further!

1Available formattersπŸ”— i

1.1Simple formattingπŸ”— i

Pure racket, very similar to JQ output (Except for adding a space before colons in objects).

procedure

( jsexpr->pretty-json js)string?

js:jsexpr?
Returns the given jsexpr as a pretty-printed string.

Returns the given jsexpr as a pretty-printed byte string.

procedure

( format-json json)string?

json:string?
Formats the given JSON value and returns it.

procedure

( format-json/bytes json)bytes?

json:bytes?
Formats the given JSON value and returns it.

Writes out the formatted jsexpr as JSON to the given port.

procedure

( pretty-print-json json[out])void?

json:string?
Writes out the formatted JSON value to the given port.

Writes out the formatted JSON value to the given port.

1.2Smart formattingπŸ”— i

Tries to use less vertical space by printing small arrays and objects on a single line. Experimental and not very well tested.

parameter

( pretty-print-json-line-width)exact-positive-integer?

(pretty-print-json-line-widthwidth)void?
= 80
The number of columns to use when deciding if an aggregate value can be printed on one line.

parameter

( pretty-print-json-tab-width)exact-positive-integer?

(pretty-print-json-tab-widthwidth)void?
= 8
How many columns a tab takes up when calculating width.

procedure

( jsexpr->pretty-json js)string?

js:jsexpr?
Returns the given jsexpr as a pretty-printed string.

Returns the given jsexpr as a pretty-printed bytestring.

procedure

( format-json json)string?

json:string?
Formats the given JSON value and returns it.

procedure

( format-json/bytes json)bytes?

json:bytes?
Formats the given JSON value and returns it.

Writes out the formatted jsexpr as JSON to the given port.

procedure

( pretty-print-json json[out])void?

json:string?
Writes out the formatted JSON value to the given port.

Writes out the formatted JSON value to the given port.

1.3JQ-powered formattingπŸ”— i

Uses an external jq process to format JSON. Also allows you to run arbitrary jsexprs through JQ.

parameter

( jq-path )(or/cpath-string? #f)

(jq-path path)void?
path:(or/cpath-string? #f)
The path to the jq executable to use.

procedure

( jsexpr-transform jsfilter)jsexpr?

js:jsexpr?
filter:string?
Return the result of running the JSON represented by js through the given jq filter program.

Technically not formatting, but it uses the same framework, so why not include it?

procedure

( jsexpr->pretty-json js)string?

js:jsexpr?
Returns the given jsexpr as a pretty-printed string.

Returns the given jsexpr as a pretty-printed bytestring.

procedure

( format-json json)string?

json:string?
Formats the given JSON value and returns it.

procedure

( format-json/bytes json)bytes?

json:bytes?
Formats the given JSON value and returns it.

Writes out the formatted jsexpr as JSON to the given port.

procedure

( pretty-print-json json[out])void?

json:string?
Writes out the formatted JSON value to the given port.

Writes out the formatted JSON value to the given port.

2Controlling formatting styleπŸ”— i

All of the above formatter modules use these parameters unless otherwise noted.

parameter

( pretty-print-json-ascii-only)boolean?

(pretty-print-json-ascii-onlyascii)void?
ascii:any/c
= #f
If true, any non-ASCII codepoints in strings are escaped as \uXXXX sequences.

parameter

( pretty-print-json-sort-keys)boolean?

(pretty-print-json-sort-keyssort-keys)void?
sort-keys:any/c
= #f
If true, objects are printed with keys in sorted order.

parameter

( pretty-print-json-indent)

(pretty-print-json-indentwidth)void?
width:(or/cexact-nonnegative-integer? 'tabs)
= 2
Controls how many spaces to insert for each level of indentation (Or one tab per level if ' tabs).

parameter

( pretty-print-json-colorize)(or/cboolean? 'terminal)

(pretty-print-json-colorizewhen)void?
when:(or/cboolean? 'terminal)
= 'terminal
Controls when to colorize the output - when the output port is a terminal, always if otherwise true, or never if false.

Colors are customized as in the same manner as jq.

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /