How to Format, Validate and Fix JSON (With Common Errors Explained)
Updated November 5, 2026 · 8 min read
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
- Open the free JSON formatter.
- Paste your JSON.
- Click Format to add indentation and line breaks, Validate to check it, or Minify to compress it to one line.
- 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
| Error | Invalid | Valid |
|---|---|---|
| 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
- "Unexpected token < in JSON at position 0": you received HTML, usually an error page or login page, not JSON. Check the URL and the HTTP status.
- "Unexpected end of JSON input": the JSON was cut off, or the response was empty.
- "Unexpected token } … at position 482": look just before character 482. It's often a trailing comma.
Format JSON in other places
- VS Code: open a
.jsonfile and run Format Document (Shift + Alt + F on Windows, Shift + Option + F on Mac). - Command line with jq:
cat data.json | jq .pretty-prints;jq -c .minifies. - Python:
python -m json.tool data.json. - JavaScript:
JSON.stringify(obj, null, 2)formats with two-space indentation. - Browsers: Firefox shows JSON responses formatted, and Chrome offers a pretty-print option for raw JSON pages.
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.
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
URL Encoding Explained: How to Encode and Decode URLs (With Examples)
What URL (percent) encoding is, which characters need it, encoding a value vs a full URL, a table of common codes, JavaScript and PHP examples, and how to fix double-encoding.
Read More →How to Share Code Snippets Online Without Breaking the Formatting
Share code so indentation survives: why chat apps mangle code, how to share a clean link with line numbers and a raw view, how to choose between a snippet link, a gist and a screenshot, and what to remove first.
Read More →How to Share a SQL Query for Review (Formatting, Context and Safety)
Share SQL so others can review it quickly: formatting conventions, the context to include (schema, EXPLAIN plan, sample data), removing real data, and sharing a clean link with line numbers.
Read More →