How to Format JSON and Fix the Errors That Break It
Make unreadable JSON easy to read, find the exact line that breaks it, and learn the few mistakes behind nearly every JSON error, like trailing commas.
Key takeaways
- JSON is stricter than JavaScript: strings and keys use double quotes only, there are no comments, and there are no trailing commas.
- Most invalid JSON errors come from a short list: trailing commas, single quotes, unquoted keys, comments and a missing comma or bracket.
- Pretty-print JSON to read it, minify it to send it, and sort keys before you compare two payloads.
- A formatter shows where parsing failed, but it cannot know what you meant, so you still fix mistakes yourself.
- Types generated from one sample are a starting point. Check which fields might be optional or null.
Pretty, minified and tree: when to use each
JSON from an API usually arrives as one long line with no spaces. That is efficient for machines and miserable for people. A formatter changes only the whitespace, so the data stays exactly the same.
| View | What it is | Use it when |
|---|---|---|
| Pretty | Indented, one item per line | You want to read or review a payload |
| Minified | All unnecessary whitespace removed | You are sending or storing it and size matters |
| Tree | A collapsible, clickable structure | The data is deeply nested and scrolling is painful |
- Minifying loses no data, only whitespace, so switching back to pretty view restores a readable version instantly.
- Indent with 2 spaces to keep deep structures compact, or 4 spaces if you prefer more air.
The rules JSON actually enforces
JSON looks like a JavaScript object, which is why people write it by hand and get caught out. The standard that defines it, RFC 8259, is much stricter. These are the rules that cause most failures.
| Rule | Valid | Invalid |
|---|---|---|
| Strings use double quotes | "name": "Asha" | 'name': 'Asha' |
| Keys must be quoted strings | {"id": 1} | {id: 1} |
| No trailing comma | [1, 2, 3] | [1, 2, 3,] |
| No comments | Data only | // note, or /* note */ |
| Numbers have no leading zero or plus sign | 0.5, -3, 1e5 | .5, 05, +1 |
| No NaN, Infinity or hex numbers | null for a missing number | NaN, Infinity, 0x1F |
| Literals are lowercase | true, false, null | True, None, undefined |
- Some editors and tools, such as TypeScript configuration files, read a relaxed variant called JSONC that allows comments. That does not make the file valid JSON for other programs.
- A top-level value can be any JSON value, including a plain string or number, not only an object or array.
The mistakes behind most errors
If your JSON will not parse, it is almost always one of these.
- A trailing comma after the last item. JavaScript allows it, JSON does not.
- Single quotes around strings or keys.
- An unquoted key, such as id instead of the quoted version.
- A comment, left in from a config file or a note to yourself.
- A missing comma between two items, or a missing closing bracket or brace.
- JavaScript-only values: undefined, NaN or Infinity.
- A raw line break or tab inside a string. These control characters must be written as escapes such as \n.
- Text copied from a console, which shows JavaScript object notation and not real JSON.
Reading the error message
An error points to where the parser gave up, which is not always where you made the mistake. A missing comma is usually reported at the start of the next item. A missing closing bracket is usually reported at the very end of the file.
So when you see an error, look at the exact line and column the formatter reports, then look at the line just before it. The JSON Formatter also checks for the common mistakes above and names the likely cause, so you can fix it in seconds. It will not repair the file for you, because it cannot know what you meant.
Step by step with the JSON Formatter
Here is a normal session.
- Paste your JSON into the JSON Formatter, or type it in.
- If there is an error, read the line and column and the suggested cause, fix it, and watch the output update.
- Switch between Pretty, Minify and Tree depending on what you need.
- Sort keys alphabetically when you want to compare two versions. It only changes the display order and leaves your values alone.
- Copy the result, or generate a TypeScript interface from it if you need types.
Two quiet problems that formatters cannot see
Valid JSON can still cause trouble. Two issues catch people out in real projects.
The first is large numbers. JSON itself does not limit the size of a number, but JavaScript reads numbers as 64-bit floating point values, which can only represent whole numbers exactly up to 9,007,199,254,740,991. IDs larger than that can be silently rounded. If you handle very large IDs, send them as strings.
The second is duplicate keys. The standard says names within an object should be unique, but it does not say what a parser must do if they are not. Some keep the last value, some report an error, and some keep both. Do not rely on any one behavior.
Generating a TypeScript interface, and its limits
The formatter can turn a JSON sample into a best-effort TypeScript interface, including nested types. It is a useful starting point, because typing out a large response by hand is dull work.
Treat the result as a draft. The generator learns from one sample, so it cannot know that a field is sometimes missing, sometimes null, or sometimes a different type in other responses. Compare it with a few real responses and mark optional and nullable fields by hand.
Why privacy matters for JSON
JSON often carries things you should not paste into a random website: access tokens, customer records, internal IDs. The JSON Formatter parses and formats everything in your browser, so what you paste is not sent to a server.
Mentioned in this post
JSON Formatter
Format, minify, or explore JSON as a collapsible tree, with alphabetical key sorting, precise error locations, and a bonus TypeScript interface generator — all in your browser.
JSON Validator
Check whether JSON is valid with an exact line and column for any error and a plain-English explanation of the likely cause, plus optional validation against a JSON Schema.
Text Diff Checker
Compare two blocks of text and see exactly what changed, line by line or word by word, using a real longest-common-subsequence diff algorithm — with options to ignore case or whitespace, live stats, and a one-click copyable unified diff.
Frequently asked questions
Why does my JSON look fine but fail to parse?
Check the exact line the error reports, and the line before it. The usual causes are a trailing comma, single quotes, an unquoted key or a comment.
Can the formatter fix invalid JSON for me?
No. It tells you where the problem is and what commonly causes it, but it cannot know what you meant, so you make the fix.
Does minifying change my data?
No. It removes only whitespace. Switch back to Pretty view and you get a readable version again.
Does sorting keys change my JSON?
Only the order in which keys are displayed. The values are untouched, and the sorted view is meant for easier comparison.
Is JSON the same as a JavaScript object?
No. JSON is a text format with stricter rules: double quotes only, quoted keys, no comments, no trailing commas and no undefined.
Is there a size limit?
The tool sets none. Very large payloads take a little longer to display, especially in tree view.
Is my JSON uploaded anywhere?
No. Parsing, formatting and the tree view all run locally in your browser.
Sources and further reading
The facts in this guide come from these pages. They are outside sites, and some of them update as the story develops.
