Reference
JSON Error Message Reference
Common JSON parse errors, likely causes and API debugging fixes.
Overview
JSON parse errors often happen after a request succeeds at the HTTP layer. The body may be empty, HTML, malformed JSON, JSON with comments or a truncated response. This reference connects common parser messages to the next debugging step.
Debugging Reference Table
| Error message | Likely cause | Debugging fix |
|---|---|---|
| Unexpected token < | HTML returned instead of JSON | Check status, redirects, auth and Content-Type. |
| Unexpected end of JSON input | Empty or truncated body | Check 204 responses, network failures and server logs. |
| Unexpected token } | Extra closing brace or trailing comma nearby | Format the JSON and inspect the previous key or array item. |
| Unexpected string | Missing comma between properties | Look between adjacent string values or keys. |
| Bad control character in string literal | Unescaped newline or control character | Escape text before inserting it into JSON strings. |
| Invalid escape character | Backslash used incorrectly | Use valid escapes such as \\, \" or \n. |
| JSON with comments | Comments are not valid JSON | Remove comments or use a JSONC-aware parser before conversion. |
| Large number changed after parsing | JavaScript number precision limit | Treat large IDs as strings when precision matters. |
Reference Table Coverage
- Parser messages seen in JavaScript.
- HTML returned to JSON clients.
- Empty responses and 204 handling.
- Precision and escaping issues.
API Debugging Examples
- A fetch call receives a login page and fails with Unexpected token <.
- A delete endpoint returns 204 and response.json() throws Unexpected end of JSON input.
- A copied config has trailing commas from a JavaScript object literal.
Common Mistakes
- Assuming valid-looking JavaScript object syntax is valid JSON.
- Parsing every successful response as JSON.
- Ignoring Content-Type when the body starts with HTML.
- Using numbers for IDs that exceed safe integer range.
FAQ
Why does JSON.parse fail on a response that looks valid?
Common hidden causes include trailing commas, invalid escapes, comments, empty body or characters before the JSON starts.
Why does Unexpected token < usually mean HTML?
HTML documents often start with <!doctype or <html, so the first unexpected JSON character is <.
Can JSON contain comments?
No. Some tools support JSONC, but standard JSON does not allow comments.