How to Format and Validate JSON (and Fix the 7 Most Common Errors)
Unreadable JSON on one line? "Unexpected token" error? Learn how to beautify, validate and minify JSON, and how to fix the syntax errors developers hit most often.
JSON in 30 Seconds
JSON (JavaScript Object Notation) is the most widely used data format on the web: API responses, configuration files (package.json, tsconfig.json), data exports, logs. It's built from just a few elements:
{
"name": "AwesomeToolkit",
"tools": 90,
"free": true,
"languages": ["en", "fr", "es"],
"owner": null
}
Objects in braces, arrays in brackets, strings in double quotes, numbers, true, false and null. That's all. And yet a single misplaced comma is enough to break everything.
Format, Validate, Minify: What's the Difference?
| Operation | What it does | When to use it |
|---|---|---|
| Format (beautify) | Adds indentation and line breaks | Reading an API response, debugging |
| Validate | Checks the syntax and locates errors | Before committing a config file, after editing by hand |
| Minify | Removes all unnecessary whitespace | Production, network transfer, embedding in a URL or a variable |
Format JSON Online
The JSON formatter does all three, in your browser: you can paste JSON containing tokens, customer data or configurations without it being sent anywhere.
- Paste your JSON: it's formatted and highlighted.
- Choose the indentation: 2 spaces, 4 spaces or a tab.
- On error, the tool shows the exact line and column and explains the problem.
- Very large numbers (beyond 2^53, like some IDs) are kept exactly as written, where many tools silently round them.
- Duplicate keys — valid in theory, but a classic source of bugs — are flagged.
The 7 Most Common JSON Errors
1. Trailing Comma
{ "a": 1, "b": 2, }
JavaScript accepts it, JSON doesn't. Remove the last comma.
2. Single Quotes
{ 'name': 'Alice' }
JSON only accepts double quotes, for keys as well as strings.
3. Keys Without Quotes
{ name: "Alice" }
Valid in JavaScript, not in JSON: every key must be in double quotes.
4. Comments
{ "port": 8080 // dev port }
Standard JSON doesn't support comments. Some tools accept them (JSONC, used by VS Code), but an API won't.
5. Missing Comma Between Two Elements
Often after copying and pasting a block: the error is reported at the start of the next element, not at the end of the previous one.
6. Unescaped Special Characters
A line break or a double quote inside a string must be escaped: \n, \". Windows paths need doubled backslashes: "C:\\Users\\file.txt".
7. Invalid Values
undefined, NaN, Infinity and dates like new Date() don't exist in JSON. Use null, a number or an ISO string ("2026-07-09T10:00:00Z").
Reading an error message
"Unexpected token" means the parser found a character it didn't expect at that position. Look at the indicated spot — and at the character just before it: that's often where the real problem is (a missing comma, an unclosed quote).
Going Further
- Convert JSON to YAML (Kubernetes, GitHub Actions, Docker Compose) with the JSON ↔ YAML converter.
- Turn a JSON array into a spreadsheet with the JSON ↔ CSV converter.
- Generate TypeScript types from an API response with JSON to TypeScript.
- Inspect a JWT, whose payload is JSON encoded in Base64URL, with the JWT decoder.
Conclusion
A JSON formatter is one of those small tools you use ten times a day without thinking about it. Format to read, validate before committing, minify for production — and when the parser complains, the error is almost always one of the seven above.
Share this article
Related articles
Regex for Beginners: Learn Regular Expressions with 10 Practical Examples
Regular expressions look cryptic, but a dozen symbols cover 90% of uses. Learn the basics of regex with concrete examples — emails, phone numbers, dates — and test them live.
JWT Explained: How JSON Web Tokens Work (with Examples)
What's inside a JWT, how the signature works, what the exp, iat and sub claims mean, and the security mistakes to avoid — with a decoded example.
How to Turn Code into a Beautiful Image (for Slides, Docs and Social Media)
Share code on LinkedIn, X or in a presentation as a sharp, highlighted image instead of a blurry screenshot. Themes, backgrounds, readability tips — and why you should always add the text version too.