JSON errors

Python, from json.loads(), json.load() and requests .json()

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Python's json module looked for a JSON value at the very first character and found nothing usable. With line 1 column 1 (char 0) the text is either empty or starts with something like < from an HTML page. At a later position it usually means Python syntax such as True or None inside the JSON.

How each browser and language words it

Python json module
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
requests 2.27+
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
simplejson
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Paste your JSON to find the exact line

This is a proxy error page, the kind of body that makes response.json() fail at char 0. There's no JSON to repair, check the request.

Skip to editor
Explorer
Open Files

No files open

0 files open

jsoneditor.io v2

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>
JSONUTF-8
Spaces: 2

Causes and how to fix each one

1. The response is empty or HTML

requests' response.json() raises this when the body is empty or is an HTML error page, from a 404, a 500, a 502 from a proxy or a rate limit page. Check the status and the content type before decoding.

Python
import requests

response = requests.get("https://api.example.com/orders", timeout=10)
response.raise_for_status()

if "application/json" not in response.headers.get("Content-Type", ""):
    raise ValueError(f"Expected JSON, got: {response.text[:120]}")

data = response.json()

2. Reading an empty file or reading it twice

json.load(f) on an empty file raises this, and so does calling it a second time on the same file object, because the first call already read to the end. Check the file has content and only read it once.

3. Python literals inside the JSON

True, False and None are Python, JSON uses true, false and null. Text produced by print(dict) or str(dict) fails on the first of them, at whatever column it appears. Use json.dumps to write JSON, or Repair to convert existing text.

Questions people ask

What does char 0 mean in the error?

It is the index of the character where decoding failed, counting from zero. char 0 means the very first character, so the text was empty or never JSON.

How do I see what requests actually received?

Print response.status_code, response.headers.get("Content-Type") and the first part of response.text before calling response.json().

Tools for this