How-to guide

How to Format JSON

To format JSON, paste it into a JSON formatter and choose an indentation width. The formatter parses the document and rewrites it with consistent line breaks and nesting. If it refuses, the JSON is invalid — most often a trailing comma, a comment, single quotes instead of double, or an unquoted key. Fix the reported position and format again.

JSON Formatter

Beautify, validate and sort JSON with precise error messages.

Open the tool

Formatting JSON is only interesting when it fails. Valid JSON reformats without incident; the work is in the half of cases where the payload came from a config file someone hand-edited, a log line that was truncated, or a JavaScript object literal that was never valid JSON to begin with.

A formatter has to parse a document before it can re-indent it, so a formatter is also a validator. When it reports an error position, that position is where parsing became impossible — which is often slightly after the actual mistake.

Step by step

  1. Paste the JSON

    Drop the whole document into the input. Do not trim it to the part you think is broken — the parser needs the full structure to report an accurate position.

  2. Choose an indentation width

    Two spaces is the common default and keeps deeply nested payloads narrow. Four is easier to scan for shallow documents. Tabs are available where a project standard requires them.

  3. Read the error if it fails

    An invalid document reports a line and column. Look at that position and the few characters before it: the parser stops where the document became impossible, which is usually just past the real mistake.

  4. Fix the four usual causes

    Trailing comma after the last item; // or /* */ comments; single quotes instead of double; an unquoted key. None of these are legal JSON, though all four are legal JavaScript, which is why they appear so often.

  5. Sort the keys if you are comparing

    Turn on key sorting when you intend to diff two payloads. Sorting both makes the diff show real value changes rather than incidental ordering differences.

  6. Copy or download the result

    Copy the formatted output back into your editor, or download it as a .json file if it is large enough that clipboard handling becomes awkward.

Example

A minified payload and the same document formatted with two-space indentation.

Minified

{"id":42,"name":"Ada","roles":["admin","dev"],"active":true}

Formatted

{
  "id": 42,
  "name": "Ada",
  "roles": [
    "admin",
    "dev"
  ],
  "active": true
}

Frequently asked questions

Why does my JSON fail when it looks fine?
Almost always one of four things that are valid JavaScript but not valid JSON: a trailing comma after the last element, a // or /* */ comment, single quotes around a string, or an unquoted object key. A JSON parser rejects all four.
Does formatting change the data?
No. Formatting only changes whitespace between tokens. Values, key order (unless you explicitly sort) and types come through unchanged, so the parsed result is identical before and after.
Is my JSON uploaded to a server?
No. The formatter parses in your browser, which is why it is safe to paste a production API response or a config file containing internal hostnames.
What indentation should I use?
Two spaces is the most common convention and keeps deep nesting readable at normal editor widths. Four spaces reads better for shallow documents. The choice has no effect on validity.
Can I format a very large file?
Files up to roughly 10 MB format comfortably. Larger ones still work but the tab may pause while parsing, because the whole document is held in memory at once.

Tools used in this guide

All tools →
All guides →

Last reviewed .