
2026-07-16
How to Fix "Invalid JSON" Errors: 7 Common Causes and Fixes
Fix "invalid JSON" fast: the 7 most common syntax mistakes — trailing commas, single quotes, unquoted keys — and how to find the broken line in seconds.
An "invalid JSON" error means your text breaks one of JSON's strict syntax rules — and in practice it's almost always one of seven small mistakes: a trailing comma, single quotes, unquoted keys, unbalanced brackets, comments, special values like NaN, or invisible smart-quote characters. A validator finds the exact broken line in seconds; here's how to read what it tells you and fix each cause.
You paste a config file, hit save, and the dreaded message appears: SyntaxError: Unexpected token } in JSON at position 47. Or an API rejects your request body with a blunt 400 Bad Request: invalid JSON. Almost every developer hits this weekly — and the frustrating part is that the broken character is usually something tiny.
The good news: invalid JSON is almost always one of seven mistakes. This guide walks through each one, shows you what the error looks like, and explains the fastest way to pinpoint the exact broken line using a validator like our free JSON Formatter & Validator, which runs 100% in your browser — nothing you paste is uploaded anywhere.
What does "invalid JSON" actually mean?
JSON has a deliberately strict grammar, defined in the RFC 8259 standard. A parser reads your text character by character, and the moment it meets something the grammar doesn't allow, it stops and reports the position. "Invalid JSON" simply means the text broke one of those rules — it says nothing about your data being wrong, only the syntax.
That strictness is a feature: because there's exactly one way to write valid JSON, every language parses it identically. But it also means JSON forgives nothing that JavaScript would happily accept.
The 7 most common causes (and their fixes)
1. Trailing commas
The number-one offender. JavaScript allows a comma after the last item; JSON does not.
{ "name": "Ali", "age": 30, } ❌
{ "name": "Ali", "age": 30 } ✅
Fix: delete the comma before every closing } or ].
2. Single quotes instead of double quotes
JSON strings — including keys — must use double quotes.
{ 'name': 'Ali' } ❌
{ "name": "Ali" } ✅
3. Unquoted keys
{ name: "Ali" } is valid JavaScript, but JSON requires { "name": "Ali" }. Every key needs double quotes, no exceptions.
4. Missing or extra brackets and braces
Every { needs a } and every [ needs a ]. In a 500-line file, one missing brace produces an error at the very end of the file — the parser only notices when it runs out of text. A formatter that re-indents the file makes the unbalanced pair jump out visually.
5. Comments
JSON has no comment syntax. // like this or /* like this */ will fail immediately. If you need commented config, that's a sign you may want YAML instead — see our comparison JSON vs YAML: which should you use?.
6. Special values: undefined, NaN, Infinity
JSON allows only true, false, null, numbers, strings, arrays and objects. JavaScript's undefined, NaN and Infinity are all invalid — use null or a string instead.
7. Invisible characters: smart quotes and BOM
The sneakiest one. Copying JSON from Word, Google Docs, a chat app or a website often converts straight quotes " into curly "smart quotes" "…" — which look nearly identical but are different characters. Files saved on Windows sometimes start with an invisible byte-order mark (BOM) that some parsers reject. If your JSON looks perfect but still fails, retype the quotes by hand or paste it through a validator that highlights the offending character.
How to read the error message
Parser errors give you a position, and decoding it saves minutes:
| Error | What it usually means |
|---|---|
Unexpected token ' in JSON | Single quotes somewhere |
Unexpected token } / ] | Trailing comma right before it |
Unexpected end of JSON input | Missing closing brace/bracket, or empty string parsed |
Unexpected token < in JSON | You received an HTML error page, not JSON at all |
Unexpected non-whitespace character after JSON | Two JSON documents pasted together |
That last < error deserves a note: it means the server sent HTML (often a 404 or login page) where your code expected JSON — the bug is in the request, not your parsing.
The fastest fix workflow
- Paste the whole text into a validator. Our JSON Formatter validates as it formats and points at the first broken line — in your browser, with nothing sent to a server, so it's safe even for API responses containing tokens or personal data.
- Fix the first error only, then re-validate. One missing quote can cascade into a dozen phantom errors below it; fixing the first often clears them all.
- Re-format (beautify) the result. Proper indentation makes any remaining structural problem — an extra bracket, a value in the wrong place — visible at a glance.
- Minify when you're done, if the JSON is headed for production, to strip the whitespace back out.
How to avoid invalid JSON in the first place
- Never hand-edit JSON in Word or Docs — use a code editor, which keeps quotes straight and can highlight syntax.
- Generate JSON with a library (
JSON.stringifyin JavaScript,json.dumpsin Python) instead of building strings manually. - Validate before you commit config files — a 5-second paste into a validator beats a failed deployment.
- If your data lives in spreadsheets, convert it properly with a CSV to JSON converter rather than hand-wrapping rows in brackets.
Frequently asked questions
Why does my JSON work in JavaScript but fail in the API? Because JavaScript object literals are a looser grammar than JSON: they allow single quotes, unquoted keys, trailing commas and comments. JSON allows none of these. Anything that must cross a network as JSON has to follow the strict rules.
Is an empty string valid JSON?
No — a completely empty input fails with "unexpected end of JSON input". The smallest valid JSON documents are {} (empty object), [] (empty array), or a single value like null or "text".
Are duplicate keys invalid JSON? Technically the syntax parses, but behaviour is undefined — most parsers silently keep only the last value, which causes bugs that are much harder to find than a syntax error. Treat duplicate keys as broken JSON.
Is it safe to paste sensitive JSON into an online validator? It depends on the tool. Our JSON Formatter & Validator does all parsing locally in your browser — the data never leaves your machine, and you can verify that by loading the page and then going offline before pasting.
Next time the red error appears, don't squint at 300 lines by eye: paste it, read the first error, fix that one thing — you'll be done in under a minute.