How to Format JSON Properly

JSON formatting (also called pretty-printing or beautifying) parses a compact JSON string and re-serializes it with consistent indentation — typically 2 or 4 spaces. The data is identical; only the whitespace changes. Use JSON.stringify(data, null, 2) in JavaScript, json.dumps(data, indent=2) in Python, or paste your JSON into the JSON Formatter above for instant results.

Try it directly in your browser:

Format your JSON →

What JSON formatting actually does

Formatting (also called pretty-printing or beautifying) parses your JSON and re-prints it with consistent indentation. It does not change your data — the JSON format is defined in RFC 8259 and whitespace outside of strings is explicitly insignificant. The key below is identical to the minified version — only the whitespace is different:

// Minified (machine output)
{"user":{"name":"Jane","age":28,"roles":["admin","editor"]},"active":true}

// Formatted (human readable)
{
  "user": {
    "name": "Jane",
    "age": 28,
    "roles": [
      "admin",
      "editor"
    ]
  },
  "active": true
}

The four core formatting rules

1. Always use double quotes

Keys and string values in JSON must use double quotes. Single quotes are invalid and will cause a parse error in every JSON parser.

// Valid
{ "name": "Alice" }

// Invalid — single quotes are not allowed
{ 'name': 'Alice' }

2. Separate properties with commas

Every property in an object and every element in an array must be separated by a comma — except the last one. A trailing comma after the final item is not allowed in standard JSON.

// Valid
{
  "first": "Alice",
  "last": "Smith"
}

// Invalid — trailing comma after last property
{
  "first": "Alice",
  "last": "Smith",
}

3. Use consistent indentation

Two spaces is the most common standard. Four spaces is also widely used. What matters is consistency — pick one and stick to it throughout the file. Mixing two and four spaces in the same file is a common mistake when editing JSON manually.

4. Keep nesting clear

Each level of nesting should increase the indentation by one unit. Opening braces { and brackets [ go on the same line as the key. Closing braces and brackets go on their own line, aligned with the key that opened them.

{
  "server": {
    "host": "localhost",
    "port": 8080,
    "options": {
      "timeout": 30,
      "retries": 3
    }
  }
}

Common formatting mistakes to avoid

When to format vs when to minify

Format JSON when you are reading, debugging, or editing it. Minify JSON when you are sending it over a network or storing it in production where file size matters. You can always format minified JSON again — the data is identical.

A good workflow: validate first to catch errors, then format for review, then minify before deployment.

What is the difference between JSON formatting and JSON validation?

JSON formatting changes only whitespace — indentation and line breaks — to make the document human-readable. The data, structure, keys, and values remain exactly the same. JSON validation checks whether the document follows correct JSON syntax: properly quoted keys, no trailing commas, no comments, and matching brackets. A formatter will fail silently or produce garbled output on invalid JSON; a validator will report the exact error and line number. The recommended workflow is to validate first, then format.

Frequently Asked Questions

Why does my JSON look inconsistently indented after manual edits?

Two spaces is the most widely used standard. Four spaces is also common. The important thing is to be consistent throughout the file.

Can I format minified JSON?

Yes. Paste any minified JSON into a formatter and click Format. The tool parses the JSON and re-prints it with proper indentation and line breaks.

Does formatting JSON change the data?

No. Formatting only changes whitespace. The actual keys, values, and structure are identical before and after formatting.

Format JSON instantly

Paste any JSON — minified, broken, or API response — and get clean, readable output in one click. No signup, works in your browser.

Open JSON Formatter

Ready to format your json?

Open JSON Formatter →
About the author

Pasindu Ishan is a software developer based in Sri Lanka. He builds developer tools at JSON Dev Tools.