"Unexpected Token in JSON": What It Means and How to Fix It
Published November 25, 2026 · 7 min read
“Unexpected token in JSON” means the parser hit a character it didn't expect, so the text is not valid JSON. Usually it is a small typo: a trailing comma, a single quote, or a missing bracket. Sometimes the server sent an HTML page instead of JSON. Here is how to find the exact cause in a minute.
Fastest way to find the error: paste the JSON into the free JSON Formatter & Validator and click Validate. It shows the line and column where the JSON breaks, so you don't have to count characters.
What the error looks like
The wording depends on where you parse the JSON. Here is the same mistake, a trailing comma in
{"a":1,}, plus the HTML case, in each environment:
| Where | Typical message |
|---|---|
| Chrome, Edge, Node.js | Expected double-quoted property name in JSON at position 7 (line 1 column 8)or, for HTML: Unexpected token '<', "<!DOCTYPE "... is not valid JSON |
| Firefox | JSON.parse: expected double-quoted property name at line 1 column 8 of the JSON dataor, for HTML: JSON.parse: unexpected character at line 1 column 1 of the JSON data |
| Python | 3.13 and newer: json.decoder.JSONDecodeError: Illegal trailing comma before end of object: line 1 column 7 (char 6)older versions: Expecting property name enclosed in double quotes: line 1 column 8 (char 7) |
| PHP | json_decode() returns null, and json_last_error_msg() says Syntax error |
The position or column number tells you where the parser gave up. The real mistake is usually just
before that spot. Older Chrome versions said Unexpected token } in JSON at position 7; it
means the same thing.
The 7 most common causes
1. A trailing comma
{ "name": "Ali", "age": 30, }
JSON doesn't allow a comma after the last item. Remove it.
2. Single quotes instead of double quotes
{ 'name': 'Ali' }
JSON keys and strings must use double quotes: { "name": "Ali" }.
3. Keys without quotes
{ name: "Ali" }
This is valid JavaScript, but not valid JSON. Every key needs double quotes.
4. The response is HTML, not JSON
If the error mentions Unexpected token '<', the first character is <.
Your API returned an HTML page, usually a 404, 500 or login page. Open the request URL in a browser or
check the Network tab to see what came back.
5. An empty response
Unexpected end of JSON input means the response was empty or cut off. Check that the
server actually sends a body, and that you are not calling JSON.parse twice.
6. Comments in the JSON
JSON has no comments. Remove any // or /* */ lines. If you need comments in
config files, consider YAML instead (see JSON vs
XML vs YAML).
7. Special characters not escaped
A raw line break or a double quote inside a string breaks the JSON. Escape them as \n and
\". Chrome reports this one as Bad control character in string literal.
A quick debugging checklist
- Log the raw text before parsing:
console.log(await response.text()). - Check the first character.
{or[is good.<means HTML. - Paste the text into the JSON Formatter to see the line and column where it breaks.
- Fix the typo, or fix the server so it returns JSON with
Content-Type: application/json.
Once it parses, How to Read a JSON API Response shows how to find the values you need. For more errors and formatting tips, see How to Format, Validate and Fix JSON.
Safe parsing in code
In JavaScript, wrap JSON.parse in try...catch so one bad response doesn't
crash the page:
try {
const data = JSON.parse(text);
} catch (err) {
console.error('Invalid JSON:', err.message);
}
In PHP, use json_decode($text, true, 512, JSON_THROW_ON_ERROR) inside
try...catch to get a clear JsonException instead of a silent
null. In Python, catch json.JSONDecodeError, which has lineno and
colno attributes.
Still stuck? Share the error and the raw response with someone who can help, with tokens removed first.
Paste JSON or drop a .json file. Validate shows the line and column of the first error, and Format indents it so the structure is easy to check.
Frequently asked questions
What does "Unexpected token < in JSON at position 0" mean?
The response starts with <, so it is HTML, not JSON. The server returned an error or login page. Newer Chrome versions word it as Unexpected token '<', "<!DOCTYPE "... is not valid JSON.
Can JSON have a trailing comma?
No. Standard JSON does not allow trailing commas in objects or arrays.
Why does my JSON work in JavaScript but not in JSON.parse?
JavaScript objects allow single quotes, unquoted keys and trailing commas. JSON does not.
How do I find the exact line with the error?
Paste it into a JSON validator such as the free JSON Formatter, which shows the line and column, or read the line and column in the Firefox or Python error message.
Related articles
How to Format, Validate and Fix JSON (With Common Errors Explained)
Turn minified JSON into readable text, validate it, and fix the most common JSON errors: trailing commas, single quotes, unquoted keys and comments. Plus formatting in VS Code and on the command line.
Read More →JSON vs XML vs YAML: Differences With Examples
The same data in JSON, XML and YAML side by side, a comparison table, YAML gotchas like the Norway problem, and when to use each format.
Read More →How to Share an Error Log, Stack Trace or Terminal Output So Someone Can Help
How to capture and share logs, stack traces and terminal output properly: copying the full output, saving it to a file, trimming to what matters, removing secrets, and sharing a link with line numbers.
Read More →