0
0
Data Analysis Pythondata~20 mins

Reading JSON (read_json) in Data Analysis Python - 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 reading a JSON file with nested objects?
Given the JSON file content:
{"name": "Alice", "details": {"age": 30, "city": "New York"}}

What will be the output of pd.read_json('file.json')?
Data Analysis Python
import pandas as pd
from io import StringIO
json_data = '{"name": "Alice", "details": {"age": 30, "city": "New York"}}'
df = pd.read_json(StringIO(json_data), typ='series')
print(df)
ADataFrame with columns 'name' and 'details', where 'details' column contains dictionaries
BRaises ValueError: Expected object or value
CDataFrame with columns 'name', 'age', and 'city' flattened automatically
DEmpty DataFrame with no columns
Attempts:
2 left
💡 Hint
Think about how pandas handles nested JSON objects by default.
data_output
intermediate
1:30remaining
How many rows are in the DataFrame after reading a JSON array?
Given this JSON array:
[{"id": 1, "score": 88}, {"id": 2, "score": 92}, {"id": 3, "score": 75}]

What is the number of rows in the DataFrame after pd.read_json()?
Data Analysis Python
import pandas as pd
from io import StringIO
json_data = '[{"id": 1, "score": 88}, {"id": 2, "score": 92}, {"id": 3, "score": 75}]'
df = pd.read_json(StringIO(json_data))
print(len(df))
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
Each object in the JSON array becomes one row.
🔧 Debug
advanced
2:00remaining
What error occurs when reading malformed JSON with pd.read_json?
What error will this code raise?
import pandas as pd
from io import StringIO
json_data = '{"name": "Bob", "age": 25,'
df = pd.read_json(StringIO(json_data))
Data Analysis Python
import pandas as pd
from io import StringIO
json_data = '{"name": "Bob", "age": 25,'
df = pd.read_json(StringIO(json_data))
ANo error, returns empty DataFrame
BValueError: Expected object or value
CTypeError: read_json() missing 1 required positional argument
DJSONDecodeError: Expecting property name enclosed in double quotes
Attempts:
2 left
💡 Hint
Look at the JSON string carefully for syntax issues.
🚀 Application
advanced
2:00remaining
How to read a JSON file with multiple JSON objects separated by newlines?
You have a file where each line is a separate JSON object:
{"id": 1, "val": 10}
{"id": 2, "val": 20}
{"id": 3, "val": 30}

Which option correctly reads this file into a DataFrame?
Apd.read_json('file.json')
Bpd.read_json('file.json', chunksize=1)
Cpd.read_json('file.json', orient='records')
Dpd.read_json('file.json', lines=True)
Attempts:
2 left
💡 Hint
Look for the parameter that handles JSON lines format.
🧠 Conceptual
expert
1:30remaining
What is the effect of the 'typ' parameter in pd.read_json?
What does setting typ='series' do when reading JSON with pd.read_json()?
AReturns a pandas Series instead of a DataFrame
BReturns a DataFrame with only one column
CRaises an error because 'typ' is not a valid parameter
DFlattens nested JSON objects automatically
Attempts:
2 left
💡 Hint
Think about the difference between Series and DataFrame in pandas.