SyntaxError: Unexpected token in JSON at position N
JavaScript, TypeScript, Node.js
JSON.parse() or await response.json() throws 'Unexpected token < in JSON at position 0' (or another token/position).
What it means (root cause)
The string passed to JSON.parse is not valid JSON — most often an HTML error page (404/500/login redirect) returned with Content-Type text/html, or hand-written JSON with trailing commas, single quotes, or unquoted keys.
The symptom developers actually see: JSON.parse() or await response.json() throws 'Unexpected token < in JSON at position 0' (or another token/position).
Step-by-step fix
Concrete, ordered steps from the dataset. Apply them in order; each line is a verified action, not generic advice.
- 1Log the raw text first: const text = await res.text(); console.log(text) to see what is actually being parsed.
- 2Confirm the Content-Type is application/json; if the server returned HTML, fix the URL/headers or the endpoint, not the parser.
- 3For hand-written JSON, remove trailing commas, quote keys, and use double quotes (JSON.parse rejects single quotes).
- 4Paste the text into a JSON formatter/validator to pinpoint the exact column before parsing.
Where this error appears
Languages, frameworks, and runtimes where this error is observed (from the 2026 DevFixPro error dataset, retrieved 2026-08-29):
Any JS/TS runtime (browser fetch, Node http, Deno, Bun). The error names the offending token and its character position — that position is your debug cursor.
How to prevent it & common questions
Practical prevention plus the questions developers ask most about this error.
What does "SyntaxError: Unexpected token in JSON at position N" mean?
The string passed to JSON.parse is not valid JSON — most often an HTML error page (404/500/login redirect) returned with Content-Type text/html, or hand-written JSON with trailing commas, single quotes, or unquoted keys.
Which environments are affected by SyntaxError: Unexpected token in JSON at position N?
JavaScript, TypeScript, Node.js
How do I fix SyntaxError: Unexpected token in JSON at position N?
Log the raw text first: const text = await res.text(); console.log(text) to see what is actually being parsed. Confirm the Content-Type is application/json; if the server returned HTML, fix the URL/headers or the endpoint, not the parser. For hand-written JSON, remove trailing commas, quote keys, and use double quotes (JSON.parse rejects single quotes). Paste the text into a JSON formatter/validator to pinpoint the exact column before parsing.
How do I prevent SyntaxError: Unexpected token in JSON at position N?
Always read response.json() only after checking res.ok and the Content-Type header; validate external payloads at the boundary.
Why does it say 'Unexpected token <'?
The '<' usually means an HTML page (error page, login redirect, or proxy response) was returned instead of JSON. Print the raw body to confirm before fixing the parser.
Does JSON.parse accept single quotes?
No. JSON requires double-quoted strings and double-quoted keys. Single quotes throw a SyntaxError — use a validator to find the exact spot.
Related DevFixPro tools
Real, browser-only utilities on DevFixPro that help while you work through this issue. These are navigation aids, not a substitute for the fix above.
JSON Formatter & Validator
Pretty-print, minify, or compact JSON and catch syntax errors instantly.
Open tool →JSON Path Extractor
Extract nested values from JSON using simple dot-path notation.
Open tool →JSON to CSV Converter
Flatten an array of JSON objects into downloadable CSV rows.
Open tool →Sources & attribution
- Error records aggregated from Google Search Console query gaps (2026-05-21~2026-08-18) for devfixpro.com, plus official framework docs (MDN, Node.js docs, Python docs, Go.dev, Rust book, Oracle Java docs, Docker docs, npm docs). Source dataset retrieved 2026-08-29. License: CC BY 4.0 — attribute DevFixPro (devfixpro.com).
- Official reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
- All cause, environment, fix and prevention text on this page is taken verbatim from the DevFixPro 2026 error dataset. DevFixPro does not invent root causes or fixes.
How this SyntaxError: Unexpected token in JSON at position N page is built
Each error page is generated from a single record in the DevFixPro 2026 error dataset. The meaning, root cause, environments, fix steps, prevention, and official references are copied verbatim from that dataset and its official-doc sources; related-error links are computed from the error's category and explicit peer list. No root cause or fix is invented. The retrieval date for this dataset is 2026-08-29.