Bird
0
0

You receive a JSON string with nested data:

hard📝 Application Q9 of 15
Python - Structured Data Files
You receive a JSON string with nested data:
json_str = '{"user": {"name": "Sam", "age": 40}, "active": true}'

Which Python code correctly extracts the user's name after parsing?
Aimport json data = json.loads(json_str) print(data["name"])
Bimport json data = json.loads(json_str) print(data["user"]["name"])
Cimport json data = json.dumps(json_str) print(data["user"]["name"])
Dimport json data = json.load(json_str) print(data["user"]["name"])
Step-by-Step Solution
Solution:
  1. Step 1: Parse JSON string to Python dictionary

    json.loads converts the JSON string to a nested Python dict.
  2. Step 2: Access nested dictionary keys

    To get the user's name, access data["user"]["name"].
  3. Final Answer:

    import json data = json.loads(json_str) print(data["user"]["name"]) -> Option B
  4. Quick Check:

    Nested JSON access = data["user"]["name"] [OK]
Quick Trick: Use nested keys to access nested JSON data [OK]
Common Mistakes:
  • Trying to access top-level key 'name' directly
  • Using json.dumps instead of json.loads
  • Using json.load which reads files, not strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes