What is the purpose of exporting data to JSON format?
Exporting data to JSON allows you to save data in a simple, text-based format that is easy to share and use in many applications, especially for web and data exchange.
Click to reveal answer
beginner
Which Python library is commonly used to export data to JSON?
The pandas library is commonly used to export data frames to JSON using the to_json() method.
Click to reveal answer
intermediate
What does the orient parameter in to_json() control?
The orient parameter controls the format of the JSON output, such as 'records', 'split', 'index', or 'columns', affecting how data is structured in the JSON file.
Click to reveal answer
beginner
How do you export a pandas DataFrame named df to a JSON file called data.json?
Use df.to_json('data.json') to save the DataFrame as a JSON file named data.json in the current folder.
Click to reveal answer
intermediate
Why might you choose the 'records' orient when exporting to JSON?
The 'records' orient exports each row as a separate JSON object in a list, which is easy to read and commonly used for APIs and data exchange.
Click to reveal answer
Which method is used to export a pandas DataFrame to JSON?
Ato_json()
Bto_csv()
Cread_json()
Djson_export()
✗ Incorrect
The to_json() method is used to export pandas DataFrames to JSON format.
What type of file is JSON?
AAudio file
BBinary file
CImage file
DText file
✗ Incorrect
JSON is a text-based file format used to store and exchange data.
Which orient option exports JSON as a list of row objects?
Arecords
Bindex
Ccolumns
Dsplit
✗ Incorrect
The 'records' orient exports each row as a separate JSON object in a list.
What happens if you call df.to_json() without a file path?
AIt saves to a default file
BIt throws an error
CIt returns a JSON string
DIt prints the DataFrame
✗ Incorrect
Without a file path, to_json() returns the JSON data as a string.
Why is JSON a popular format for data exchange?
AIt is binary and compact
BIt is human-readable and widely supported
CIt only works with Python
DIt requires special software to read
✗ Incorrect
JSON is easy to read and supported by many programming languages and tools.
Explain how to export a pandas DataFrame to a JSON file and describe the role of the 'orient' parameter.
Think about how you save data and how you can change the shape of the JSON output.
You got /4 concepts.
Describe why JSON is useful for sharing data and how it compares to other formats like CSV.
Consider how data looks and what kinds of data structures each format can handle.
You got /4 concepts.
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
Step 1: Understand JSON format use
JSON is a text format that is easy to share and read by many programs.
Step 2: Identify the purpose of exporting
Exporting to JSON helps save data in this easy-to-share format.
Final Answer:
To save data in a format that is easy to share and widely used -> Option A
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
Step 1: Recall pandas export method
The correct pandas method to export JSON is to_json.
Step 2: Match method with filename
Using df.to_json('data.json') saves the DataFrame to the file.
Final Answer:
df.to_json('data.json') -> Option C
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
Step 1: Understand orient='records'
This option converts each row to a JSON object in a list.
Step 2: Convert DataFrame rows to JSON objects
Rows become [{"A":1,"B":3}, {"A":2,"B":4}].
Final Answer:
'[{"A":1,"B":3},{"A":2,"B":4}]' -> Option B
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
Step 1: Check valid orient options
Valid options include 'records', 'columns', 'index', etc., but not 'wrong'.
Step 2: Understand error behavior
Using an invalid orient raises a ValueError immediately.
Final Answer:
It raises a ValueError because 'wrong' is not a valid orient -> Option A
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
Step 1: Understand nested data export needs
Nested dictionaries require a format that preserves complex structure.
Step 2: Identify orient='table' features
Orient='table' exports data with schema and supports nested structures well.
Step 3: Compare other options
'records' flattens rows, 'split' and 'index' do not preserve nested well.
Final Answer:
'table' -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.