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

An unexpected token JSON error usually means the parser reached a character or value it did not expect while reading JSON. The input may have invalid JSON syntax, or it may not be JSON at all.

By Archana Bhandari ·

A quick example

INVALID JSON: after a trailing comma, the parser reaches } where it expected another property. But not every unexpected-token error means a comma or quote is wrong: input beginning with <!DOCTYPE html> is usually an HTML page being parsed as JSON. If you have the input, validate it first; JSONPlease processes it locally in your browser.

{
  "name": "Rahul",
}

What does unexpected token in JSON mean?

A JSON parser reads input according to JSON rules. A token is simply a meaningful part of that input, often a character or value. When it finds something that does not belong in that position, parsing stops. Exact wording differs between browsers, JavaScript runtimes, and libraries.

What does at position 0 mean?

Position 0 is the very beginning of the input. If parsing fails there, inspect the first character or value. Valid JSON can start with {, [, a double quote, a number, true, false, or null. It does not have to start with an object or array. If it starts with <, it may be HTML.

VALID top-level JSON values:
{"name":"Rahul"}
["Delhi", "Mumbai"]
"hello"
42
true
null

Unexpected token '<' in JSON

HTML, not JSON: this is a very common cause. Your application expected JSON but received an HTML page instead. Changing commas in JavaScript will not fix it; inspect the actual HTTP response and endpoint.

<!DOCTYPE html>
<html>
  <body>404 Not Found</body>
</html>
  • Wrong API URL or a 404 page.
  • A 500 server error page.
  • A login, access-denied, proxy, CDN, or WAF page.
  • A server configured to return HTML for an error.

Expected JSON vs received HTML

VALID JSON expected by the app:

{
  "status": "success"
}

Unexpected token 'u' in JSON

A common possible cause is parsing undefined, or text that became "undefined". undefined is a JavaScript value, not valid JSON. JSON has null but not undefined. The exact token message varies by environment, so inspect the actual value before deciding the cause.

JAVASCRIPT, not JSON:
JSON.parse(undefined);

VALID JSON alternative when no value is available:
null

Unexpected token 'o' in JSON

A common possible cause is trying to parse a JavaScript object instead of a JSON string. user below is already an object, so JSON.parse() is not needed. Exact error wording varies by browser and runtime.

JAVASCRIPT, not JSON:
const user = { name: "Rahul" };
JSON.parse(user); // incorrect

const json = '{"name":"Rahul"}';
const parsedUser = JSON.parse(json); // correct

Unexpected token because of a trailing comma

INVALID JSON: standard JSON does not allow the comma after the last property.

{
  "name": "Rahul",
  "city": "Dehradun",
}

Trailing comma fixed

VALID JSON: remove the final comma.

{
  "name": "Rahul",
  "city": "Dehradun"
}

Single quotes and unquoted property names

INVALID JSON: both examples use JavaScript-style syntax that strict JSON rejects. JSON requires double quotes around keys and string values.

{ 'name': 'Rahul' }

{ name: "Rahul" }

Correct JSON quotes

VALID JSON:

{
  "name": "Rahul"
}

Unexpected token because of comments

INVALID JSON: standard JSON has no comments. Remove the comment instead of trying to parse it.

{
  "name": "Rahul",
  // User city
  "city": "Dehradun"
}

Unexpected token because of extra text

INVALID input: a parser expecting one JSON document cannot accept Success: before it. Debug output, warnings, or server messages before JSON can corrupt an API response. PHP notices are one practical example, but any server-side output can cause this.

Success:
{
  "status": true
}

Extra text fixed

VALID JSON: return only the JSON document.

{
  "status": true
}

Unexpected token because of bad escaping

INVALID JSON: the inner quotes close the message too early. Escape double quotes that are part of a string with a backslash.

{
  "message": "He said "Hello""
}

Escaped quotes fixed

VALID JSON:

{
  "message": "He said \"Hello\""
}

How to fix unexpected token in JSON step by step

Read the complete error and note the token and position. Inspect the actual input instead of assuming it is JSON. Check its first character, then inspect the API status, response body, and content type where applicable. Validate it, format minified data for inspection, correct or repair malformed JSON, and retry parsing only after confirming the input is appropriate.

How to debug unexpected token with fetch()

JAVASCRIPT, not JSON: when debugging an unknown response, read the raw text before calling response.json(). It can reveal JSON, HTML, an empty response, or an error message. Production code should handle response status and content appropriately rather than blindly parsing every response. Do not log sensitive response data.

const response = await fetch("/api/user");
const text = await response.text();

console.log(response.status);
console.log(response.headers.get("content-type"));
console.log(text);

Why valid JSON can still lead to an unexpected token error

The JSON you are looking at may be valid while the actual runtime response is different. For example, the object below is valid JSON, but a warning printed before it makes the complete response invalid. Inspect the complete response, not just its last JSON-looking lines.

Warning: Database connection issue
{
  "status": "success"
}

Unexpected token vs JSON parse error

A JSON parse error is a broad description of parsing failure. Unexpected token is one kind of parse or syntax error: the parser encountered something it did not expect.

Unexpected token vs unexpected end of JSON input

Unexpected token means the parser found something wrong. Unexpected end means it expected more data but the input ended, often because JSON is incomplete. For example, this is INVALID JSON because it stops too soon.

{
  "name": "Rahul",

Quick troubleshooting table

Unexpected token < — possible cause: HTML instead of JSON — first check: inspect the API response. Unexpected token u — possible cause: undefined or non-JSON input — first check: inspect the value before JSON.parse(). Unexpected token o — possible cause: an existing object — first check: check the value type. Token near a comma or bracket — possible cause: JSON syntax — first check: validate the complete input. Position 0 — input fails immediately — first check: inspect the beginning of the response.

Quick checklist

Work through this list before changing unrelated code.

  • Is the response actually JSON?
  • Does it start with < or contain an error page?
  • Did the server return a 404, 500, or login page?
  • Is the response empty?
  • Are you passing undefined or an existing JavaScript object?
  • Are JSON keys and strings in double quotes?
  • Is there a trailing comma, comment, extra text, or bad escaping?
  • Does the JSON Validator accept the complete input?

Frequently asked questions

What does unexpected token in JSON mean? The parser found input that does not fit JSON rules at that position.

What does position 0 mean? Parsing failed at the very beginning of the input.

Why do I get unexpected token < in JSON? An HTML page was likely returned where JSON was expected.

Why do I get unexpected token u or o? Common causes include undefined input or trying to parse an existing object; check the actual value because wording varies.

How do I find an unexpected token? Inspect the reported position and the complete input, then validate it.

Can valid JSON still produce a parsing error? Yes, if the actual runtime response includes HTML, warnings, or other text.

How can I check whether JSON is valid? Paste the complete input into a JSON validator.

Related JSON tools