An unexpected-token message means the parser encountered a character that cannot appear at the current point in a JSON document. The reported character is useful, but the real mistake is often a few characters earlier. A missing quote, comma or closing bracket changes what the parser expects next.

Start with strict JSON rules

JSON allows objects, arrays, strings, numbers, booleans and null. Object keys and strings must use double quotes. Comments, trailing commas, undefined, NaN and JavaScript expressions are not part of the JSON grammar. A payload that looks like a JavaScript object may therefore fail in JSON.parse.

The fastest first check is to identify the context of the reported token. A single quote usually indicates a JavaScript-style string. A closing brace may indicate a trailing comma. A letter near the start of a property often means the key is not quoted. An end-of-input error usually points to an unfinished string, object or array.

{ "name": "anvil", "ready": true }

Translate a character position into a line

Some runtimes report an absolute character position instead of a line and column. Count newline characters before that position to determine the line, then subtract the location of the most recent newline to estimate the column. A formatter that preserves the original input can do this without rewriting the broken document.

Inspect the entire logical value around the error rather than only the highlighted character. For example, a parser may highlight the first character of the next key when the preceding value is missing a comma. Repair the earliest definite problem, parse again, and repeat. Later errors may be side effects of the first one.

Check encoding and invisible characters

Text copied from documents or chat systems may contain typographic quotes, non-breaking spaces or a byte-order mark. Curly quotes are not string delimiters in JSON. Replace them only when you are certain they were intended as syntax rather than data inside a string.

Network responses can also be HTML disguised by an incorrect content type. If the first unexpected token is a less-than sign, inspect the raw response: an authentication gateway, proxy or error page may have returned HTML instead of JSON.

Repair carefully, then validate intent

Deterministic repair is useful for logs, copied configuration and generated text. It can close containers, quote keys and remove trailing commas. It cannot always infer what ambiguous input was meant to say. Keep the original beside the repaired result and compare important values.

After syntax repair, validate the payload against application rules. Valid JSON can still contain the wrong type, a missing business field or a dangerous value. Syntax validation answers whether the document can be parsed; schema and domain validation answer whether the application should accept it.

Practical takeaways

  • Fix the earliest definite syntax error first.
  • Use double quotes and remove JavaScript-only syntax.
  • Inspect raw network responses when the first token is “<”.
  • Validate repaired data against application rules.

How this guide was prepared

JSON Anvil guides are written for working developers, checked against reproducible examples, and reviewed for technical clarity. Tool output is tested locally; readers should still validate behavior in the exact runtime used by their application.