Raw JSON is hard to read. Here's how to format it safely.

When JSON is crushed into one line or pasted from an API response, the first goal is readability. A formatter fixes the whitespace without changing the data, but only after the payload is valid.

4 min readFormatter / Validator
TL;DRRun valid JSON through a formatter to add clean indentation and line breaks. If the formatter refuses to run, validate the syntax first because formatting cannot repair broken JSON.

Start with a safe formatting workflow

  1. 1Paste the JSON into a formatter.
  2. 2Choose 2 spaces for common web output or 4 spaces for deeper nested structures.
  3. 3Run the format action and review the result.
  4. 4Copy the output back into your codebase, docs, or debugging workflow.

Before and after formatting

The important part is this: the data does not change. Only the whitespace changes, which makes the structure much easier to scan and review.

Before: valid but compressed JSON

This is valid JSON. It is just hard to read quickly.

After: the same data, formatted for humans

This is the same payload, only with indentation and line breaks added.

Choose 2 spaces or 4 spaces based on where the JSON is going

  • Use 2 spaces when the JSON is headed back into web code, frontend configs, or API examples where compact readability matters.
  • Use 4 spaces when the structure is deeply nested and you want each level to stand out more clearly.
  • Match the indentation style your team already uses if the JSON will live in a shared codebase or docs repo.

What to do when the formatter fails

A formatter can only beautify valid JSON. If it throws an error, the issue is not the whitespace. The issue is the syntax.

That usually means a trailing comma, missing quote, missing colon, or a copied JavaScript object literal that never counted as JSON in the first place.

Do not try to format your way out of invalid JSON. Switch to the validator, read the parser message, fix the broken syntax, then come back to the formatter once the payload passes.

Common mistakes before formatting

  • Using single quotes instead of double quotes for keys or string values.
  • Leaving a trailing comma after the last array item or object property.
  • Copying JavaScript object literals and assuming they are valid JSON.

Quick answers

What to do next

Start with the right tool now, then move back to the hub or sideways into the adjacent guides if the first answer was not the whole fix.