Lingua-e
← Errors
Python

JSONDecodeError: What It Means and How to Fix It

Error reference

JSONDecodeError

Language

Python

Severity

Medium

When it happens

json.JSONDecodeError is raised when json.loads() or json.load() receives a string that is not valid JSON.

Potential fixes

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.

Deep dive

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.

Potential fixes

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.

Examples

Python

Code that triggers the error

import json

data = json.loads("{'key': 'value'}")  # single quotes are not valid JSON

Error output

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

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}")

Practice in English

How would you explain a JSONDecodeError to a fellow dev? Choose the right phrase:

"I hit an error...

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