Guide
How to fix “Unexpected token” and other JSON errors
A practical guide to the JSON parse errors you are most likely to see in JavaScript and Python, what causes each one, and how to fix it.
The quick way to find the problem
Paste the JSON into the 2JSON viewer. If it isn’t valid, the Tree tab shows the parser’s error, and the Pretty tab still indents the text so you can see where the structure goes wrong. Most errors come down to one of a handful of mistakes, explained below.
Error messages differ between browsers and languages, but they almost always include a position (a character count) or a line and column. The mistake is usually at that spot or just before it. Programs stop reading at the first problem, so fix errors one at a time and re-check.
“Unexpected token” and “Expected property name”
Typical messages (Chrome/Node, then Python):
SyntaxError: Expected double-quoted property name in JSON at position 8 (line 1 column 9)
SyntaxError: Expected property name or '}' in JSON at position 1 (line 1 column 2)
SyntaxError: Unexpected token ']', "[1, 2, ]" is not valid JSON
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 9 (char 8)
The parser met a character that can’t appear at that point. The usual causes:
Trailing comma
{ "name": "Ada", "age": 36, } ✗
{ "name": "Ada", "age": 36 } ✓
JavaScript and many languages allow a comma after the last item; JSON doesn’t. This is
the most common error in hand-edited JSON. Remove the comma before } or
]. In an object, the parser expects another key after the comma, so the
message talks about a “property name”; in an array it reports the unexpected
].
Single quotes
{ 'name': 'Ada' } ✗
{ "name": "Ada" } ✓
JSON only accepts double quotes for both keys and strings. Text copied from Python output
often has single quotes, because printing a Python dictionary doesn’t produce JSON. Use
json.dumps(data) instead of print(data).
Unquoted keys
{ name: "Ada" } ✗
{ "name": "Ada" } ✓
Comments
{
// the user's name ✗
"name": "Ada"
}
Standard JSON has no comments. Some tools accept them in config files (a format sometimes
called JSONC), but JSON.parse and most APIs don’t. Remove them, or move the
note into a value like "_comment": "...".
Missing comma between items
{ "name": "Ada" "age": 36 } ✗
{ "name": "Ada", "age": 36 } ✓
Messages like “Expected ',' or '}' after property value” point here. It often happens when two lines are pasted together.
“Unexpected token <” … “<!DOCTYPE” is not valid JSON
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
This one is rarely a problem with your JSON. Your code asked a server for JSON but got an
HTML page back, and the first character of HTML is <.
Common reasons:
- The URL is wrong, and the server returned a 404 page.
- The server crashed and returned an HTML error page (500).
- You are not logged in, and the request was redirected to a login page.
- In a single-page app, the dev server returned
index.htmlfor an API route that doesn’t exist.
Check the response in your browser’s developer tools (Network tab), and check the status code before parsing:
const res = await fetch('/api/user')
if (!res.ok) throw new Error(`Request failed: ${res.status}`)
const data = await res.json()
“Unexpected end of JSON input”
SyntaxError: Unexpected end of JSON input
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
The parser ran out of text before the JSON was complete. If the text stops right after a value, you may see “Expected ',' or '}' after property value” instead, and an unclosed string gives “Unterminated string”. Either way, the cause is the same:
- The input is empty. For example, a server replied with 204 No Content or an empty body, and the code called
res.json()anyway. - The JSON was cut off. A log line was truncated, a download stopped early, or a bracket was never closed.
Paste the text into the viewer. In the Pretty tab, a missing closing bracket shows up as indentation that never returns to the left margin.
Invalid values: NaN, undefined and friends
{ "score": NaN } ✗ use null or a string
{ "total": Infinity } ✗
{ "value": undefined } ✗ leave the key out or use null
{ "flag": True } ✗ true (lowercase)
{ "code": 007 } ✗ 7, or "007" as a string
{ "hex": 0xFF } ✗ 255
These usually come from building JSON by hand or from another language’s output. Python
writes True, False and None; JSON needs
true, false and null. Note that Python’s
json.dumps writes NaN by default, which other parsers reject; pass
allow_nan=False to catch it early.
Problems inside strings
-
Unescaped quotes:
"She said "hi""ends the string early. Write"She said \"hi\"". -
Raw line breaks or tabs: a string can’t contain an actual new line.
Use
\nand\t. Messages like “Bad control character in string literal” point to this. -
Windows paths:
"C:\new\folder"is read as\n(new line) plus\f(form feed). Double the backslashes:"C:\\new\\folder". -
Smart quotes: text copied from Word, Google Docs or some chat apps may
use “curly” quotes instead of straight
"ones. Replace them.
Invisible characters
If the JSON looks perfect but still fails at position 0 or at a strange spot, there may be an invisible character:
- A byte order mark (BOM,
U+FEFF) at the start of a file saved by some Windows editors. Re-save the file as “UTF-8” without BOM. - A non-breaking space (
U+00A0) or zero-width space copied from a web page or document.
Retyping the area around the reported position, or pasting the text into a plain-text editor that shows hidden characters, usually reveals them.
Double-encoded JSON
Sometimes JSON was turned into a string twice, so it arrives wrapped in quotes with escaped quotes inside:
"{\"name\":\"Ada\",\"age\":36}"
This is technically valid JSON, but it is a single string rather than an object. Parse it
twice (JSON.parse(JSON.parse(text))), and fix the sender so it serializes only
once.
How to avoid JSON errors
- Never build JSON by joining strings. Use
JSON.stringify,json.dumpsor your language’s library, which handle quoting and escaping for you. - Check the HTTP status and Content-Type before parsing a response.
- Validate configuration in your editor or CI. Most editors underline JSON errors as you type.
- Format before you read. Indented JSON makes missing brackets and commas easy to spot. Paste it into the formatter.
Cheat sheet
| Message contains | Most likely cause |
|---|---|
| Expected double-quoted property name | Trailing comma in an object |
| Expected property name or '}' | Single quotes or unquoted key |
Unexpected token ] | Trailing comma in an array |
Unexpected token < / <!DOCTYPE | Server returned HTML, not JSON |
| Unexpected end of JSON input | Empty response or truncated JSON |
| Expected ',' or '}' after property value | Missing comma, or JSON cut off after a value |
| Unterminated string | Missing closing quote, or JSON cut off inside a string |
| Bad control character | Raw line break or tab inside a string |
| Unexpected token N / I / u | NaN, Infinity or undefined |