Flatten nested JSONs
A feature of JSON data is that it can be nested: an attribute's value can consist of attribute-value pairs. This nested data is more useful unpacked, or flattened, into its own dataframe columns. The pandas.io.json
submodule has a function, json_normalize()
, that does exactly this.
The Yelp API response data is nested. Your job is to flatten out the next level of data in the coordinates
and location
columns.
pandas
(as pd
) and requests
have been imported. The results of the API call are stored as response
.
This exercise is part of the course
Streamlined Data Ingestion with pandas
Exercise instructions
- Load the
json_normalize()
function frompandas
'io.json
submodule. - Isolate the JSON data from
response
and assign it todata
. - Use
json_normalize()
to flatten and load the businesses data to a dataframe,cafes
. Set thesep
argument to use underscores (_
), rather than periods. - Show the
data
head.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Load json_normalize()
____
# Isolate the JSON data from the API response
data = ____
# Flatten business data into a dataframe, replace separator
cafes = ____(data["businesses"],
____)
# View data
print(cafes.head())