Skip to content
CheckcraftDownload
Practical workflow

JSON parse error: what it means and how to fix it

Checkcraft is a desktop app for checking APIs and logs without code, with a free edition. Download, no sign-up →

A JSON parse error means a program received text that was supposed to be JSON and could not read it. There are three kinds of causes: the text is empty, it is not JSON at all (often an HTML page), or the JSON has a syntax error. Below is how to tell which one you have and what to do.

Checkcraft ·

Get help with your first test →

What JSON parse error means

Parse means read and convert. The program reads the text character by character and builds an object. As soon as it meets something JSON cannot contain, parsing stops with an error. The message usually includes the position or line where it happened, and that is the main clue.

Three kinds of causes

Empty text: the server returned nothing or the file is empty, hence The document is empty and Unexpected end of JSON input. Not JSON: an HTML error or login page arrived, hence Unexpected token with a less-than sign at the start. Broken JSON: single quotes, a trailing comma, a comment, a truncated response.

JSON check diagram: syntax, required fields, types, values, extra fields

If someone else's app shows the error

If you see a JSON parse error in an app, a game or a website rather than your own code, the service usually returned an empty response or an error page. Retry later, check your connection and VPN, update the app, and for desktop programs delete the corrupted settings file so the program recreates it.

How to avoid it in your code

Check the status code and content type before parsing, handle an empty body separately and put the start of the received text into the error message. Instead of a cryptic JSON parse error, the log then shows that a 502 page or an empty response arrived.

API call diagram: method and URL, headers, body, status and response body, logs and database

How the error looks in different languages

WhereMessageUsually means
JavaScriptUnexpected token < in JSON at position 0HTML arrived instead of JSON
JavaScriptUnexpected end of JSON inputEmpty or truncated response
PythonJSONDecodeError: Expecting value: line 1 column 1Empty or not JSON from the first character
Java, JacksonJsonParseException: Unexpected characterAn invalid character in the text
1CUnexpected character when reading JSONHTML, BOM or empty body
AppsThe document is empty, Failed to parse JSON configEmpty response or corrupted settings file

Specific cases: Unexpected token, Unexpected end of JSON input, DOCTYPE is not valid JSON, the 1C error.

How to find the spot

  1. Look at the position or line and column in the message; the mistake is usually just left of it.
  2. Paste the text into a JSON validator: it shows the spot and the likely cause.
  3. If the first character is <, it is HTML: check the status code and the request URL, not the JSON.
  4. If the text is empty, there is nothing to parse: find out why the service or file returned nothing.

Protecting your code

const res = await fetch(url);
const text = await res.text();
if (!text.trim()) throw new Error(`Empty response, status ${res.status}`);
try {
  return JSON.parse(text);
} catch (e) {
  throw new Error(`JSON parse error: ${e.message}. Status ${res.status}, start: ${text.slice(0, 200)}`);
}

Such a handler turns a cryptic parse error into a message with the status code and the start of the text. The cause is visible at a glance.

More on JSON

Section overview and all guides: JSON: what the format is, syntax rules, examples and common errors.

Frequently asked questions

What does JSON parse error mean?

A program received text it had to read as JSON and could not. Usually the text is empty, an HTML page, or has a syntax error.

What does parse mean here?

Reading text and turning it into an object.

What if an app shows JSON parse error?

The service failed or a settings file is corrupted. Retry later, check the connection and VPN, update the app or delete the corrupted settings file.

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

RFC 8259: the JSON format · MDN: JSON.parse

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.