"Unexpected token" means JSON.parse hit a character that valid JSON cannot contain at that point. When the token is < at position 0, you almost always received an HTML page (an error page, login page or 404) instead of JSON. Other common causes are trailing commas, single quotes, comments, NaN and an invisible byte-order mark.
The fix is nearly always in the data, not the parser. This guide maps each error message to its cause, shows how to find the exact character that broke, and explains the JSON-versus-JavaScript differences behind most of these errors. The messages quoted below are exactly what JSON.parse printed when we ran each broken input in Node.js 22 (V8). The AltFTool JSON editor flags the same faults in its own words, with a line and column.
JSON's strict rules
JSON looks like a JavaScript object literal, but it is a much stricter, language-independent data format, standardised as RFC 8259 and ECMA-404. Many parse errors come from writing JavaScript habits into JSON.
| Rule | Valid JSON | Not valid JSON |
|---|---|---|
| Keys must be strings in double quotes | {"a": 1} | {a: 1} |
| Strings use double quotes only | "text" | 'text' |
| No trailing commas | [1, 2, 3] | [1, 2, 3,] |
| No comments of any kind | {"a": 1} | {"a": 1 // note} |
| Only true, false and null as literals, in lower case | true | True, undefined |
| Numbers are decimal, with an optional exponent | 0.5, 31, -1e3 | .5, +1, 01, 0x1F, NaN, Infinity |
| No raw line breaks inside strings | "line1\nline2" | "line1 (newline) line2" |
If your data needs comments or trailing commas, you are using a JSON-like format (such as JSONC, used by many editor config files, or JSON5), not JSON. Strip those features before you hand the text to JSON.parse or send it to an API.
Error messages and what causes them
Node.js 22 (which uses the V8 engine) either quotes the start of the bad input or gives a position, followed by a line and column. This table pairs each message with the input that produced it.
| Message (Node.js 22) | Input that caused it | Likely cause |
|---|---|---|
| Unexpected token '<', "<!DOCTYPE "... is not valid JSON | <!DOCTYPE html>… | Server returned HTML: an error page, login redirect, 404 or dev-server fallback |
| Unexpected end of JSON input | an empty string | Empty response body (often a 204 or a failed request), or truncated text |
| "[object Object]" is not valid JSON | an object passed to JSON.parse | Parsing something that is already an object |
| "undefined" is not valid JSON | the value undefined | A missing variable or object property passed to JSON.parse |
| Expected double-quoted property name in JSON at position 8 | {"a": 1,} | Trailing comma in an object |
| Unexpected token ']', "[1, 2, 3,]" is not valid JSON | [1, 2, 3,] | Trailing comma in an array |
| Expected property name or '}' in JSON at position 1 | {'a': 1} or {a: 1} | Single-quoted or unquoted key |
| Expected ',' or '}' after property value in JSON at position 8 | {"a": 1 // note} | A comment |
| Unexpected token 'N', "{"a": NaN}" is not valid JSON | {"a": NaN} | NaN or Infinity written into the data |
| Unexpected number in JSON at position 7 | {"a": 01} | A number with a leading zero |
| Bad control character in string literal in JSON at position 12 | {"a": "line1 (raw line break) line2"} | Unescaped newline or tab inside a string |
| Unexpected token '', "{"a": 1}" is not valid JSON | text starting with U+FEFF | Byte-order mark at the start of the file |
Older versions of Chrome and Node.js worded the first case as "Unexpected token < in JSON at position 0". It is the same failure: the first character of the response was the opening < of an HTML tag. The byte-order-mark case is nasty because the culprit is invisible. The message seems to quote an empty token, and the text looks perfect in most editors. JSON text sent over a network must not start with a byte-order mark, although parsers are allowed to ignore one, so the safest fix is to save the file as UTF-8 without a BOM.
Validate JSON with the line and column of the first error, spot duplicate keys, then pretty-print, minify or export to CSV, all in your browser.
Finding the exact position
When the message includes "at position N", N is the zero-based index of the offending character, so position 0 is the first character. Node 22 also adds a line and column, counted from 1. For {"a": 1,}, position 8 (line 1, column 9) is the closing brace, the point where the parser expected another property name after the comma.
- Copy the exact text that failed. Log it rather than retyping it, because the bug is often an invisible character.
- If the message gives a position, jump to that character with
text.slice(Math.max(0, N - 20), N + 20)to see it in context. - Look one character to the left as well: a trailing comma or missing quote usually sits just before the reported position.
- For long payloads, paste the text into a validator that reports lines and columns, fix the first error, and run it again. One mistake can cause errors further down.
The JSON formatter shows the parser's message as you type, and its "Why does my JSON fail to parse?" notes cover the JavaScript-versus-JSON slips listed above.
Debugging fetch() responses
Many "Unexpected token '<'" errors in web apps come from calling response.json() on a response that is not JSON. response.json() rejects with a SyntaxError when the body cannot be parsed, but its message shows at most the first few characters of the body. Check the response before parsing:
const res = await fetch(url);
const text = await res.text();
if (!res.ok) {
throw new Error(`HTTP ${res.status}: ${text.slice(0, 200)}`);
}
const type = res.headers.get("content-type") || "";
if (!/\bjson\b/i.test(type)) {
throw new Error(`Expected JSON, got ${type}: ${text.slice(0, 200)}`);
}
const data = JSON.parse(text);
Reading the body as text first means a failure shows you the real content: an HTML error page, a proxy's login screen, or an empty body. Common root causes are a wrong URL or base path that falls through to an HTML page, an expired session that redirects to a login page, a server error page, and a request that hit a different environment than you expected.
JSON vs a JavaScript object literal
JavaScript accepts most of the "not valid" column of the first table (unquoted and single-quoted keys, trailing commas, NaN, Infinity, hex numbers and .5), which is why copying an object out of source code is a common cause of parse errors. Two rules of thumb:
- Produce JSON with JSON.stringify, not by hand or with string concatenation. It quotes keys, escapes strings and never adds trailing commas or comments.
- Do not parse what is already parsed.
JSON.parse(obj)converts the object to the string "[object Object]" first, then fails on it. If a library already returned an object, use it directly.
Duplicate keys are the opposite trap: they do not throw at all. JSON.parse('{"a":1,"a":3}') quietly returns {"a":3}, keeping the last value. RFC 8259 says keys within an object should be unique, so treat duplicates as a bug even though the parser accepts them. If you are hand-editing a project manifest, the package.json generator writes valid JSON for you.
Frequently asked questions
The very first character of the text you parsed was <, which almost always means the server sent HTML (an error page, login page or 404) instead of JSON. Log the response status and text to see what you actually received.
Usually because the deployed URL is different: a wrong base path or rewrite rule falls through to the app's HTML page, or an expired session or gateway returns a login or error page. Log res.status and the first 200 characters of res.text() in production to see which.
You passed an object, not a string, to JSON.parse, so it was turned into the text [object Object] first. This often happens after res.json() or a library has already parsed the data. Use the object directly.
The text ended before the JSON was complete. Most often the response body was empty, for example a 204 No Content or a failed request, or a download was cut short. Check the status and length of the body before parsing.
No. JSON numbers must be finite decimals, and the only literal names are true, false and null. JSON.stringify writes NaN and Infinity as null and leaves out object properties whose value is undefined.
Look for an invisible byte-order mark (U+FEFF) at the start of the file. Re-save the file as UTF-8 without a BOM, or strip a leading \uFEFF before parsing.
Sources and notes
Links the author listed for this post.
- 1
- 2
- 3JSON.parse() (opens in a new tab)
MDN Web Docs
- 4Response: json() method (opens in a new tab)
MDN Web Docs







Discussion
0 comments