Challenge - 5 Problems
JSON Reading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code reading JSON string?
Given the JSON string representing a list of records, what will be the output DataFrame?
Pandas
import pandas as pd json_str = '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]' df = pd.read_json(json_str) print(df)
Attempts:
2 left
💡 Hint
The JSON string is a list of dictionaries, so read_json will create a DataFrame with columns from keys.
✗ Incorrect
The JSON string is a list of objects with keys 'name' and 'age'. pandas.read_json parses it into a DataFrame with columns 'name' and 'age' and rows for each object.
❓ data_output
intermediate1:30remaining
How many rows and columns does this DataFrame have?
Using pandas.read_json on this JSON string, what is the shape of the resulting DataFrame?
Pandas
import pandas as pd json_str = '{"A": [1, 2, 3], "B": [4, 5, 6]}' df = pd.read_json(json_str) print(df.shape)
Attempts:
2 left
💡 Hint
The JSON string is a dictionary with keys as columns and lists as values for rows.
✗ Incorrect
The JSON string has keys 'A' and 'B' with lists of length 3. pandas.read_json creates a DataFrame with 3 rows and 2 columns.
🔧 Debug
advanced2:00remaining
What error does this code raise when reading JSON?
What error will this code raise when trying to read the JSON string?
Pandas
import pandas as pd json_str = '{"name": "Alice", "age": 30' df = pd.read_json(json_str)
Attempts:
2 left
💡 Hint
Look carefully at the JSON string syntax.
✗ Incorrect
The JSON string is missing a closing brace and quote, causing a JSONDecodeError for malformed JSON.
🚀 Application
advanced2:30remaining
Which option correctly reads nested JSON into a DataFrame?
Given this nested JSON string, which code correctly reads it into a flat DataFrame?
Pandas
json_str = '{"id": 1, "name": "Alice", "scores": {"math": 90, "english": 85}}'Attempts:
2 left
💡 Hint
Use json_normalize to flatten nested JSON structures.
✗ Incorrect
pd.read_json cannot flatten nested JSON directly. pd.json_normalize applied to a dict flattens nested fields into columns.
🧠 Conceptual
expert1:30remaining
What is the effect of the 'orient' parameter in pandas.read_json?
Which statement best describes the role of the 'orient' parameter in pandas.read_json?
Attempts:
2 left
💡 Hint
Think about how JSON data can be structured differently.
✗ Incorrect
The 'orient' parameter tells pandas how the JSON data is organized, such as 'records', 'columns', or 'index', so it can parse it properly.