Pretty Printing JSON
1 Available formatters
1.1 Simple formatting
jsexpr->pretty-json
jsexpr->pretty-json/  bytes
format-json
format-json/  bytes
pretty-print-jsexpr
pretty-print-json
pretty-print-json/  bytes
1.2 Smart formatting
pretty-print-json-line-width
pretty-print-json-tab-width
jsexpr->pretty-json
jsexpr->pretty-json/  bytes
format-json
format-json/  bytes
pretty-print-jsexpr
pretty-print-json
pretty-print-json/  bytes
1.3 JQ-powered formatting
jq-path
jsexpr-transform
jsexpr->pretty-json
jsexpr->pretty-json/  bytes
format-json
format-json/  bytes
pretty-print-jsexpr
pretty-print-json
pretty-print-json/  bytes
2 Controlling formatting style
pretty-print-json-ascii-only
pretty-print-json-sort-keys
pretty-print-json-indent
pretty-print-json-colorize
8.12

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

    2 Controlling formatting style

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?
Returns the given jsexpr as a pretty-printed string.

procedure

(jsexpr->pretty-json/bytes js)  bytes?

  js : jsexpr?
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.

procedure

(pretty-print-jsexpr js [out])  void?

  js : jsexpr?
  out : output-port? = (current-output-port)
Writes out the formatted jsexpr as JSON to the given port.

procedure

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

  json : string?
  out : output-port? = (current-output-port)
Writes out the formatted JSON value to the given port.

procedure

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

  json : bytes?
  out : output-port? = (current-output-port)
Writes out the formatted JSON value to the given 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
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-width width)  void?
  width : exact-positive-integer?
 = 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.

procedure

(jsexpr->pretty-json/bytes js)  bytes?

  js : jsexpr?
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.

procedure

(pretty-print-jsexpr js [out])  void?

  js : jsexpr?
  out : output-port? = (current-output-port)
Writes out the formatted jsexpr as JSON to the given port.

procedure

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

  json : string?
  out : output-port? = (current-output-port)
Writes out the formatted JSON value to the given port.

procedure

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

  json : bytes?
  out : output-port? = (current-output-port)
Writes out the formatted JSON value to the given 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")
The path to the jq executable to use.

procedure

(jsexpr-transform js filter)  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.

procedure

(jsexpr->pretty-json/bytes js)  bytes?

  js : jsexpr?
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.

procedure

(pretty-print-jsexpr js [out])  void?

  js : jsexpr?
  out : output-port? = (current-output-port)
Writes out the formatted jsexpr as JSON to the given port.

procedure

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

  json : string?
  out : output-port? = (current-output-port)
Writes out the formatted JSON value to the given port.

procedure

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

  json : bytes?
  out : output-port? = (current-output-port)
Writes out the formatted JSON value to the given 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
If true, any non-ASCII codepoints in strings are escaped as \uXXXX sequences.

parameter

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

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

parameter

(pretty-print-json-indent)

  (or/c exact-nonnegative-integer? 'tabs)
(pretty-print-json-indent width)  void?
  width : (or/c exact-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/c boolean? 'terminal)

(pretty-print-json-colorize when)  void?
  when : (or/c boolean? '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.