Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Reading JSON (read_json) in Data Analysis Python - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What does the pandas.read_json() function do in data analysis?
easy
A. It saves a DataFrame to a CSV file.
B. It converts a DataFrame into a JSON string.
C. It loads JSON data into a DataFrame for easy analysis.
D. It visualizes JSON data as a chart.

Solution

  1. Step 1: Understand the purpose of read_json

    The function pandas.read_json() is designed to read JSON formatted data and convert it into a DataFrame.
  2. Step 2: Compare options with function behavior

    Only It loads JSON data into a DataFrame for easy analysis. correctly describes this behavior. Options B, C, and D describe other unrelated functions.
  3. Final Answer:

    It loads JSON data into a DataFrame for easy analysis. -> Option C
  4. Quick Check:

    read_json loads JSON to DataFrame [OK]
Hint: Remember: read_json reads JSON into DataFrame [OK]
Common Mistakes:
  • Confusing read_json with to_json (writing JSON)
  • Thinking it saves data instead of loading
  • Assuming it creates visualizations
2. Which of the following is the correct syntax to read a JSON file named data.json into a DataFrame using pandas?
easy
A. df = pd.read_json(data.json)
B. df = pd.read_json('data.json')
C. df = pd.read_json("data.json", header=None)
D. df = pd.read_json[data.json]

Solution

  1. Step 1: Check correct function call syntax

    The function pd.read_json() requires the filename as a string argument inside parentheses.
  2. Step 2: Identify correct string usage

    df = pd.read_json('data.json') uses single quotes correctly. df = pd.read_json(data.json) misses quotes, causing a NameError. df = pd.read_json("data.json", header=None) uses an invalid header parameter (not supported by read_json). df = pd.read_json[data.json] uses square brackets which is invalid syntax.
  3. Final Answer:

    df = pd.read_json('data.json') -> Option B
  4. Quick Check:

    Use parentheses and quotes for filename [OK]
Hint: Use parentheses and quotes for file name in read_json [OK]
Common Mistakes:
  • Forgetting quotes around filename
  • Using square brackets instead of parentheses
  • Passing variable without defining it
  • Using invalid parameters like header= (confusing with read_csv)
3. Given the JSON string '{"name": ["Alice", "Bob"], "age": [25, 30]}', what will be the output of pd.read_json(json_str)?
medium
A. A DataFrame with columns 'name' and 'age' and two rows for Alice and Bob.
B. A DataFrame with two rows and columns named 0 and 1.
C. A Series object with keys 'name' and 'age'.
D. An error because JSON string is invalid.

Solution

  1. Step 1: Understand JSON structure

    The JSON string has keys 'name' and 'age' with lists as values, representing columns with two entries each.
  2. Step 2: Behavior of pd.read_json on this JSON

    By default, pd.read_json() interprets this as columns with lists of values, creating a DataFrame with columns 'name' and 'age' and two rows.
  3. Final Answer:

    A DataFrame with columns 'name' and 'age' and two rows for Alice and Bob. -> Option A
  4. Quick Check:

    JSON keys become columns, lists become rows [OK]
Hint: Keys become columns, lists become rows in DataFrame [OK]
Common Mistakes:
  • Thinking JSON keys become rows
  • Expecting a Series instead of DataFrame
  • Assuming JSON string is invalid
4. What is wrong with this code snippet?
import pandas as pd
json_str = '{"0": {"name": "Alice", "age": 25}, "1": {"name": "Bob", "age": 30}}'
df = pd.read_json(json_str)
print(df)
medium
A. The JSON string is missing quotes around keys.
B. The print statement is incorrect syntax.
C. pd.read_json cannot read JSON strings, only files.
D. The JSON string format requires orient='index' to read correctly.

Solution

  1. Step 1: Analyze JSON structure

    The JSON string has numeric keys '0' and '1' with nested dictionaries representing rows.
  2. Step 2: Understand read_json orient parameter

    By default, read_json expects columns orientation. Here, the data is oriented by index, so orient='index' is needed to parse correctly.
  3. Final Answer:

    The JSON string format requires orient='index' to read correctly. -> Option D
  4. Quick Check:

    Use orient='index' for nested dicts keyed by row labels [OK]
Hint: Use orient='index' for JSON keyed by row labels [OK]
Common Mistakes:
  • Ignoring orient parameter causing wrong DataFrame
  • Assuming JSON keys need quotes (they have quotes)
  • Thinking read_json can't read strings
  • Misreading print syntax
5. You have a JSON file where each entry is a list of values representing rows, like this:
[ ["Alice", 25], ["Bob", 30] ]
How do you read this JSON into a DataFrame with columns 'name' and 'age'?
hard
A. Use pd.read_json('file.json', orient='records') then assign columns manually.
B. Use pd.read_json('file.json', orient='values', columns=['name', 'age'])
C. Use pd.read_json('file.json', orient='split') with columns parameter.
D. Use pd.read_json('file.json', orient='records', names=['name', 'age'])

Solution

  1. Step 1: Understand JSON structure and orient options

    The JSON is a list of lists, which matches the 'records' orient but without keys for columns.
  2. Step 2: Read JSON and assign columns

    Using orient='records' reads the data as rows but columns are unnamed, so columns must be assigned manually after reading.
  3. Final Answer:

    Use pd.read_json('file.json', orient='records') then assign columns manually. -> Option A
  4. Quick Check:

    Assign columns manually when JSON has no keys [OK]
Hint: Assign columns after reading list-of-lists JSON [OK]
Common Mistakes:
  • Using non-existent orient values like 'values'
  • Expecting read_json to assign columns automatically
  • Confusing 'split' orient with list-of-lists JSON
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