0
0
Pythonprogramming~20 mins

Serializing and deserializing JSON in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A{"name": "Alice", "age": 30, "is_student": True}
B{'name': 'Alice', 'age': 30, 'is_student': False}
C{"name": "Alice", "age": "30", "is_student": false}
D{"name": "Alice", "age": 30, "is_student": false}
Attempts:
2 left
💡 Hint
Remember that JSON uses double quotes and lowercase true/false for booleans.
Predict Output
intermediate
2: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)
A{'fruits': ['apple', 'banana'], 'count': 2}
B{"fruits": ["apple", "banana"], "count": 2}
C{fruits: ['apple', 'banana'], count: 2}
D['apple', 'banana', 2]
Attempts:
2 left
💡 Hint
json.loads converts JSON strings to Python dictionaries and lists.
Predict Output
advanced
2: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)
ATypeError: Object of type Person is not JSON serializable
BValueError: Invalid JSON format
CAttributeError: 'Person' object has no attribute 'name'
DNo error, outputs '{}'
Attempts:
2 left
💡 Hint
json.dumps can only serialize basic Python types by default.
Predict Output
advanced
2: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)
A{'name': 'Alice', 'age': 30}
Bjson.decoder.JSONDecodeError: Expecting property name enclosed in double quotes
CSyntaxError: invalid syntax
DTypeError: string indices must be integers
Attempts:
2 left
💡 Hint
JSON keys must be double quoted strings.
🧠 Conceptual
expert
3: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'}
Ajson.dumps(dict(map(str, data.items())))
Bjson.dumps(data)
Cjson.dumps({str(k): v for k, v in data.items()})
Djson.dumps(data, ensure_ascii=False)
Attempts:
2 left
💡 Hint
JSON keys must be strings. Python dict keys can be other types.