Reference

JSON Error Message Reference

Common JSON parse errors, likely causes and API debugging fixes.

Overview

JSON parse errors often happen after a request succeeds at the HTTP layer. The body may be empty, HTML, malformed JSON, JSON with comments or a truncated response. This reference connects common parser messages to the next debugging step.

Debugging Reference Table

Error messageLikely causeDebugging fix
Unexpected token <HTML returned instead of JSONCheck status, redirects, auth and Content-Type.
Unexpected end of JSON inputEmpty or truncated bodyCheck 204 responses, network failures and server logs.
Unexpected token }Extra closing brace or trailing comma nearbyFormat the JSON and inspect the previous key or array item.
Unexpected stringMissing comma between propertiesLook between adjacent string values or keys.
Bad control character in string literalUnescaped newline or control characterEscape text before inserting it into JSON strings.
Invalid escape characterBackslash used incorrectlyUse valid escapes such as \\, \" or \n.
JSON with commentsComments are not valid JSONRemove comments or use a JSONC-aware parser before conversion.
Large number changed after parsingJavaScript number precision limitTreat large IDs as strings when precision matters.

Reference Table Coverage

  • Parser messages seen in JavaScript.
  • HTML returned to JSON clients.
  • Empty responses and 204 handling.
  • Precision and escaping issues.

API Debugging Examples

  • A fetch call receives a login page and fails with Unexpected token <.
  • A delete endpoint returns 204 and response.json() throws Unexpected end of JSON input.
  • A copied config has trailing commas from a JavaScript object literal.

Common Mistakes

  • Assuming valid-looking JavaScript object syntax is valid JSON.
  • Parsing every successful response as JSON.
  • Ignoring Content-Type when the body starts with HTML.
  • Using numbers for IDs that exceed safe integer range.

FAQ

Why does JSON.parse fail on a response that looks valid?

Common hidden causes include trailing commas, invalid escapes, comments, empty body or characters before the JSON starts.

Why does Unexpected token < usually mean HTML?

HTML documents often start with <!doctype or <html, so the first unexpected JSON character is <.

Can JSON contain comments?

No. Some tools support JSONC, but standard JSON does not allow comments.