Bird
Raised Fist0

Find the error in this code:

medium📝 Debug Q7 of Q15
Python - Structured Data Files
Find the error in this code:
import json
json_str = '{name: "John", age: 30}'
py_obj = json.loads(json_str)
Ajson.loads() cannot parse strings
BJSON keys must be in double quotes
CMissing import statement
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check JSON string format

    JSON requires keys and string values to be enclosed in double quotes. Here, keys are unquoted.
  2. Step 2: Understand json.loads() requirements

    Because keys are not quoted, json.loads() will raise a JSONDecodeError.
  3. Final Answer:

    JSON keys must be in double quotes -> Option B
  4. Quick Check:

    JSON keys need double quotes [OK]
Quick Trick: Always quote JSON keys with double quotes [OK]
Common Mistakes:
MISTAKES
  • Using single quotes for keys
  • Leaving keys unquoted
  • Confusing Python dict syntax with JSON syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes