Bird
Raised Fist0

What will be the output of this code snippet?

medium📝 Predict Output Q5 of Q15
Python - Structured Data Files

What will be the output of this code snippet?

import json

json_data = '{"city": "Paris", "population": 2148327}'
data = json.loads(json_data)
print(data['city'])

A{'city': 'Paris', 'population': 2148327}
B2148327
CParis
DError
Step-by-Step Solution
Solution:
  1. Step 1: Parse JSON string to Python dict

    json.loads() converts JSON string to Python dictionary.
  2. Step 2: Access dictionary value by key

    data['city'] accesses the value 'Paris'.
  3. Final Answer:

    Paris -> Option C
  4. Quick Check:

    json.loads() parses string to dict, access by key [OK]
Quick Trick: Use json.loads() to parse JSON string to dict [OK]
Common Mistakes:
MISTAKES
  • Trying to print whole dict as string
  • Accessing wrong key

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes