Python
JSON
Data Parsing
Programming Errors
Troubleshooting

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.

Browse interview questions

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, and null

This is not valid JSON:

python
1import json
2
3text = "{'name': 'Ava', 'active': True}"
4json.loads(text)

It looks like a Python dictionary literal, not JSON. This version is valid:

python
1import json
2
3text = '{"name": "Ava", "active": true}'
4data = json.loads(text)
5print(data["name"])

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.

python
1import json
2
3text = '{"name": "Ava",}'
4
5try:
6    json.loads(text)
7except json.JSONDecodeError as exc:
8    print("message:", exc.msg)
9    print("line:", exc.lineno)
10    print("column:", exc.colno)

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.

python
1import json
2
3raw = b'{"city": "Montr\xc3\xa9al"}'
4text = raw.decode("utf-8")
5data = json.loads(text)
6print(data["city"])

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:

  1. decode bytes to text
  2. parse the decoded text as JSON

If the source is a file, specify the encoding when opening it:

python
1import json
2
3with open("data.json", "r", encoding="utf-8") as file:
4    data = json.load(file)

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 None and True
  • unescaped newline or control characters inside strings

For example, this fails because of the trailing comma:

python
1import json
2
3text = '{"items": [1, 2, 3,]}'
4json.loads(text)

And this fails because None is Python syntax, not JSON:

python
text = '{"middle_name": None}'

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:

python
1import ast
2
3text = "{'name': 'Ava', 'age': 30}"
4data = ast.literal_eval(text)
5print(data["name"])

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 true and null.
  • Read JSONDecodeError line 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.