How to Format JSON and Fix Common JSON Errors
Learn how to format JSON so it’s readable, validate it, and fix the errors that break it. Free JSON formatter and validator that runs in your browser.

In this article
- What is JSON?
- How to format JSON in 4 steps
- Formatted vs minified JSON
- The 7 most common JSON errors (and fixes)
- How to read a JSON error message
- JSON data types explained
- Escaping special characters
- Why does my JSON look fine but still fail?
- Working with large JSON files
- Converting JSON to other formats
- JSON best practices
- Is it safe to format JSON online?
- JSON vs other data formats
- Validating JSON against a schema
- How to format JSON in code
- Conclusion
An API returns one long line of text, and you need to find a single value buried inside. Or your app crashes with “Unexpected token.” Knowing how to format JSON and fix its common errors turns both problems into a 10-second job.
This guide covers the JSON rules that matter, how to format and validate JSON for free, and how to fix the errors developers hit most often.
What is JSON?
JSON (JavaScript Object Notation) is a lightweight text format for data. APIs, config files, databases and apps use it to store and exchange information. It’s built from two structures:
- Objects: key–value pairs inside curly braces, like
{"name": "Ada"}. - Arrays: ordered lists inside square brackets, like
[1, 2, 3].
Values can be strings, numbers, true, false, null, objects or arrays. The full grammar fits on one page at json.org.
How to format JSON in 4 steps
ToolBubble’s JSON Formatter & Validator runs in your browser. Your data isn’t sent to any server.
- Open the JSON Formatter.
- Paste your raw or minified JSON into the input box.
- Click Format. The tool indents the JSON and checks the syntax.
- If there’s an error, the tool shows where it is. Fix it and format again, then copy the result.
Need the opposite? Use the minify option to strip whitespace for production payloads.
Formatted vs minified JSON
Here’s the same data in both styles. Minified:
{"user":{"id":42,"name":"Ada","roles":["admin","editor"]}}
Formatted:
{
"user": {
"id": 42,
"name": "Ada",
"roles": ["admin", "editor"]
}
}
Both are identical to a computer. Formatted JSON is for humans. Minified JSON is for networks, where every byte counts.
The 7 most common JSON errors (and fixes)
| Error | Invalid | Valid |
|---|---|---|
| Trailing comma | [1, 2, 3,] | [1, 2, 3] |
| Single quotes | {'a': 1} | {"a": 1} |
| Unquoted key | {a: 1} | {"a": 1} |
| Comments | {"a": 1 // note} | {"a": 1} |
| Missing comma | {"a": 1 "b": 2} | {"a": 1, "b": 2} |
| Undefined or NaN | {"a": undefined} | {"a": null} |
| Unescaped quote | {"q": "say "hi""} | {"q": "say \"hi\""} |
Most of these come from writing JSON by hand or copying JavaScript objects. JavaScript allows single quotes, unquoted keys and trailing commas. JSON doesn’t.
How to read a JSON error message
Error messages like “Unexpected token } in JSON at position 87” look cryptic, but they’re precise. The position is the character where parsing failed.
The real mistake is usually just before that position. A missing comma or an extra one makes the next character unexpected. Look one or two tokens to the left of where the error points.
A formatter helps here too. Once JSON is indented, errors like a missing closing bracket become obvious, because the indentation looks wrong.
JSON data types explained
Every value in JSON is one of six types. Getting them right prevents subtle bugs:
| Type | Example | Notes |
|---|---|---|
| String | "hello" | Always double quotes. Escape quotes and backslashes. |
| Number | 42, 3.14, -1e3 | No leading zeros, no NaN or Infinity. |
| Boolean | true, false | Lowercase only. |
| Null | null | Lowercase. Means “no value.” |
| Object | {"a": 1} | Keys must be strings in double quotes. |
| Array | [1, "two", null] | Values can mix types. |
A common trap is "true" in quotes. That’s a string, not a boolean, and code that checks if (value === true) will fail.
Escaping special characters
Strings can contain any Unicode text, but some characters must be escaped with a backslash:
\"for a double quote\\for a backslash\nfor a new line and\tfor a tab\u00e9for a Unicode code point, here “é”
Windows file paths are a classic source of errors. "C:\new\file" is invalid because \n and \f become control characters. Write "C:\\new\\file" instead.
Why does my JSON look fine but still fail?
Sometimes JSON looks perfect but won’t parse. Invisible characters are usually to blame:
- Smart quotes. Word processors and chat apps turn
"into curly quotes. JSON only accepts straight quotes. - A byte order mark (BOM). Some editors add an invisible character at the start of the file.
- Non-breaking spaces. Copying from web pages can bring in special space characters.
Paste the JSON into a plain-text editor or the formatter and retype any suspicious quotes. Never write JSON in a word processor.
Working with large JSON files
API responses can run to thousands of lines. A few habits make them manageable:
- Format first, then use your browser’s find (Ctrl+F or Cmd+F) to jump to a key.
- Collapse sections you don’t need in an editor that supports folding.
- Copy just the part you need into a fresh document to focus on it.
Converting JSON to other formats
Sometimes JSON isn’t the format you need. Spreadsheets want CSV, and older systems want XML.
- JSON to CSV turns an array of objects into a spreadsheet, using keys as column headers.
- CSV to JSON does the reverse, ideal for importing spreadsheet data into an app.
- XML to JSON converts XML feeds and legacy API responses.
JSON best practices
- Use consistent key names. Pick camelCase or snake_case and stick with it.
- Prefer null over missing keys when a value is intentionally empty, so consumers know the field exists.
- Use ISO 8601 dates, such as
2026-10-08T09:30:00Z. They sort correctly and parse everywhere. - Keep numbers as numbers. Don’t wrap prices or IDs in quotes unless they can exceed safe integer limits.
- Validate before you ship. A quick check prevents broken config files in production.
Is it safe to format JSON online?
Many online formatters send your data to their servers. That’s risky if your JSON contains customer records or tokens.
ToolBubble’s formatter runs locally in your browser. The data stays on your device. Even so, it’s good practice never to paste live passwords or API keys into any website, and to rotate any secret that’s been exposed.
JSON vs other data formats
JSON isn’t the only way to store structured data. Here’s when each popular format fits best:
- JSON is the default for web APIs and JavaScript apps. It’s compact and every language can read it.
- YAML is popular for config files because it allows comments and is easy to read. Indentation errors can be hard to spot.
- XML is verbose but powerful, with schemas and namespaces. It’s still common in enterprise systems, RSS feeds and office documents.
- CSV is perfect for flat tables, such as spreadsheet exports. It can’t represent nested data well.
If you’re designing a new API, JSON is almost always the right choice. Pair it with clear documentation and example responses.
Validating JSON against a schema
Syntax validation tells you the JSON is well formed. It doesn’t tell you the data is correct. A field might be missing, or a number might arrive as a string.
JSON Schema solves this. You describe the expected shape once, including required fields, types and allowed values. Libraries in every language can then check incoming data against it automatically.
How to format JSON in code
You can also format JSON programmatically. In JavaScript, JSON.stringify(data, null, 2) produces indented output. In Python, json.dumps(data, indent=2) does the same.
For quick checks, a browser tool is faster than writing a script. For automated pipelines, use your language’s built-in JSON library and a linter in your editor.
Conclusion
Now you know how to format JSON, read its error messages and fix the mistakes that break it. Most errors come down to quotes, commas and brackets.
Paste your next API response into ToolBubble’s free JSON Formatter. It’s instant, private and catches errors before they reach production.
Frequently asked questions
What does formatting JSON do?
Formatting adds line breaks and indentation so nested objects and arrays are easy to read. It doesn’t change the data itself.
Why is my JSON invalid?
The most common causes are trailing commas, single quotes instead of double quotes, unquoted keys, comments and missing brackets. A validator shows the exact position of the problem.
Can JSON have comments?
No. Standard JSON doesn’t allow comments. Remove them, or use a format like JSONC or YAML if your tool supports it.
What is minified JSON?
Minified JSON has all unnecessary spaces and line breaks removed. It’s smaller for sending over networks but hard for people to read.
Is it safe to paste API data into an online JSON formatter?
ToolBubble’s formatter runs in your browser, so the JSON isn’t sent anywhere. Still, avoid pasting live secrets like API keys into any website.
- #json
- #json formatter
- #json validator
- #developer tools
- #api
Written by
ToolBubble Team
The team behind ToolBubble, a free collection of browser-based tools for files, media and everyday work.
Can’t find the tool you need?
Tell us what you’re trying to do. Many ToolBubble tools started as a request from someone like you.