0
0
Pandasdata~20 mins

Reading JSON with read_json in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Reading Master
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 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)
A
   name  age
0  Alice   30
1    Bob   25
B
   0      1
0  name   age
1  Alice  30
2  Bob    25
C
   name  age
0  Alice  25
1  Bob    30
D
   age   name
0  30    Alice
1  25    Bob
Attempts:
2 left
💡 Hint
The JSON string is a list of dictionaries, so read_json will create a DataFrame with columns from keys.
data_output
intermediate
1: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)
A(3, 2)
B(6, 1)
C(1, 6)
D(2, 3)
Attempts:
2 left
💡 Hint
The JSON string is a dictionary with keys as columns and lists as values for rows.
🔧 Debug
advanced
2: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)
AKeyError: 'age'
BTypeError: string indices must be integers
CValueError: Expected object or value
DJSONDecodeError: Unterminated string starting at: line 1 column 22 (char 21)
Attempts:
2 left
💡 Hint
Look carefully at the JSON string syntax.
🚀 Application
advanced
2: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}}'
Apd.json_normalize(pd.read_json(json_str))
Bpd.read_json(json_str)
Cpd.json_normalize({'id': 1, 'name': 'Alice', 'scores': {'math': 90, 'english': 85}})
Dpd.read_json(json_str, orient='records')
Attempts:
2 left
💡 Hint
Use json_normalize to flatten nested JSON structures.
🧠 Conceptual
expert
1: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?
AIt controls the encoding type used to read the JSON file.
BIt specifies the format of the JSON string to correctly parse rows and columns.
CIt determines whether to convert JSON keys to lowercase.
DIt sets the compression algorithm for reading JSON files.
Attempts:
2 left
💡 Hint
Think about how JSON data can be structured differently.