How to Format, Validate and Fix JSON (With Common Errors Explained)

How to Format, Validate and Fix JSON (With Common Errors Explained)

APIs return JSON as one long line, config files break because of a single comma, and error messages like Unexpected token } in JSON at position 482 don't say much. Formatting makes JSON readable; validating tells you exactly what's wrong. Here's how to do both, and how to fix the errors you'll meet most often.

Format JSON in seconds

  1. Open the free JSON formatter.
  2. Paste your JSON.
  3. Click Format to add indentation and line breaks, Validate to check it, or Minify to compress it to one line.
  4. Copy or download the result.

Everything runs in your browser, and nothing is sent to a server. If the JSON is invalid, the formatter points to the line with the problem.

Before and after

{"user":{"id":42,"name":"Sam","roles":["admin","editor"],"active":true}}
{
  "user": {
    "id": 42,
    "name": "Sam",
    "roles": ["admin", "editor"],
    "active": true
  }
}

The most common JSON errors and fixes

ErrorInvalidValid
Trailing comma{"a": 1, "b": 2,}{"a": 1, "b": 2}
Single quotes{'name': 'Sam'}{"name": "Sam"}
Unquoted key{name: "Sam"}{"name": "Sam"}
Comment{"a": 1 // note}Remove the comment
Missing comma{"a": 1 "b": 2}{"a": 1, "b": 2}
Unescaped quote in a string{"q": "He said "hi""}{"q": "He said \"hi\""}
Special values{"a": undefined, "b": NaN}Use null or a number

Most of these happen when JSON is written by hand or copied from JavaScript code, which allows things JSON doesn't.

Reading "Unexpected token" errors

Format JSON in other places

Sharing JSON with someone

Format it first, remove tokens, emails and other personal data, then paste it into the code sharing tool with the JSON language selected. The link keeps indentation and adds line numbers, which is much easier to discuss than a one-line blob in chat. See how to share code snippets online.

JSON in URLs

Passing JSON in a URL parameter? It must be URL-encoded first, or characters like &, # and spaces will break the link. See how to encode and decode URLs.

Format, validate and minify JSON in your browser, with copy and download. Nothing is sent to a server.

Format or validate JSON

Frequently asked questions

How do I format JSON?

Paste it into a JSON formatter and click Format. It adds line breaks and indentation so the structure is readable. In VS Code, use Format Document (Shift+Alt+F on Windows, Shift+Option+F on Mac).

Why is my JSON invalid?

The most common causes are a trailing comma after the last item, single quotes instead of double quotes, unquoted keys, comments, and missing commas or brackets.

Can JSON have comments?

No. Standard JSON does not allow comments. Some tools accept variants such as JSONC or JSON5, but strict parsers will reject them.

What does minify JSON mean?

Removing all unnecessary spaces and line breaks to make the JSON as small as possible, which is useful for sending it over a network or storing it.

Is it safe to paste JSON into an online formatter?

The sshot.online formatter runs in your browser, and nothing is sent to a server. Still, remove tokens and personal data before pasting data into any website.

Related articles

← All guides