JSON is deceptively simple — until it isn't. A single misplaced character breaks the entire payload, and the error messages are often cryptic. Here are the 10 JSON errors that show up most often, with the exact error text you'll see and a clear fix for each.
If you want to pinpoint errors automatically, paste your JSON into the JSON Validator — it reports the exact line and column of the problem.
1. Unexpected token < in JSON at position 0
Error message: SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
The server returned HTML — usually a 404 or 500 error page — instead of JSON. The parser hits the opening < of the HTML document and fails immediately.
// What you're receiving (not JSON): <!DOCTYPE html> <html><body>404 Not Found</body></html>
Fix: Check the HTTP response status code first. Log response.status before calling response.json(). A 404 or 500 returns HTML, not JSON. Fix the API URL or handle the error case explicitly.
const res = await fetch('/api/data');
if (!res.ok) throw new Error(`HTTP ${res.status}`); // Check before .json()
const data = await res.json();
2. Trailing comma after last property
Error message: SyntaxError: Unexpected token '}' in JSON
JavaScript allows trailing commas. JSON does not. This is the most common error when writing JSON by hand or copying from JS source code.
// Error:
{
"name": "Alice",
"age": 28, // ↠trailing comma
}
// Fixed:
{
"name": "Alice",
"age": 28
}
Fix: Remove the comma after the last property or array element. Use the JSON Validator to find it — it highlights the exact position.
3. Single quotes instead of double quotes
Error message: SyntaxError: Unexpected token ''' is not valid JSON
JSON requires double quotes for both keys and string values. Single quotes are valid JavaScript but invalid JSON.
// Error:
{'name': 'Alice', 'city': 'NYC'}
// Fixed:
{"name": "Alice", "city": "NYC"}
Fix: Replace all single quotes with double quotes. If your keys come from a Python dict or JS object literal, make sure you're serializing with json.dumps() (Python) or JSON.stringify() (JavaScript).
4. Unquoted keys
Error message: SyntaxError: Unexpected token 'n' is not valid JSON
In JavaScript, object keys don't need quotes. In JSON, every key must be a double-quoted string.
// Error (valid JS, invalid JSON):
{name: "Alice", age: 28}
// Fixed:
{"name": "Alice", "age": 28}
Fix: Always quote your keys. If you're constructing JSON manually, use JSON.stringify() instead — it always produces valid JSON.
5. Undefined value
Error message: SyntaxError: Unexpected token 'u', "...undefined..." is not valid JSON
undefined is a JavaScript concept. It has no equivalent in JSON. JSON.stringify() silently drops keys whose values are undefined; but if you somehow embed the literal string undefined in JSON text, it fails immediately.
// Error:
{"name": undefined}
// Fixed (use null for absent values):
{"name": null}
Fix: Use null instead of undefined. Audit your serialization code for values that could be undefined before stringifying.
6. Comments in JSON
Error message: SyntaxError: Unexpected token '/' in JSON
JSON does not support comments of any kind — no //, no /* */. This trips up developers who treat JSON config files like JavaScript.
// Error:
{
// database connection
"host": "localhost",
"port": 5432
}
// Fixed:
{
"host": "localhost",
"port": 5432
}
Fix: Remove all comments. If your tool supports JSONC (JSON with Comments — used by VS Code's settings.json), make sure you're actually using a JSONC parser, not a standard JSON.parse() call.
7. Unexpected end of JSON input
Error message: SyntaxError: Unexpected end of JSON input
The JSON is incomplete — a bracket, brace, or quote was never closed. Often caused by truncated API responses, incomplete file writes, or copy-pasting only part of a JSON blob.
// Error (missing closing brace):
{
"name": "Alice",
"address": {
"city": "NYC"
}
// ↠missing closing } for the outer object
Fix: Paste the JSON into the JSON Formatter — the tree view makes it immediately obvious where a structure is left open. Check for unmatched {, [, or ".
8. NaN or Infinity as a number value
Error message: SyntaxError: Unexpected token 'N', "NaN" is not valid JSON
JavaScript's NaN and Infinity are not valid JSON values. JSON.stringify(NaN) produces null silently; parsing NaN as a literal string fails.
// Error:
{"ratio": NaN, "limit": Infinity}
// Fixed:
{"ratio": null, "limit": null}
Fix: Validate numeric values before serializing. Replace NaN and Infinity with null or a sentinel value your application can handle.
9. Circular reference
Error message: TypeError: Converting circular structure to JSON
A circular reference occurs when an object contains a reference back to itself (directly or through a chain). JSON.stringify() cannot serialize infinite structures.
const obj = { name: "Alice" };
obj.self = obj; // circular reference
JSON.stringify(obj); // TypeError
Fix: Break the cycle before serializing. Use a replacer function with JSON.stringify() to detect and skip circular references, or use a library like flatted for circular-safe serialization.
// Simple circular-safe stringify:
function safeStringify(obj) {
const seen = new WeakSet();
return JSON.stringify(obj, (key, value) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) return '[Circular]';
seen.add(value);
}
return value;
}, 2);
}
10. Numbers as object keys
Error message: SyntaxError: Unexpected number in JSON
In JSON, all keys must be strings. Numeric keys are valid in JavaScript arrays and objects, but invalid in JSON.
// Error (numeric key):
{1: "first", 2: "second"}
// Fixed:
{"1": "first", "2": "second"}
Fix: Always quote keys. If you're serializing from JavaScript, JSON.stringify() automatically converts numeric keys to strings for plain objects.
Quick Reference: Error → Cause → Fix
| Error message snippet | Cause | Fix |
|---|---|---|
| Unexpected token < | Server returned HTML | Check HTTP status before .json() |
| Unexpected token } | Trailing comma | Remove last comma |
| Unexpected token ' | Single quotes | Use double quotes everywhere |
| Unexpected token n (unquoted key) | Unquoted key | Quote all keys |
| Unexpected token u (undefined) | undefined value | Replace with null |
| Unexpected token / | Comment in JSON | Remove all comments |
| Unexpected end of JSON input | Truncated / unclosed | Check for unclosed braces/brackets |
| Unexpected token N (NaN) | NaN or Infinity | Replace with null |
| Converting circular structure | Circular reference | Use replacer to skip cycles |
| Unexpected number | Numeric key | Quote all keys |
The fastest way to diagnose any JSON error
Paste the JSON into the JSON Validator. It parses the input and reports the exact line number, column, and error type. For complex nested structures, the JSON Formatter's tree view makes open brackets and missing values immediately visible.