You've copied a JSON response from an API, pasted it into your code, and... nothing works. The error message says "unexpected token at line 1." The whole payload is one massive line of text with no indentation, and somewhere in there, a comma is missing. Sound familiar?
JSON is everywhere. Config files, API responses, database exports, package manifests — if you write code, you deal with JSON daily. Yet it's shockingly easy to break. One trailing comma, one unquoted key, one misplaced bracket, and the parser throws up its hands.
Let's fix that. Here's everything you need to format, validate, and debug JSON quickly.
What valid JSON actually looks like
Before hunting for errors, it helps to know the rules. JSON is strict — much stricter than JavaScript objects. Here's what's allowed:
- Keys must be double-quoted strings.
{ name: "Jo" }is invalid.{ "name": "Jo" }is correct. - Strings use double quotes only. Single quotes won't work.
- No trailing commas.
{ "a": 1, "b": 2, }fails. Remove that last comma. - No comments. Not
//, not/* */, not anything. JSON has zero support for comments. - Values can be: strings, numbers, booleans (
true/false),null, arrays, or nested objects. That's it. - Numbers can't have leading zeros.
007is invalid. Use7instead.
These rules trip up even experienced developers. You write JavaScript all day, and your brain auto-corrects the differences.
Pretty printing: why formatting matters
Minified JSON is great for network transfer. It's terrible for humans. When you're debugging a 200-field API response, you need indentation and line breaks — otherwise you're reading a wall of characters.
Pretty printing (also called "beautifying") adds consistent indentation so you can see the structure at a glance. Nested objects indent further. Arrays line up. You can actually spot where a bracket opens and closes.
Most code editors can format JSON, but when you're working with a quick snippet from a curl response or a log file, firing up VS Code feels like overkill. That's where a browser-based JSON Formatter comes in handy. Paste your JSON, hit format, and you've got clean, readable output in seconds.
Common JSON errors and how to fix them
Here are the mistakes that cause 90% of JSON parsing failures:
Trailing commas
{
"name": "Alice",
"age": 30,
}
That comma after 30 is illegal in JSON. JavaScript allows it. JSON doesn't. Delete it.
Single quotes
{
'name': 'Alice'
}
JSON requires double quotes for both keys and string values. Replace every single quote with a double quote.
Unquoted keys
{
name: "Alice"
}
This is valid JavaScript but not valid JSON. Wrap the key in double quotes: "name".
Missing commas between items
{
"first": "Alice"
"last": "Smith"
}
Each key-value pair needs a comma separator (except the last one). Add a comma after "Alice".
Comments
{
"debug": true // enable debug mode
}
Strip all comments before parsing. If you need commented config files, consider JSONC (JSON with Comments) or YAML instead — but standard JSON parsers will reject this.
Wrong value types
{
"count": undefined
}
undefined isn't a valid JSON value. Use null if you need to represent an absent value.
How to validate JSON fast
You've got a chunk of JSON and you're not sure it's valid. What's the quickest way to check?
Option 1: Browser tool. Open the JSON Formatter on ToolsJam, paste your JSON, and it'll instantly tell you whether it's valid. If there's a syntax error, you'll see exactly where it breaks. No installs, no accounts, nothing leaves your browser.
Option 2: Command line. If you're already in a terminal, echo '{"a":1}' | python3 -m json.tool will validate and pretty-print in one shot. On failure, Python gives you a line and column number.
Option 3: In your editor. Most editors highlight JSON syntax errors in real time. But this only works if the file has a .json extension — pasting into a scratch buffer might not trigger validation.
For quick one-off checks, a browser-based tool is usually the fastest path. You don't need to context-switch out of whatever you're working on.
JSON formatting cheat sheet
Here's a quick reference for everyday JSON tasks:
| Task | How |
|------|-----|
| Pretty print | Paste into a JSON formatter or use JSON.stringify(obj, null, 2) |
| Minify | JSON.stringify(obj) with no extra arguments |
| Validate | Wrap in try { JSON.parse(str) } catch(e) { ... } |
| Sort keys | JSON.stringify(obj, Object.keys(obj).sort(), 2) |
| Extract a value | JSON.parse(str).path.to.value |
| Convert from CSV | Map rows to objects, then JSON.stringify() |
Working with large JSON files
When your JSON file is several megabytes — a database export, a huge API response, a GeoJSON file — formatting gets trickier. Some tips:
- Don't paste into a text editor that loads the whole file into memory. It'll freeze.
- Use streaming parsers like
jqon the command line. Runjq '.' file.jsonto pretty print without loading everything at once. - Filter before formatting. If you only need a few fields, extract them first:
jq '.results[] | {id, name}' file.json. - Browser tools work fine for files under a few MB. The JSON Formatter handles typical API responses without breaking a sweat.
JSON vs. similar formats
Wondering whether JSON is the right format for your use case? Here's how it stacks up:
- YAML — More readable for config files, supports comments, but whitespace-sensitive (indentation errors are painful).
- TOML — Great for simple config. Less suited for deeply nested data.
- XML — Verbose but has schemas, namespaces, and attributes. Still dominant in enterprise systems.
- CSV — Perfect for flat, tabular data. Can't represent nesting.
JSON wins for API communication and data interchange because it's lightweight, universally supported, and maps directly to data structures in most languages.
FAQ
What's the difference between a JSON formatter and a JSON validator?
A formatter takes valid JSON and makes it readable by adding indentation and line breaks. A validator checks whether a string is valid JSON at all. Most tools — including the ToolsJam JSON Formatter — do both at once: they validate your input and format it if it's correct.
Can I format JSON with comments?
Standard JSON doesn't support comments. If your input contains // or /* */ comments, you'll need to strip them before formatting. Some tools support JSONC (JSON with Comments), but that's a different spec. For standard JSON, comments are a syntax error.
Is there a maximum size for JSON?
The JSON spec doesn't define a size limit. In practice, the limit depends on your parser and available memory. Browser-based tools typically handle files up to a few megabytes. For anything larger, use command-line tools like jq.
Why does my JSON work in JavaScript but fail in a parser?
JavaScript object literals are more forgiving than JSON. They allow unquoted keys, single quotes, trailing commas, and comments. JSON allows none of these. If your "JSON" was written as a JS object, you'll need to convert it: wrap keys in double quotes, switch to double-quoted strings, and remove trailing commas and comments.
Keep a JSON Formatter bookmarked. You'll reach for it more often than you'd expect — and the thirty seconds it saves each time adds up fast.