Skip to content
CheckcraftDownload
Practical workflow

Unexpected token is not valid JSON: what it means and how to find it

The error says the text you are parsing as JSON is not JSON. Look at the character after Unexpected token: it almost always tells you the cause right away.

Checkcraft ·

Get help with your first test →

What the character in the message means

If it is < followed by <!DOCTYPE or <html, the server returned an HTML page instead of JSON: an error, a login page or the site home page. If the message says [object Object] or the older Unexpected token o, an object was passed to JSON.parse. If it says undefined, the variable holding the response is empty. Unexpected end of JSON input means the response is empty or cut off. Expected property name or Unexpected token } means the JSON itself has a trailing comma, single quotes or an unquoted field name.

How to find the spot quickly

The browser message usually includes a position or a line and column. Paste the text into the JSON validator on this site: it shows the line, the character and the cause. If the error happened on an API request, open the browser developer tools, the Network tab, the request and its Response tab to see what the server actually sent.

How to avoid it in code

Before calling response.json(), check the status code and the Content-Type header. Then instead of a cryptic parse error you get a clear message: the server answered 404 or returned HTML.

const response = await fetch('/api/orders/42');
const type = response.headers.get('content-type') || '';
if (!response.ok || !type.includes('application/json')) {
  const text = await response.text();
  throw new Error(`HTTP ${response.status}, ${type}: ${text.slice(0, 200)}`);
}
const order = await response.json();

If the error is in Postman

In Postman the parse error appears when a test calls pm.response.json() and the response is not JSON. Check the status code and the Body tab: it is often a server error page or an empty body. A Checkcraft report shows the same thing at once: status, headers and body next to the rules that did not match.

Next step

Repeat this on your own API

Checkcraft is a desktop app that saves API response checks without code and shows what did not match. Start with a sample, then use your own sanitized data. Free runs on Windows and Linux with no time limit.

Download FreeDownload and first run

No sign-up or email needed, the archive is ready in a couple of minutes. See the edition limits.

References

MDN: JSON.parse errors

Start with the linked example and compare its result with your own test requirement.

Ask a person

Get help with your first test

Tell us what you want to test or where you got stuck. We can help you start Checkcraft, choose an example and understand your first run. You can ask before downloading.

The Telegram group is public: do not post passwords, tokens or confidential work data. A description of your task is enough to start.

You can also email us support@checkcraft.ru

No account required. We will reply by email. This is a support request, not a newsletter subscription.