Bird
Raised Fist0
Data Analysis Pythondata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of exporting data to JSON format in data analysis?
easy
A. To save data in a format that is easy to share and widely used
B. To make data unreadable for security
C. To convert data into an Excel spreadsheet
D. To compress data to reduce file size

Solution

  1. Step 1: Understand JSON format use

    JSON is a text format that is easy to share and read by many programs.
  2. Step 2: Identify the purpose of exporting

    Exporting to JSON helps save data in this easy-to-share format.
  3. Final Answer:

    To save data in a format that is easy to share and widely used -> Option A
  4. Quick Check:

    Exporting to JSON = Easy sharing [OK]
Hint: JSON is for sharing data simply and widely [OK]
Common Mistakes:
  • Thinking JSON is for data compression
  • Confusing JSON with Excel format
  • Believing JSON makes data unreadable
2. Which of the following is the correct way to export a pandas DataFrame df to a JSON file named data.json?
easy
A. df.write_json('data.json')
B. df.export_json('data.json')
C. df.to_json('data.json')
D. df.save_json('data.json')

Solution

  1. Step 1: Recall pandas export method

    The correct pandas method to export JSON is to_json.
  2. Step 2: Match method with filename

    Using df.to_json('data.json') saves the DataFrame to the file.
  3. Final Answer:

    df.to_json('data.json') -> Option C
  4. Quick Check:

    pandas export JSON = to_json() [OK]
Hint: Use df.to_json(filename) to save DataFrame as JSON [OK]
Common Mistakes:
  • Using non-existent methods like export_json
  • Confusing save_json or write_json with to_json
  • Forgetting to provide the filename as a string
3. Given the DataFrame df = pd.DataFrame({'A':[1,2], 'B':[3,4]}), what is the output of df.to_json(orient='records')?
medium
A. '{"A":{"0":1,"1":2},"B":{"0":3,"1":4}}'
B. '[{"A":1,"B":3},{"A":2,"B":4}]'
C. '{"0":{"A":1,"B":3},"1":{"A":2,"B":4}}'
D. '[1,2,3,4]'

Solution

  1. Step 1: Understand orient='records'

    This option converts each row to a JSON object in a list.
  2. Step 2: Convert DataFrame rows to JSON objects

    Rows become [{"A":1,"B":3}, {"A":2,"B":4}].
  3. Final Answer:

    '[{"A":1,"B":3},{"A":2,"B":4}]' -> Option B
  4. Quick Check:

    orient='records' = list of row dicts [OK]
Hint: orient='records' makes list of row dictionaries [OK]
Common Mistakes:
  • Confusing 'records' with default 'columns' orient
  • Expecting a nested dictionary instead of list
  • Misreading JSON string format
4. What is wrong with this code snippet for exporting a DataFrame df to JSON?
df.to_json('output.json', orient='wrong')
medium
A. It raises a ValueError because 'wrong' is not a valid orient
B. It will save the file but with empty content
C. It ignores the orient and uses default silently
D. It saves the file but with incorrect JSON format

Solution

  1. Step 1: Check valid orient options

    Valid options include 'records', 'columns', 'index', etc., but not 'wrong'.
  2. Step 2: Understand error behavior

    Using an invalid orient raises a ValueError immediately.
  3. Final Answer:

    It raises a ValueError because 'wrong' is not a valid orient -> Option A
  4. Quick Check:

    Invalid orient = ValueError [OK]
Hint: Invalid orient causes ValueError, check spelling [OK]
Common Mistakes:
  • Assuming invalid orient defaults silently
  • Expecting empty file instead of error
  • Thinking JSON format is saved anyway
5. You want to export a DataFrame df with nested dictionaries in a column to JSON, preserving the nested structure. Which orient option should you use to keep the nested data intact?
hard
A. 'records'
B. 'split'
C. 'index'
D. 'table'

Solution

  1. Step 1: Understand nested data export needs

    Nested dictionaries require a format that preserves complex structure.
  2. Step 2: Identify orient='table' features

    Orient='table' exports data with schema and supports nested structures well.
  3. Step 3: Compare other options

    'records' flattens rows, 'split' and 'index' do not preserve nested well.
  4. Final Answer:

    'table' -> Option D
  5. Quick Check:

    Nested data export = orient='table' [OK]
Hint: Use orient='table' to keep nested JSON structure [OK]
Common Mistakes:
  • Using 'records' which flattens nested data
  • Choosing 'split' or 'index' which lose nested info
  • Not specifying orient and losing structure
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard