Expecting value: line 1 column 1 (char 0) in Python
Updated
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) means Python’s json module looked for a value at the first character and found nothing it recognizes. The text is empty, or it starts with something that isn’t JSON. From requests the same error reads requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0), and there it nearly always means the server didn’t send JSON. Python looked. There was nothing to find.
Read the position first
Expecting value is raised wherever a value should begin: at the start, after a colon, after a comma or [. The position tells you which case you have. char counts characters from 0; line and column count from 1, so char 0 is line 1, column 1. Checked on Python 3.9.6 and 3.14.10:
| Message ends with | What is at that spot | Next step |
|---|---|---|
line 1 column 1 (char 0), the text is empty | Nothing: an empty response or file, or a file object that was already read | Responses, files |
line 1 column 1 (char 0), the text is not empty | Not JSON: an HTML page (<), an error message, None, undefined | Unexpected token '<' |
line 2 column 1 (char 1) on a single line break | Whitespace only, such as the blank last line of a JSON Lines file | JSON Lines |
line 1 column 8 (char 7) in {"ok": True} | A Python literal where a JSON value belongs: True, None | Invalid literal |
line 1 column 10 (char 9) in {"name": 'Ada'} | A string in single quotes | Single quotes |
line 1 column 7 (char 6) in [1, 2,], Python 3.12 and older | A trailing comma. Python 3.13 and later report Illegal trailing comma before end of array instead | Trailing comma |
In the terms of RFC 8259, JSON-text = ws value ws: after optional whitespace a value must follow, one of false, null, true, an object, an array, a number or a string. Empty text has none, and <, T or ' start none of them.
With requests: look at the response first
response.json() checks neither the status nor the content type: it parses whatever came back. Print what came back:
import requests
r = requests.get(url, timeout=10)
print(r.status_code, r.headers.get("Content-Type"), len(r.content))
print(r.history, r.url)
print(r.text[:300])204and a length of0: there is no body. Don’t call.json()on a 204.200 text/htmlwith a non-emptyr.history: requests followed a redirect, usually to a login page.403 text/html: a firewall or bot check. Your script lacks a header, cookie or token that the browser sends.500,502or503with HTML or text: the server failed, and the body says how.200 application/jsonand the error anyway: the body is empty or broken. Use the position table above.
Since requests 2.27, requests.exceptions.JSONDecodeError inherits from both json.JSONDecodeError and requests.RequestException, so either except clause catches it, and it is still a ValueError. httpx 0.28.1 raises the plain json.JSONDecodeError. aiohttp 3.13.5 checks the content type first and raises ContentTypeError: 200, message='Attempt to decode JSON with unexpected mimetype: text/html; charset=utf-8' without parsing at all.
The fix is the same check as in any language: status and type before parsing. Run against a test server answering JSON, HTML, an HTML 500 and an empty 204:
import requests
def get_json(url, **kwargs):
r = requests.get(url, timeout=10, **kwargs)
ctype = r.headers.get("Content-Type", "")
if r.ok and "json" in ctype:
return r.json()
raise RuntimeError(f"{r.status_code} {ctype} from {r.url}: {repr(r.text[:200])}")With files: check what you actually opened
- An empty file.
os.path.getsize(path)is 0: something created the file and never wrote it, or a crash left it truncated. - A file you already read. After
f.read()the position is at the end, sojson.load(f)sees an empty string and fails atchar 0. Callf.seek(0)or parse the text you already have. - The wrong file. A relative path resolved against another working directory, or a web page saved as
.json: a GitHub link with/blob/in it downloads the page around the file, not the file. - A byte order mark has its own message,
Unexpected UTF-8 BOM (decode using utf-8-sig). Open the file withencoding="utf-8-sig"; see BOM.
import json
from pathlib import Path
path = Path("data.json")
raw = path.read_bytes()
print(path.resolve(), len(raw), raw[:80])
data = json.loads(raw.decode("utf-8-sig"))The three printed values answer which file was opened, how big it is and how it starts; decoding with utf-8-sig also drops a byte order mark if there is one. From the shell, python -m json.tool data.json prints the same message with the position, and Python 3.14 also accepts the shorter python -m json. See the json module documentation.
The same input in other languages
Python’s message is the vaguest of the common parsers: an empty body, a web page and a Python literal all produce Expecting value. Other languages name the character, which helps when all you have is a log line:
| Input | Python 3.9.6 and 3.14.10 | JavaScript, V8 (Node.js 25.1) | Go 1.25.3 |
|---|---|---|---|
| empty | Expecting value: line 1 column 1 (char 0) | Unexpected end of JSON input | unexpected end of JSON input |
| an HTML page | Expecting value: line 1 column 1 (char 0) | Unexpected token '<', "<!DOCTYPE "... is not valid JSON | invalid character '<' looking for beginning of value |
None | Expecting value: line 1 column 1 (char 0) | Unexpected token 'N', "None" is not valid JSON | invalid character 'N' looking for beginning of value |
{"ok": True} | Expecting value: line 1 column 8 (char 7) | Unexpected token 'T', "{"ok": True}" is not valid JSON | invalid character 'T' looking for beginning of value |
{"name": 'Ada'} | Expecting value: line 1 column 10 (char 9) | Unexpected token ''', "{"name": 'Ada'}" is not valid JSON | invalid character '\'' looking for beginning of value |
Prevent it
- Wrap HTTP calls in one function that checks the status and type before
.json(), likeget_jsonabove. - Write files atomically:
json.dumpinto a temporary file, thenos.replaceit over the old one. Readers never see an empty or half-written file. - Skip blank lines when reading JSON Lines:
if line.strip():. - In pipelines, check
returncodebefore parsingstdout: a failed command writes to stderr and leaves stdout empty. - Catch
json.JSONDecodeErrorwhere the text enters your program and loge.poswith the first 200 characters, so the report carries the evidence.
Try it
Paste the text into the validator to see the first bad character named, with its line and column. In a long document, the position finder turns char 18734 into a line and column and shows the spot.
Related: Expecting property name enclosed in double quotes, Unexpected token '<', Unexpected end of JSON input, and the list of JSON errors.
Expecting value FAQ
What does “Expecting value: line 1 column 1 (char 0)” mean?
Python found no JSON value at the very first character. The text is empty, or it starts with something that is not JSON, most often an HTML page from a server.
Why does response.json() fail when the request succeeded?
A 200 status says nothing about the body. It can be a login page after a redirect, or nothing at all. Print r.status_code, r.headers.get("Content-Type") and r.text[:300] before parsing.
How do I catch this error?
Catch json.JSONDecodeError; with requests, requests.exceptions.JSONDecodeError works too. Both are subclasses of ValueError, so older code that catches ValueError keeps working.
Is “char 0” the first character?
Yes. char is a zero-based index into the text, while line and column start at 1. The exception carries all three as e.pos, e.lineno and e.colno.