Unexpected Token in JSON: What It Means and How to Fix It
Every 'Unexpected token' message names the character that stopped parsing. Learn what Chrome, Firefox and Safari each say, why trailing commas and HTML pages cause it, and how to find the fix fast.
In brief
- What is it?
- An 'Unexpected token' error is what JavaScript throws when JSON.parse meets a character it doesn't expect — the message names that token, so the position it points at marks exactly where the grammar broke.
- Who is it for?
- Developers debugging malformed API responses, config files or webhook payloads that fail to parse, and who want to interpret the engine error instead of guessing.
- How DataFormatter's tool is different
- The JSON Validator runs the same parse with precise line-and-column output, so the fix is found in one click instead of eyeballing a long error string.
'Unexpected token' is the message you get when JavaScript's JSON.parse stops at a character it wasn't expecting to see. It reads like a roadblock sign: the parser was moving along and hit something it couldn't consume. The good news is the message names the token and the position — so the error is telling you exactly where to look.
- JSON.parse
- The JavaScript standard-library function that converts a JSON string into a JavaScript value, throwing a SyntaxError when the input isn't valid JSON.
- Token
- The smallest meaningful unit a parser reads — a brace, bracket, quote, comma, colon, or a literal like true. 'Unexpected token' names the token the parser rejected.
- Trailing comma
- A comma after the last element of an object or array. Legal in JavaScript objects and most JS arrays, but forbidden everywhere in JSON.
- Parser position
- The character offset and line/column the parser had reached when it failed — the reference point for the malformed construct.
What the message actually tells you
The phrase 'Unexpected token X' means: I was expecting a valid JSON token here, and I found X instead. When X is a quote, the failure is quoting; when X is a brace or bracket, it's structure; when X is an identifier like <, you're parsing something that isn't JSON at all.
Modern engines, newer messages
Since Chrome 123, V8 reports messages like Unexpected token '<', "<html>…" is not valid JSON — quoting the offending string so you can see the context at a glance. Firefox says JSON.parse: unexpected character at line … column … of the JSON data, and Safari says JSON Parse error: Unexpected token … without extra context. Each engine phrases the same failure slightly differently.
| Engine | Message shape | Example trigger |
|---|---|---|
| Chrome / V8 | Unexpected token '…', "…" is not valid JSON | Pasting HTML as the payload |
| Firefox | JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data | Missing quote on a key |
| Safari | JSON Parse error: Unexpected token '<' | HTML error page as payload |
The most common causes
- Trailing comma: { "a": 1, } or [1, 2,] — fine in most JavaScript, invalid in JSON.
- Single quotes: keys and strings that use ' instead of ".
- Unquoted keys: { name: "Ada" } instead of { "name": "Ada" }.
- Comments or NaN, Infinity, undefined — constructs JSON doesn't allow.
- HTML or an error page where the JSON response should be — check the content type.
Fixing trailing commas
A trailing comma usually appears on the last property or element. Because the parser reports the position just after the comma, the fix is almost always one line above the reported position — delete the comma on the previous line.
{
"name": "John",
"age": 30,
}{
"name": "John",
"age": 30
}Fixing 'HTML where JSON should be'
Unexpected token '<' with HTML in the message means the endpoint returned a document — a login wall, a 404 page or a CDN error — instead of JSON. No amount of JSON editing fixes this: verify the request with the API Client or Header Inspector, follow redirects, and confirm the Content-Type is application/json.
Using the validator to pinpoint the failure
- Paste the failing document into the JSON Validator.
- Read the report: it gives the line and column of the exact offender.
- Click the reported location to jump the editor to the character.
- Fix, re-validate, then format or minify the clean result.
Frequently asked questions
Why does JSON.parse reject things my JavaScript accepts?
JSON is a strict data interchange format: no single quotes, no unquoted keys, no trailing commas, no comments, no NaN or undefined. JavaScript objects allow all of these, which is exactly why a validator exists.
Why do the error messages differ between browsers?
Each engine implements its own JSON parser and reporting format. Chrome quotes the offending string, Firefox names line and column, Safari is terse. The underlying grammar rules are identical.
How do I find the position in a long minified document?
Paste it into the JSON Validator, which reports line and column, then click to jump. Formatting first with the JSON Formatter makes structure errors visible even before validation.
What does Unexpected token '<' with HTML in it mean?
The response body starts with a tag like <html> — the endpoint returned a document instead of JSON. Check the status code, redirects and Content-Type with the API Client.