Fix "Unexpected token in JSON at position N"
JavaScript / Node.js ยท JSON error
Opens the editor with a broken example loaded and repaired, entirely in your browser โ nothing is uploaded.
What causes it
This is the error JSON.parse() (and fetch().then(r => r.json())) throws when it reaches a character that cannot legally appear at that point in the text. The position is the zero-based character index of the offending token, and the token itself is the biggest clue:
- Unexpected token
<at position 0 โ the response is HTML, not JSON. You are almost certainly parsing an error page (a 404 or 500) or a login redirect instead of the API payload. Check the network response and the status code. - Unexpected token
oat position 1 โ you calledJSON.parse()on something that is already a JavaScript object. Theocomes from the word[o]bjectwhen the object is coerced to a string. Remove the extra parse. - Any other token โ a genuine syntax mistake such as a trailing comma, a single quote, an unquoted key, or a missing bracket at that position.
How to fix it
- Log the raw response text before parsing and confirm it actually is JSON (not HTML or empty).
- If it starts with "<", you are parsing an HTML error page โ fix the request URL, auth, or status handling instead.
- If the token is "o" at position 1, remove the redundant JSON.parse() โ the value is already an object.
- Otherwise, paste the text into JSON Wallet to jump to the exact position and repair trailing commas, single quotes, or missing brackets.
Before and after
Broken:
{
"name": "Ada",
"roles": ["admin",]
}Fixed:
{
"name": "Ada",
"roles": ["admin"]
}Want to fix your own JSON instead of the example? Open the editor, paste it in, and click Repair โ it runs entirely in your browser.
Frequently asked questions
What does "Unexpected token < in JSON at position 0" mean?
The very first character is <, so the response is HTML, not JSON. You are parsing an error page, a redirect, or an empty body. Inspect the network tab and the HTTP status code before calling .json().
Why do I get "Unexpected token o in JSON at position 1"?
You passed an object (not a string) to JSON.parse(). It gets stringified to [object Object], and the parser trips on the o. The value is already parsed โ use it directly.
Related JSON errors
Fix "Unexpected end of JSON input"
The parser reached the end of the text while still expecting more โ an empty response, a truncated payload, or an unclosed bracket or quote.
Fix a Trailing Comma in JSON
JSON forbids a comma after the last element of an object or array. JavaScript and many editors allow it, which is why this trips people up constantly.
Fix JSON With Single Quotes
JSON only allows double quotes. Single quotes โ common when copying object literals from JavaScript or Python โ are rejected by every strict parser.
For a full reference of JSON mistakes, read Common JSON errors and how to fix them.