Fixing “Unexpected token” Errors in JSON
Unexpected token means the parser reached a character that JSON syntax does not allow at that position. The common causes are single quotes, trailing commas, comments, unquoted keys, or an HTML error page returned where you expected JSON; validate the raw text, locate the reported offset, and correct the syntax.
Where the error comes from
The message looks technical, but most cases fall into a short list:
- Strings or property names wrapped in single quotes. JSON accepts double quotes only.
- A comma after the last item in an object or array.
//or/* */comments. The JSON specification does not allow comments.- Property names without quotes, or “smart quotes” and full-width punctuation copied from a document.
- Raw line breaks or tab characters inside a string.
- An API request that failed and returned an HTML login or error page, usually starting with
<.
The following text has three problems at once: a trailing comma, a missing comma, and single quotes. Passing it to JSON.parse throws immediately:
const badJSON = `{
"name": "hpjson",
"modes": ["format", "view", "diff",],
"offline": true
"local": 'yes'
}`;
try {
JSON.parse(badJSON);
} catch (error) {
console.error(error.message);
}
Find and fix the offending spot
Chrome reports a character offset, not a line number, for example Unexpected token ']' ... is not valid JSON. Paste the text into a formatter to highlight the first spot that cannot be parsed, then remove the trailing comma, add the missing separator, and replace single quotes with double quotes:
{
"name": "hpjson",
"modes": ["format", "view", "diff"],
"offline": true,
"local": "yes"
}
How to avoid it next time
- Generate JSON with
JSON.stringifyinstead of joining strings by hand. - Check the HTTP status and
Content-Type, and confirm the body starts with{or[. - Format and validate a config file before editing it, then validate again after.
- Use a format that supports comments when comments are genuinely needed.
- Inspect the raw response in the network panel so a login page or error page is not mistaken for data.
- When you own the API, return a consistent JSON error body so a failed request is still valid JSON.
FAQ
Is the reported position a line number? No. It is usually a character offset starting at zero. Count the newlines before that offset to convert it to a line and column.
The API is supposed to return JSON, so why does parsing fail? A failed request often returns HTML or plain text. Check the raw response and Content-Type before assuming the data itself is wrong.
Single quotes are valid in JavaScript, so why not in JSON? JSON has its own grammar. Strings and property names require double quotes; JavaScript object-literal rules do not apply.
Does a trailing comma really count as an error? Yes. Standard JSON forbids it. Some JavaScript engines tolerate it, but that tolerance is not part of JSON.
Does an online checker upload my data? No. hpjson.com processes data locally in the browser, parses it in a Web Worker, and does not upload file contents to a server.
When a parse error appears, run the raw text through the JSON formatter to find the exact location, and check the FAQ for related edge cases. The tool is free, currently has no ads, and supports both Chinese and English.