Common JSON errors and how to fix them
A field guide to the JSON errors you'll actually encounter in the wild - what they mean, where they come from, and how to fix them fast.
Every developer who works with JSON eventually learns its error messages by heart. “Unexpected token in JSON at position 47” — we’ve all stared at that one at 3 am trying to ship a release. The error messages are technically correct but often unhelpful: the position mentioned is rarely where the actual mistake lives.
Here’s a field guide to the JSON errors you’ll actually see in practice, what they really mean, and how to fix each one quickly. All examples are tested against the standard JSON.parse() implementation used by every browser and Node.js.
1. “Unexpected token , in JSON”
Almost always a trailing comma.
{
"name": "Alice",
"role": "admin",
}
The comma after "admin" is invalid. JSON doesn’t allow trailing commas, unlike JavaScript objects or Python dicts.
Fix: remove the trailing comma.
This usually comes from humans hand-editing JSON, or from tools that export JavaScript-style object literals and call them JSON. One more thing: the parser usually points at the line after the comma, not at the comma itself. If the error says “line 5”, look at line 4 first.
2. “Unexpected end of JSON input”
The file is truncated. A closing bracket or brace is missing.
{
"users": [
{ "name": "Alice" },
{ "name": "Bob" }
]
← missing closing brace for the outer object
Fix: count your brackets. Most editors show bracket matching — put your cursor on the opening { and see what it pairs with.
This often comes from failed HTTP downloads (connection dropped before the full response arrived), incomplete writes from a crashing process, or file corruption. Check the file size — if you expected 200 KB and got 180 KB, the file is truncated. Go back to the source.
3. “Unexpected token } in JSON”
An extra closing brace, or a missing comma before a sibling key.
{
"name": "Alice" ← missing comma here
"role": "admin"
}
Fix: add the missing comma.
Or:
{ "name": "Alice" } } ← extra closing brace
Fix: remove the extra brace.
4. “Unexpected token u in JSON”
Literally the character u appeared where a value was expected. Most commonly this is the string undefined, which is not valid JSON.
{ "role": undefined }
undefined is a JavaScript value, not a JSON value. JSON only has null.
Fix: replace undefined with null, or omit the key entirely.
5. “Unexpected token < in JSON”
Your “JSON” is actually HTML. The < is the start of an HTML tag.
<!DOCTYPE html>
<html>
<head><title>500 Server Error</title>
...
Fix: check what your server is actually returning. It’s probably an error page — often HTML — instead of the JSON response you expected.
This comes from a misconfigured endpoint, an authentication failure redirecting to a login page, or a server returning an error page instead of a JSON error body. Before parsing, do console.log(response.text) and look at what actually came back. If you see HTML, your problem is on the server side, not in your JSON parsing.
6. “Unexpected token ’ in JSON”
Single quotes instead of double quotes.
{ 'name': 'Alice' }
JSON strings must be double-quoted. Single-quoted strings are invalid JSON, even though they work in JavaScript and Python.
Fix: replace all single quotes with double quotes. If the strings contain literal double quotes, escape them (\").
This comes from pasting JavaScript object literals and calling them JSON, or from Python dict output that wasn’t serialised via json.dumps().
7. “Unexpected token n in JSON”
Often a literal newline inside a string.
{ "description": "hello
world" }
JSON strings cannot contain literal newlines. Newlines must be escaped as \n.
Fix:
{ "description": "hello\nworld" }
This comes from manually constructing JSON from multi-line input, or from systems that dump text fields directly without escaping.
8. “Unexpected number in JSON”
A number in an invalid format. Common culprits:
+5(leading plus sign — not allowed)07(leading zero on integer — not allowed)1.(decimal point with no following digit).5(leading dot with no zero)Infinity,-Infinity,NaN(not allowed)
Fix: remove leading +, drop leading zeros (07 → 7), add the missing zero (1. → 1.0, .5 → 0.5), and replace Infinity/NaN with null or a sentinel string.
9. “Duplicate key ’…’ in JSON”
An object has the same key twice.
{
"name": "Alice",
"name": "Bob"
}
Standard parsers technically accept this — they silently take the last value. Strict parsers reject it. If your parser flags duplicates as errors, it’s doing you a favour.
Fix: decide which value is correct and remove the other. Always.
10. “Expected ’:’ after property name in JSON”
You used = instead of :, or forgot the colon entirely.
{ "name" = "Alice" } ← wrong (= instead of :)
{ "name" "Alice" } ← wrong (no colon)
Fix: use a colon. This is pure muscle memory from other languages (= in YAML or TOML).
11. Invalid UTF-8 or BOM
The file starts with a BOM (byte-order mark) or contains invalid UTF-8 bytes. The BOM (U+FEFF) is a legacy Windows file-encoding marker. JSON parsers technically should reject it, though many tolerate it silently.
Fix: strip the BOM. On macOS/Linux: tail -c +4 file.json > file-clean.json (drops the 3 BOM bytes). On Windows: save the file as “UTF-8 without BOM” in your editor.
This comes from Windows Notepad saving JSON files, Microsoft products exporting text, and older CMS exports.
12. “Circular structure” errors from JSON.stringify
Strictly a serialisation error, not a parse error, but it comes up often enough.
const obj = { a: 1 }
obj.self = obj
JSON.stringify(obj) // TypeError: Converting circular structure to JSON
Fix: don’t pass a self-referencing structure to stringify. Flatten the data before serialising, or use a library like flatted or safe-stringify that handles cycles explicitly.
This usually comes from React components, DOM nodes, or ORM models with back-references.
A debugging workflow
When you hit a JSON parse error:
- Copy the offending JSON into a formatter
- Click Format — if it rejects, it’ll tell you where
- Look 2-3 lines above the reported line; the real error is often earlier
- Eyeball for the common culprits — trailing comma, missing comma, single quote, unclosed string
- If it’s huge, use binary search: delete the second half of the document and try again. If valid, the error is in the deleted half. Narrow down.
- Check the source — did your API actually return JSON, or HTML / plain text?
Prevention
A few habits that stop JSON errors before they start:
Validate JSON before committing config files to version control. A pre-commit hook that runs jq empty file.json catches most problems.
Use a linter (ESLint, Prettier, etc.) that understands JSON and flags issues on save.
Prefer serialisation libraries (JSON.stringify, json.dumps) over manually constructing JSON strings. Manual construction is where trailing commas, unquoted keys, and unescaped strings come from.
Use a JSON schema for important documents so that structure problems — not just syntax — are caught before deployment.
Keep values typed consistently. Don’t alternate between "1" (string) and 1 (number) for the same field across records; someone will hit a parse error downstream.
Most errors on this list are findable in seconds with our JSON Formatter. Paste the broken JSON, click Format, read the error message. Everything runs in your browser so you can debug sensitive config or API responses without any of it leaving your device.
JSON is simple by design, which means errors are also simple — but they’re annoying when you’re under pressure. A two-minute pass through a formatter usually catches whatever’s wrong and gets you back to actually shipping.