ShareYourGroupShareYourGroupError Explainer

JavaScript SyntaxError: Unexpected token in JSON

A plain-words explanation of this JavaScript message: what it means, the usual causes, and the steps that fix it.

The messageUncaught SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON

Make the error yourself

Run in Google Chrome 157.0.8103.51. This is the smallest example we could find that shows it.

JavaScript (in a script tag)

const text = "<!DOCTYPE html><html><body>Not found</body></html>";
const data = JSON.parse(text);
console.log(data);

Browser console (the real message)

SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON

Why it happens

JSON.parse (and response.json() in fetch) was given text that is not valid JSON. The text of the message shows the first characters it received. When you see Unexpected token '<' and <!DOCTYPE, the server sent a web page, usually an error page (404 or 500) or a login page, instead of data. Other forms: "Unexpected end of JSON input" means the text was empty or cut off; a message about u or undefined means you parsed the value undefined; single quotes or a trailing comma are not allowed in JSON.

How to fix it

  1. Log the raw text first: const text = await response.text(); console.log(text.slice(0, 200));.
  2. Check response.ok and the status code before you parse.
  3. Check the address you are calling. A typo in the path often returns the site's HTML 404 page.
  4. In the Network tab of your browser, open the request and read the Response tab.
  5. Wrap the parse in try/catch so one bad answer does not stop the page.

The corrected version

JavaScript (in a script tag)

const text = "<!DOCTYPE html><html><body>Not found</body></html>";
try {
  console.log(JSON.parse(text));
} catch (err) {
  console.log("Not JSON:", text.slice(0, 20));
}

Browser console (the corrected version)

Not JSON: <!DOCTYPE html><html

How to avoid it

Always check response.ok before response.json(), and show a friendly message when the answer is not what you expected.

Questions

What does "Unexpected end of JSON input" mean?

The text was empty or stopped in the middle, for example when a request failed or when a file was cut off.

This is an independent guide. It is not connected with Python, Microsoft, Google, GitHub or the Git project. Product names belong to their owners. Examples were run on 13 November 2026 in a practice folder or a blank workbook; your version may word a message slightly differently.