Python JSONDecodeError: Expecting Value, Extra Data and Other Fixes
Fix json.decoder.JSONDecodeError in Python: Expecting value at line 1 column 1, Extra data, Expecting property name enclosed in double quotes, and Invalid control character. Causes and code for each.
Find the exact error positionMatch the message to the cause
json.loads and json.load raise json.JSONDecodeError, a subclass of ValueError. The message names the problem and the position as line, column and char (a zero-based character offset). Look at the text around that offset first.
- Expecting value: line 1 column 1 (char 0): the text is empty or is not JSON at all.
- Extra data: line 1 column N: one valid JSON value was followed by more text.
- Expecting property name enclosed in double quotes: single-quoted keys, a trailing comma, or a Python dict printed with str().
- Invalid control character at: a raw newline or tab inside a string.
- Unterminated string starting at: a missing closing quote, often a truncated file.
- Unexpected UTF-8 BOM (decode using utf-8-sig): the file starts with a byte order mark.
Expecting value: line 1 column 1 (char 0)
The parser found nothing it could read at the very start. With requests this usually means the server returned an empty body, an HTML error page, or a redirect to a login page. With files it means an empty file or reading a file handle that was already consumed.
Print the status and the first part of the body before parsing. Newer versions of requests raise requests.exceptions.JSONDecodeError, which also inherits from ValueError, so one except clause catches both.
import requests
r = requests.get(url, timeout=10)
r.raise_for_status()
if "application/json" not in r.headers.get("Content-Type", ""):
raise ValueError(f"Expected JSON, got {r.status_code}: {r.text[:200]!r}")
data = r.json()Extra data: several JSON values in one string
json.loads accepts exactly one value. Extra data appears when a file holds JSON Lines (one object per line), two objects written back to back, or a stray character after the closing brace.
For JSON Lines, parse each line. If you control the writer, wrap the records in a single array instead.
import json
with open("events.jsonl", encoding="utf-8") as f:
events = [json.loads(line) for line in f if line.strip()]Expecting property name enclosed in double quotes
JSON keys must use double quotes and the last item cannot be followed by a comma. The classic cause is writing a dictionary with str(data) or print(data), which produces {'name': 'Ada', 'ok': True}. That is Python syntax, not JSON.
Write files with json.dump or json.dumps. If you must read text that really is a Python literal, ast.literal_eval parses it safely, but fix the producer instead when you can.
import json
with open("out.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2) # not f.write(str(data))Invalid control character and BOM errors
JSON strings cannot contain raw control characters such as a literal newline; they must be escaped as \n. json.loads(text, strict=False) accepts them, which is useful for reading messy logs, but the producer should be fixed to escape them.
For a file that starts with a BOM, open it with encoding="utf-8-sig", which removes the mark if present.
data = json.loads(text, strict=False) # tolerate raw newlines in strings
with open("data.json", encoding="utf-8-sig") as f: # strip a BOM
data = json.load(f)Put the example to work
Use your own sample, check the result, and copy or download it. The tool processes your data locally in your browser.
Find the exact error positionReference documentation