FORMATTING

How to Format JSON for Readability

Minified JSON can be difficult to inspect, while deeply indented JSON can be inconvenient to transmit or store. Formatting changes whitespace, not the represented values, so choose the presentation that fits your next task.

Beginner5 min read

What formatting JSON means

Formatting (also called pretty-printing or beautifying) adds line breaks and indentation around JSON objects and arrays. A parser ignores this insignificant whitespace outside strings, so formatted and minified versions can represent the same JSON value.

Minified versus formatted

Here is one JSON object written compactly. It is valid but harder to scan:

Minified JSON
{"service":"catalog","enabled":true,"limits":{"pageSize":25,"retries":3},"regions":["north","west"]}

With indentation, the relationships are easier to see:

Formatted JSON with two-space indentation
{
  "service": "catalog",
  "enabled": true,
  "limits": {
    "pageSize": 25,
    "retries": 3
  },
  "regions": [
    "north",
    "west"
  ]
}

Both examples contain the same keys, values, and array order. The formatted version uses whitespace to make object boundaries and nesting visible.

Choosing indentation

Two spaces, four spaces, and tabs are common indentation styles. JSON syntax does not require a specific one; teams often select a style in their repository conventions. Consistency is more important than one universally correct width. For long arrays or nested values, line breaks can make individual elements easier to review.

When to format JSON

  • Inspect an API response or configuration file.
  • Review changes in version control or debug a deeply nested object.
  • Prepare a JSON example for documentation or a code review.
  • Check whether values are placed under the intended keys.

Use the JSON Formatter to select indentation and format valid input. Formatting does not repair malformed JSON; use the syntax guide or JSON Validator if parsing fails.

When to minify JSON

Minification removes unnecessary whitespace and line breaks. It can reduce the text size of a JSON payload or create a compact representation for a system that expects one. It does not compress or encrypt the data, and it does not change the values.

Do not minify a file you need people to review or edit by hand. Keep an original or use version control before replacing readable source data. The JSON Minifier creates a compact output from valid JSON.

Important: A formatter changes whitespace, not the data model. Reordering object keys may change the textual output, although JSON object order is not generally meaningful to consumers. Array order, however, is meaningful and must be preserved.

A practical workflow

  1. Start with the source JSON and keep a copy if it is important.
  2. Format it with a consistent indentation style.
  3. Review the nesting and values; use validation if there is a syntax error.
  4. Minify only when compact text is useful, and retain readable source where people maintain it.

Key takeaways

  • Formatting adds whitespace to improve readability; minifying removes it.
  • Whitespace outside strings does not change the JSON value.
  • Formatting requires valid JSON and is not a repair operation.
  • Array order is data, not formatting, and should not be changed.