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.
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
-
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.
-
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.
-
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.
-
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.
-
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.
-
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?
Does formatting change the data?
Is my JSON uploaded to a server?
What indentation should I use?
Can I format a very large file?
Tools used in this guide
All tools →- JSON Formatter Beautify, validate and sort JSON with precise error messages. In your browser
- JSON Validator Check whether JSON is valid and see exactly where it breaks. In your browser
- JSON Minifier Strip whitespace from JSON and see the size saved. In your browser
- JSON Path Extractor Pull specific values out of a JSON document by path. In your browser
Related guides
All guides →- JSON Validator vs JSON Formatter Both parse your JSON. They differ in what they give you back and which one to reach for.
- JSON vs YAML Which to use for configuration and which for data interchange, and the YAML gotchas worth knowing.
- How to Convert XML to JSON Turn an XML document or feed into JSON, and handle the attributes and repeated elements that make it lossy.
Last reviewed .