skip to content

What does the prettyPrint flag do, and what are the practical considerations for using it (output, performance, configurability)?

level: juniorimportance: should knowfreq 45%

answer

  1. prettyPrint = indented multi-line output
  2. default false = compact one line
  3. prettyPrintIndent customizes indent (4 spaces default)
  4. affects encode only; decode ignores whitespace
  5. use for logs/debug, compact for wire

basics

~10 s

prettyPrint = true makes the JSON output human-readable with indentation and line breaks instead of one compact line. It's for logs and debugging; compact output is better for the wire.

solid answer

~30 s

prettyPrint (default false) controls formatting. When false, encodeToString emits compact single-line JSON. When true, output is multi-line with indentation, which aids readability in logs, files, and debugging. The indent string is configurable via prettyPrintIndent (default four spaces). Pretty printing produces larger payloads and slightly more work, so prefer compact JSON for network/storage and reserve prettyPrint for human-facing output. It only affects encoding; decoding ignores whitespace either way. Like all builder settings, create one Json { prettyPrint = true } instance and reuse it rather than rebuilding per call.

go deeper

for a junior

Knows prettyPrint produces readable, indented output and is off by default.

for a middle

Mentions prettyPrintIndent, that decode ignores whitespace, and compact-for-wire guidance.

for a senior

Notes payload/perf cost, reuse of the instance, and that it's purely cosmetic.

for a principal

Frames it as an output-channel concern (human vs machine) and avoids it on hot serialization paths.

## What prettyPrint does `prettyPrint` is a `Json { }` builder flag (default `false`) that controls only the **encoding** format. - `false`: compact, single-line output — `{"id":1,"name":"Ada"}`. - `true`: indented, multi-line output for human readability. ```kotlin val pretty = Json { prettyPrint = true } println(pretty.encodeToString(User(1, "Ada"))) // { // "id": 1, // "name": "Ada" // } ``` ## Configuring the indent The indentation string is controlled by `prettyPrintIndent` (default four spaces). You can change it, e.g. `Json { prettyPrint = true; prettyPrintIndent = " " }` for two-space indent. Setting `prettyPrintIndent` without `prettyPrint = true` has no effect. ## Practical considerations - **Decoding is unaffected:** the parser ignores insignificant whitespace, so any Json instance reads both compact and pretty input. - **Payload size & cost:** pretty output is larger and marginally slower to produce; use compact JSON for the wire and storage. - **Use cases:** logs, config files, fixtures, and debug dumps where a human reads the output. - **Reuse the instance:** as with every builder option, build the pretty `Json` once and reuse it. prettyPrint is purely cosmetic — it never changes which keys/values appear, only their layout.

  • Does prettyPrint affect decoding or which fields appear?
    No. It only changes encoding layout; the parser ignores whitespace and the set of keys/values is unchanged.
  • How do you get a two-space indent?
    Set prettyPrintIndent = " " together with prettyPrint = true.

Compact JSON is a packed suitcase; prettyPrint unfolds everything neatly so a human can see each item.

saying these in an interview costs you the question

  • Thinking prettyPrint changes which fields are serialized
  • Using prettyPrint for network payloads by default
  • Believing it affects decoding
  • Setting prettyPrintIndent and expecting it to work without prettyPrint = true

context