Python 3.5+Exceptionsexception
json.JSONDecodeError
module exceptions
Malformed JSON while parsing: json.loads('{bad'). Carries the error position (pos/lineno/colno).
Syntax
raise json.JSONDecodeError
Usually the text being parsed is not JSON at all: an empty response body, an HTML error page, or a Python dict repr with single quotes, while JSON demands double quotes and forbids trailing commas. In the handler, the pos, lineno, colno and doc attributes pinpoint where parsing broke instead of merely saying that it did. It is a ValueError subclass, so a broad except ValueError catches it too; catch this class specifically when you need to tell malformed JSON apart from other failures.
Examples
import json
try:
json.loads('{bad')
except json.JSONDecodeError as e:
print(type(e).__name__) # → JSONDecodeError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy