Open Tools LibraryOpen Tools Library
Developer ToolsJSONDeveloperDebugging6 min read·October 24, 2026

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.

Dhaval Khatri

By Dhaval Khatri

Published October 24, 2026

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.

Three ways to view the same JSON
ViewWhat it isUse it when
PrettyIndented, one item per lineYou want to read or review a payload
MinifiedAll unnecessary whitespace removedYou are sending or storing it and size matters
TreeA collapsible, clickable structureThe 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.

Valid and invalid JSON, according to RFC 8259
RuleValidInvalid
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 commentsData only// note, or /* note */
Numbers have no leading zero or plus sign0.5, -3, 1e5.5, 05, +1
No NaN, Infinity or hex numbersnull for a missing numberNaN, Infinity, 0x1F
Literals are lowercasetrue, false, nullTrue, 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.

FAQ

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.

About the author

Dhaval Khatri

Dhaval Khatri

Designer, Developer and Educator

Dhaval Khatri has spent more than 12 years in the IT industry, working in design and development. He is also a visiting professor at a university. He started Open Tools Library because everyday tasks should not cost you your privacy or your patience. Merging a PDF or resizing a photo should take a minute, with no sign-up, no waiting and no one else holding your files. Every tool here runs in your own browser, and that promise is the reason the site exists.