Python JSON debugging

Python JSONDecodeError

Python raises json.JSONDecodeError when json.loads or json.load cannot decode strict JSON. The exception includes a message plus line, column and character position that can help locate malformed input.

Quick answer

Read the exception message together with line, column and char position, then inspect the surrounding input. “Expecting value” at line 1 column 1 often means the string is empty or not JSON at all.

Expecting property name enclosed in double quotes

The input often contains Python-dict syntax such as single-quoted strings or unquoted keys. Serialize Python objects with json.dumps instead of converting them with str().

Extra data

json.loads expects one JSON value. Concatenated objects or NDJSON can trigger Extra data because content remains after the first value. Parse line-delimited records individually when the source is NDJSON.

Requests and empty bodies

Calling response.json() on an empty or HTML response can raise a decode error. Inspect status_code, headers and response.text when the server behavior is uncertain.

Inspect a Python JSONDecodeError

import json

try:
    data = json.loads(text)
except json.JSONDecodeError as exc:
    print(exc.msg, exc.lineno, exc.colno, exc.pos)

Related JSON tools and guides

Frequently asked questions

What causes JSONDecodeError at line 1 column 1?

Often an empty string, whitespace-only input or a response body that is not JSON.

Can json.loads parse a Python dict string?

Only if the string also happens to be valid JSON. Python repr output commonly uses single quotes and is not valid JSON.

What does Extra data mean?

The decoder parsed one JSON value successfully but found additional non-whitespace content afterward.