Why Is My JSON Invalid? 7 Common Errors and How to Fix Them Fast
Trailing commas, single quotes, unescaped newlines — JSON parsers are unforgiving. Here are the seven errors that break almost every JSON file, with real fixes and a validator that points to the line.
You paste a config file into a parser and get back a single, infuriating line: SyntaxError: Unexpected token } in JSON at position 247. The character at position 247 looks fine. The character before it looks fine. So what's wrong?
JSON is one of the simplest data formats ever standardized — and one of the easiest to break. The grammar is so strict that a single trailing comma can invalidate a 10,000-line file. This guide walks through the seven most common reasons your JSON won't parse, why each one happens, and how to fix them in seconds.
1. Trailing commas — the #1 cause of invalid JSON
JavaScript allows trailing commas in arrays and objects. JSON does not. This is the single most common source of "unexpected token" errors, and it bites everyone — from junior devs hand-editing a config file to senior engineers copying a snippet out of a JS file.
Broken:
{
"name": "Ada",
"role": "engineer",
} Valid:
{
"name": "Ada",
"role": "engineer"
} The fix is to delete the comma after the last property in every object and the last element in every array. Our JSON formatter highlights the offending line so you don't have to scan a 200-line file by eye.
2. Single quotes instead of double quotes
JSON keys and string values must be wrapped in double quotes. Single quotes are valid in JavaScript and Python literals but not in JSON.
Broken: { 'name': 'Ada' }
Valid: { "name": "Ada" }
A find-and-replace of ' with " usually works, but watch out for apostrophes inside strings ("it's" becomes "it"s" — broken). Better is a parser-aware fix; pasting your data into a JSON formatter that auto-corrects quotes is the safest path.
3. Unquoted keys
Another JavaScript habit that doesn't translate. In JS, { name: "Ada" } is fine. In JSON, every key must be quoted: { "name": "Ada" }.
This often shows up when copying JS object literals into a .json file or into a config field that expects strict JSON. The error message is usually "Unexpected token n in JSON at position 2" — pointing to the first letter of the unquoted key.
4. Unescaped newlines and tabs in strings
JSON strings cannot contain literal newlines or tab characters. They must be escaped as \\n and \\t. Pasting a multi-line string from a text editor directly into a JSON value is a near-guaranteed parse error.
Broken:
{
"address": "221B Baker Street
London"
} Valid:
{
"address": "221B Baker Street\nLondon"
}
Other characters that need escaping inside JSON strings: backslash (\\\\), double quote (\\"), carriage return (\\r), backspace (\\b), form feed (\\f), and any control character below U+0020.
5. Comments — they're not allowed
JSON has no comment syntax. Not //, not /* */, not #. If you copied a JSON file from a Stack Overflow answer that had explanatory comments, every comment is now a parse error.
There's a popular extension called JSON5 that does allow comments, trailing commas, and unquoted keys — but most parsers (including JSON.parse in browsers, Python's json module, and most server-side libraries) only accept strict JSON. If you need comments in a config file, JSONC (used by VS Code) or YAML are better choices than trying to bend JSON.
6. undefined, NaN, and Infinity
Three values that exist in JavaScript but not in JSON: undefined, NaN, and Infinity. If your data source produced one of these — common when serializing JS objects with JSON.stringify over data containing failed numeric conversions — the result is invalid JSON.
Replace them with null (the JSON equivalent of "no value"), or with a sensible default like 0 for numerics. JSON.stringify in JavaScript actually converts undefined to nothing (it removes the key entirely) and NaN/Infinity to null — so the bug is usually upstream of the serializer.
7. BOM and encoding mishaps
A byte order mark (BOM) is an invisible character some Windows editors insert at the start of UTF-8 files. Most JSON parsers reject it. The error looks like "Unexpected token in JSON at position 0" — with what appears to be empty space before "Unexpected token".
To strip a BOM: open the file in VS Code, click "UTF-8 with BOM" in the bottom-right status bar, and select "Save with Encoding → UTF-8" (without BOM). Or paste the file into our JSON formatter — it normalizes encoding automatically before parsing.
How to debug an "unexpected token" error in 30 seconds
The position number in the error message is character offset, not line number — which is why it's so unhelpful for big files. The fastest debugging workflow:
- Paste the file into a JSON validator that shows line and column for errors.
- If the error points to
}or], look at the line above — the actual mistake is usually a trailing comma or missing comma there. - If the error points to a key, check that the previous line ended with a comma.
- If the error points to position 0, check for a BOM.
- If validating a fragment, wrap it in
{ "data": ... }to give the parser a top-level structure.
When the API response is the broken JSON
Sometimes the JSON isn't yours — it's coming from an API and your client is throwing a parse error. Three quick checks:
- HTML in the response. A common gotcha: the server returned an error page (HTML) instead of JSON. Look at the raw response — if it starts with
<!DOCTYPE html>, the API is misconfigured or you're hitting a redirect. - Double-encoded JSON. Sometimes APIs return a JSON string that contains another JSON string. You'll need to
JSON.parsetwice. The give-away: the response starts with"\\"(an escaped double quote inside a string). - Truncated response. If the JSON ends mid-key, your network request was interrupted. Retry with a longer timeout.
For JWTs specifically — which look like JSON but aren't, exactly — see our companion guide on debugging JWT errors. JWTs use base64-encoded JSON in the payload, and decoding them reveals JSON structure that has all the same gotchas above.
Preventing JSON errors before they happen
Three habits that have saved a lot of debugging time:
- Never hand-edit large JSON files. Use a tool that respects the grammar. Editors like VS Code with the built-in JSON validator catch trailing commas as you type.
- Validate before committing. Run JSON files through a validator in CI. A pre-commit hook with
jq empty file.jsonexits non-zero on invalid JSON and saves you from breaking a deploy. - Pretty-print on save. Pretty-printed JSON makes structural errors obvious. Compact JSON hides them. The JSON formatter does both — beautify for editing, minify for production.
The five-minute rule
If you've been staring at a JSON file for more than five minutes and can't find the error, the issue is almost always one of: a trailing comma, a missing comma, a stray single quote, or an unescaped newline inside a string. Validate the file with a tool that shows line numbers, fix the line it points to, repeat. Most "broken" JSON files are one character away from valid.