Bird
0
0

What is wrong with this Python code that tries to convert a Python dict to JSON?

medium📝 Debug Q7 of 15
Python - Structured Data Files
What is wrong with this Python code that tries to convert a Python dict to JSON?
import json
data = {"name": "Eve", "age": 28}
json_str = json.dumps(data["name"])
print(json_str)
Ajson.dumps cannot convert strings
BIt only converts the value of 'name', not the whole dictionary
CThe dictionary keys must be integers
Djson.dumps requires a list, not a dict
Step-by-Step Solution
Solution:
  1. Step 1: Analyze what is passed to json.dumps

    data["name"] is a string "Eve", so only this string is converted to JSON.
  2. Step 2: Understand the intended conversion

    To convert the whole dictionary, json.dumps(data) should be used instead.
  3. Final Answer:

    It only converts the value of 'name', not the whole dictionary -> Option B
  4. Quick Check:

    json.dumps argument controls output [OK]
Quick Trick: Pass whole dict to json.dumps to convert all data [OK]
Common Mistakes:
  • Thinking json.dumps can't convert strings
  • Believing keys must be integers
  • Assuming json.dumps only accepts lists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes