0
0
Data Analysis Pythondata~20 mins

Exporting to JSON in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Export 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 JSON export code?
Consider this Python code that exports a DataFrame to JSON format. What is the output string?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'name': ['Alice', 'Bob'], 'age': [25, 30]})
json_str = df.to_json(orient='records')
print(json_str)
A[{"name":"Alice","age":25},{"name":"Bob","age":30}]
B{"name":{"0":"Alice","1":"Bob"},"age":{"0":25,"1":30}}
C[{"0":"Alice","1":"Bob"},{"0":25,"1":30}]
D{"records":[{"name":"Alice","age":25},{"name":"Bob","age":30}]}
Attempts:
2 left
💡 Hint
The 'records' orient outputs a list of dictionaries, each dictionary is a row.
data_output
intermediate
1:30remaining
How many items are in the JSON array after export?
Given this DataFrame and export code, how many JSON objects are in the resulting JSON string?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'city': ['NY', 'LA', 'SF'], 'population': [8000000, 4000000, 900000]})
json_str = df.to_json(orient='records')
import json
json_data = json.loads(json_str)
print(len(json_data))
A1
B0
C2
D3
Attempts:
2 left
💡 Hint
Each row becomes one JSON object in the list.
🔧 Debug
advanced
2:00remaining
What error does this JSON export code raise?
This code tries to export a DataFrame with a datetime column to JSON. What error occurs?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'date': [pd.Timestamp('2023-01-01'), pd.Timestamp('2023-01-02')], 'value': [10, 20]})
json_str = df.to_json(orient='records')
print(json_str)
ANo error, outputs JSON string with dates as ISO strings
BTypeError: Object of type Timestamp is not JSON serializable
CValueError: Invalid orient parameter
DKeyError: 'date'
Attempts:
2 left
💡 Hint
Pandas handles datetime conversion automatically in to_json.
🚀 Application
advanced
2:00remaining
Which option exports DataFrame to JSON with indentation for readability?
You want to export a DataFrame to a JSON file with indentation (pretty print). Which code snippet does this correctly?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'x': [1, 2], 'y': [3, 4]})
Adf.to_json('file.json', indent=4)
Bdf.to_json('file.json', indent=4, orient='records')
Cdf.to_json('file.json', lines=True)
Ddf.to_json('file.json', pretty=True)
Attempts:
2 left
💡 Hint
to_json supports indent parameter and orient to control format.
🧠 Conceptual
expert
3:00remaining
Why might exporting a DataFrame with mixed data types to JSON cause issues?
Consider a DataFrame with columns of different types: numbers, strings, dates, and custom objects. What is the main challenge when exporting this DataFrame to JSON?
AJSON format does not support mixed data types in the same array
BDates are always lost when exporting to JSON
CCustom objects may not be serializable to JSON without conversion
DNumbers are converted to strings automatically causing data loss
Attempts:
2 left
💡 Hint
Think about what JSON can represent and what Python objects need special handling.