Challenge - 5 Problems
JSON Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this JSON serialization code?
Consider this Python code that serializes a dictionary to a JSON string. What will be printed?
Python
import json data = {'name': 'Alice', 'age': 30, 'is_student': False} json_str = json.dumps(data) print(json_str)
Attempts:
2 left
💡 Hint
Remember that JSON uses double quotes and lowercase true/false for booleans.
✗ Incorrect
The json.dumps function converts Python objects to JSON strings. It uses double quotes for keys and string values, and converts Python False to JSON false.
❓ Predict Output
intermediate2:00remaining
What is the value of 'data' after deserializing this JSON string?
Given this JSON string, what Python object results from deserializing it?
Python
import json json_str = '{"fruits": ["apple", "banana"], "count": 2}' data = json.loads(json_str) print(data)
Attempts:
2 left
💡 Hint
json.loads converts JSON strings to Python dictionaries and lists.
✗ Incorrect
The JSON string represents an object with a list and a number. json.loads converts it to a Python dict with a list and int.
❓ Predict Output
advanced2:00remaining
What error does this code raise when serializing?
What error will this code raise when trying to serialize the object to JSON?
Python
import json class Person: def __init__(self, name): self.name = name p = Person('Bob') json_str = json.dumps(p)
Attempts:
2 left
💡 Hint
json.dumps can only serialize basic Python types by default.
✗ Incorrect
Custom objects like Person are not serializable by default and cause a TypeError.
❓ Predict Output
advanced2:00remaining
What is the output of this code using json.loads with an invalid JSON string?
What happens when you run this code?
Python
import json invalid_json = '{"name": "Alice", age: 30}' data = json.loads(invalid_json)
Attempts:
2 left
💡 Hint
JSON keys must be double quoted strings.
✗ Incorrect
The key 'age' is not enclosed in double quotes, so json.loads raises JSONDecodeError.
🧠 Conceptual
expert3:00remaining
Which option correctly serializes a Python dictionary with non-string keys to JSON?
Given a Python dictionary with integer keys, which option produces a valid JSON string with string keys?
Python
import json data = {1: 'one', 2: 'two'}
Attempts:
2 left
💡 Hint
JSON keys must be strings. Python dict keys can be other types.
✗ Incorrect
Option C converts integer keys to strings before serializing, producing valid JSON. Option C raises TypeError. Option C is invalid syntax. Option C does not fix non-string keys.