Pretty Printing JSON
Want JSON that’s easier for humans to read instead of the compact form produced by write-json and jsexpr->string? Look no further!
1 Available formatters
1.1 Simple formatting
Pure racket, very similar to JQ output (Except for adding a space before colons in objects).
(require json/format/simple) | package: json-format |
procedure
(jsexpr->pretty-json js) → string?
js : jsexpr?
procedure
(jsexpr->pretty-json/bytes js) → bytes?
js : jsexpr?
procedure
(format-json json) → string?
json : string?
procedure
(format-json/bytes json) → bytes?
json : bytes?
procedure
(pretty-print-jsexpr js [out]) → void?
js : jsexpr? out : output-port? = (current-output-port)
procedure
(pretty-print-json json [out]) → void?
json : string? out : output-port? = (current-output-port)
procedure
(pretty-print-json/bytes json [out]) → void?
json : bytes? out : output-port? = (current-output-port)
1.2 Smart formatting
Tries to use less vertical space by printing small arrays and objects on a single line. Experimental and not very well tested.
(require json/format/smart) | package: json-format |
parameter
(pretty-print-json-line-width) → exact-positive-integer?
(pretty-print-json-line-width width) → void? width : exact-positive-integer?
= 80
parameter
(pretty-print-json-tab-width) → exact-positive-integer?
(pretty-print-json-tab-width width) → void? width : exact-positive-integer?
= 8
procedure
(jsexpr->pretty-json js) → string?
js : jsexpr?
procedure
(jsexpr->pretty-json/bytes js) → bytes?
js : jsexpr?
procedure
(format-json json) → string?
json : string?
procedure
(format-json/bytes json) → bytes?
json : bytes?
procedure
(pretty-print-jsexpr js [out]) → void?
js : jsexpr? out : output-port? = (current-output-port)
procedure
(pretty-print-json json [out]) → void?
json : string? out : output-port? = (current-output-port)
procedure
(pretty-print-json/bytes json [out]) → void?
json : bytes? out : output-port? = (current-output-port)
1.3 JQ-powered formatting
Uses an external jq process to format JSON. Also allows you to run arbitrary jsexprs through JQ.
(require json/format/jq) | package: json-format |
parameter
(jq-path) → (or/c path-string? #f)
(jq-path path) → void? path : (or/c path-string? #f)
= (find-executable-path "jq")
procedure
(jsexpr-transform js filter) → jsexpr?
js : jsexpr? filter : string?
Technically not formatting, but it uses the same framework, so why not include it?
procedure
(jsexpr->pretty-json js) → string?
js : jsexpr?
procedure
(jsexpr->pretty-json/bytes js) → bytes?
js : jsexpr?
procedure
(format-json json) → string?
json : string?
procedure
(format-json/bytes json) → bytes?
json : bytes?
procedure
(pretty-print-jsexpr js [out]) → void?
js : jsexpr? out : output-port? = (current-output-port)
procedure
(pretty-print-json json [out]) → void?
json : string? out : output-port? = (current-output-port)
procedure
(pretty-print-json/bytes json [out]) → void?
json : bytes? out : output-port? = (current-output-port)
2 Controlling formatting style
All of the above formatter modules use these parameters unless otherwise noted.
(require json/format/config) | package: json-format |
parameter
(pretty-print-json-ascii-only) → boolean?
(pretty-print-json-ascii-only ascii) → void? ascii : any/c
= #f
parameter
(pretty-print-json-sort-keys) → boolean?
(pretty-print-json-sort-keys sort-keys) → void? sort-keys : any/c
= #f
parameter
→ (or/c exact-nonnegative-integer? 'tabs) (pretty-print-json-indent width) → void? width : (or/c exact-nonnegative-integer? 'tabs)
= 2
parameter
(pretty-print-json-colorize) → (or/c boolean? 'terminal)
(pretty-print-json-colorize when) → void? when : (or/c boolean? 'terminal)
= 'terminal
Colors are customized as in the same manner as jq.