"Unexpected Token in JSON": What It Means and How to Fix It

"Unexpected Token in JSON": What It Means and How to Fix It

“Unexpected token in JSON” means the parser hit a character it didn't expect, so the text is not valid JSON. Usually it is a small typo: a trailing comma, a single quote, or a missing bracket. Sometimes the server sent an HTML page instead of JSON. Here is how to find the exact cause in a minute.

Fastest way to find the error: paste the JSON into the free JSON Formatter & Validator and click Validate. It shows the line and column where the JSON breaks, so you don't have to count characters.

Open the JSON Formatter

What the error looks like

The wording depends on where you parse the JSON. Here is the same mistake, a trailing comma in {"a":1,}, plus the HTML case, in each environment:

WhereTypical message
Chrome, Edge, Node.jsExpected double-quoted property name in JSON at position 7 (line 1 column 8)
or, for HTML: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
FirefoxJSON.parse: expected double-quoted property name at line 1 column 8 of the JSON data
or, for HTML: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Python3.13 and newer: json.decoder.JSONDecodeError: Illegal trailing comma before end of object: line 1 column 7 (char 6)
older versions: Expecting property name enclosed in double quotes: line 1 column 8 (char 7)
PHPjson_decode() returns null, and json_last_error_msg() says Syntax error

The position or column number tells you where the parser gave up. The real mistake is usually just before that spot. Older Chrome versions said Unexpected token } in JSON at position 7; it means the same thing.

The 7 most common causes

1. A trailing comma

{ "name": "Ali", "age": 30, }

JSON doesn't allow a comma after the last item. Remove it.

2. Single quotes instead of double quotes

{ 'name': 'Ali' }

JSON keys and strings must use double quotes: { "name": "Ali" }.

3. Keys without quotes

{ name: "Ali" }

This is valid JavaScript, but not valid JSON. Every key needs double quotes.

4. The response is HTML, not JSON

If the error mentions Unexpected token '<', the first character is <. Your API returned an HTML page, usually a 404, 500 or login page. Open the request URL in a browser or check the Network tab to see what came back.

5. An empty response

Unexpected end of JSON input means the response was empty or cut off. Check that the server actually sends a body, and that you are not calling JSON.parse twice.

6. Comments in the JSON

JSON has no comments. Remove any // or /* */ lines. If you need comments in config files, consider YAML instead (see JSON vs XML vs YAML).

7. Special characters not escaped

A raw line break or a double quote inside a string breaks the JSON. Escape them as \n and \". Chrome reports this one as Bad control character in string literal.

A quick debugging checklist

  1. Log the raw text before parsing: console.log(await response.text()).
  2. Check the first character. { or [ is good. < means HTML.
  3. Paste the text into the JSON Formatter to see the line and column where it breaks.
  4. Fix the typo, or fix the server so it returns JSON with Content-Type: application/json.

Once it parses, How to Read a JSON API Response shows how to find the values you need. For more errors and formatting tips, see How to Format, Validate and Fix JSON.

Safe parsing in code

In JavaScript, wrap JSON.parse in try...catch so one bad response doesn't crash the page:

try {
  const data = JSON.parse(text);
} catch (err) {
  console.error('Invalid JSON:', err.message);
}

In PHP, use json_decode($text, true, 512, JSON_THROW_ON_ERROR) inside try...catch to get a clear JsonException instead of a silent null. In Python, catch json.JSONDecodeError, which has lineno and colno attributes.

Still stuck? Share the error and the raw response with someone who can help, with tokens removed first.

Paste JSON or drop a .json file. Validate shows the line and column of the first error, and Format indents it so the structure is easy to check.

Validate your JSON

Frequently asked questions

What does "Unexpected token < in JSON at position 0" mean?

The response starts with <, so it is HTML, not JSON. The server returned an error or login page. Newer Chrome versions word it as Unexpected token '<', "<!DOCTYPE "... is not valid JSON.

Can JSON have a trailing comma?

No. Standard JSON does not allow trailing commas in objects or arrays.

Why does my JSON work in JavaScript but not in JSON.parse?

JavaScript objects allow single quotes, unquoted keys and trailing commas. JSON does not.

How do I find the exact line with the error?

Paste it into a JSON validator such as the free JSON Formatter, which shows the line and column, or read the line and column in the Firefox or Python error message.

Related articles

← All guides