HPJson HighPerformanceJson

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:

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

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.