Challenge - 5 Problems
JSON Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Reading JSON data from a file
What is the output of this Python code that reads a JSON file and prints the data?
Python
import json with open('data.json', 'r') as file: data = json.load(file) print(data)
Attempts:
2 left
💡 Hint
Remember json.load reads JSON and converts it to Python objects.
✗ Incorrect
json.load reads the JSON file and converts it to a Python dictionary. The print statement outputs the dictionary.
❓ Predict Output
intermediate2:00remaining
Writing Python dictionary to JSON file
What will be the content of 'output.json' after running this code?
Python
import json data = {'city': 'Paris', 'population': 2148327} with open('output.json', 'w') as f: json.dump(data, f)
Attempts:
2 left
💡 Hint
json.dump writes JSON formatted string to file with double quotes.
✗ Incorrect
json.dump converts the dictionary to a JSON string with double quotes and writes it to the file.
❓ Predict Output
advanced2:00remaining
Handling JSON decoding errors
What error will this code raise when trying to load invalid JSON content?
Python
import json invalid_json = '{"name": "Bob", age: 25}' data = json.loads(invalid_json)
Attempts:
2 left
💡 Hint
JSON keys must be in double quotes.
✗ Incorrect
The JSON string is invalid because the key 'age' is not in double quotes, causing JSONDecodeError.
❓ Predict Output
advanced2:00remaining
Using json.dumps with indentation
What is the output of this code?
Python
import json data = {'fruit': 'apple', 'count': 5} result = json.dumps(data, indent=2) print(result)
Attempts:
2 left
💡 Hint
indent=2 adds line breaks and spaces for readability.
✗ Incorrect
json.dumps with indent=2 formats the JSON string with new lines and two spaces indentation.
🧠 Conceptual
expert2:00remaining
Understanding JSON serialization of custom objects
What happens when you run this code?
Python
import json class Person: def __init__(self, name): self.name = name p = Person('Eve') 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, causing TypeError.