Why can't Python parse this JSON data?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When Python refuses to parse JSON, the parser is usually doing exactly what it should: rejecting input that is not valid JSON. In practice, the failure almost always comes from one of three places: invalid JSON syntax, wrong text decoding, or confusing JSON with Python or JavaScript syntax.
The fastest way to debug the problem is to stop treating the payload as a blob and inspect it systematically. Look at the actual text, read the exception location, and verify the format before trying any workaround.
JSON Is a Specific Format, Not "Anything Object-Like"
Many parsing errors happen because the data looks close enough to JSON that people assume it should work. But JSON has strict rules:
- object keys use double quotes
- string values use double quotes
- trailing commas are not allowed
- valid literal values are
true,false, andnull
This is not valid JSON:
It looks like a Python dictionary literal, not JSON. This version is valid:
That distinction explains a large percentage of "why can't Python parse this" questions.
Let JSONDecodeError Point to the Problem
Python's json module usually tells you where parsing failed. Use that information instead of scanning the whole string manually.
If the input is large, the line and column values save a lot of time. They narrow the problem to one specific area, which is often enough to reveal:
- a trailing comma
- a missing quote
- an extra brace
- malformed escaping
Decode Bytes Before Parsing Text
Sometimes the JSON syntax is fine, but the input bytes were decoded incorrectly before json.loads ever saw them.
If you decode with the wrong encoding, you might get:
- a
UnicodeDecodeError - corrupted text
- control characters that make parsing fail later
That means the correct order is:
- decode bytes to text
- parse the decoded text as JSON
If the source is a file, specify the encoding when opening it:
Common Input That Is Not Valid JSON
Here are some frequent troublemakers:
- single quotes instead of double quotes
- comments
- trailing commas
- Python literals such as
NoneandTrue - unescaped newline or control characters inside strings
For example, this fails because of the trailing comma:
And this fails because None is Python syntax, not JSON:
Valid JSON would use null.
Use the Right Parser for the Actual Format
Sometimes the data simply is not JSON. It might be:
- a Python literal
- a JavaScript snippet
- a log line that only contains a JSON fragment
- some custom text format
If the input is genuinely a Python literal, ast.literal_eval may be the right tool:
That does not make the text JSON. It only means you picked the parser that matches the real format. Avoid forcing json.loads onto data that was never JSON in the first place.
Common Pitfalls
The biggest mistake is assuming anything that looks like an object is JSON. Python dictionaries, JavaScript object literals, and JSON are similar, but they are not interchangeable.
Another common issue is focusing only on punctuation when the real problem is decoding. If the bytes are broken before they become text, JSON syntax checking is not yet the main issue.
Developers also sometimes use eval as a shortcut when json.loads fails. That is unsafe and unnecessary. If the format is wrong, fix the format or use a safe parser that matches it.
Finally, be careful with payloads copied from browser consoles, logs, or debugging output. Those sources often contain quoting or escaping that is not valid JSON anymore.
Summary
- Python usually fails to parse JSON because the input is not valid JSON, not because the parser is broken.
- Valid JSON requires double quotes, no trailing commas, and JSON literals such as
trueandnull. - Read
JSONDecodeErrorline and column details instead of guessing. - Decode bytes correctly before parsing text.
- If the data is actually another format, use the parser that matches that format instead of forcing
json.loads.
Related reading
- Why can't Python parse this JSON data?
- Why can't Python's raw string literals end with a single backslash?
- Why CIFAR-10 images are not displayed properly using matplotlib?
- Why compile Python code?
- Why can't you look at messages in the Rabbit Queue
- Why check this null?
- Why dict.get(key) instead of dict[key]?
- Why dict.getkey instead of dictkey?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.