json.JSONDecodeError is raised when json.loads() or json.load() receives a string that is not valid JSON. It is a subclass of ValueError and includes the position of the syntax error in the input, making it easier to debug malformed payloads.
Why it happens
- •Using single quotes instead of double quotes (JSON requires double quotes).
- •Trailing commas in objects or arrays, which are not permitted in JSON.
- •Receiving an HTML error page or empty string where JSON is expected.
- •A truncated response from a network request that was not fully received.
Examples
Code that triggers the error
import json
data = json.loads("{'key': 'value'}") # single quotes are not valid JSONError output
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
How to fix it
Always validate the raw response before parsing: check that it starts with { or [. Use try/except json.JSONDecodeError to handle malformed input gracefully. Log the raw string when a decode error occurs so you can inspect what was actually received.
Fixed code
import json
raw = '{"key": "value"}' # double quotes
try:
data = json.loads(raw)
print(data["key"])
except json.JSONDecodeError as e:
print(f"Invalid JSON at position {e.pos}: {e.msg}")Related errors
Ready to practice your English at work?
Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.
Try Lingua-e for free