Data Extraction: Parse A 3-Nested JSON Object and Convert It To A Pandas Dataframe
Data Extraction: Parse A 3-Nested JSON Object and Convert It To A Pandas Dataframe
Tejeswini
Search
Jun 6, 2021 · 4 min read · Listen
Follow
Matt Eland
Mohammad Alim
We can use the requests package for this purpose. To get the data as a JSON
output, we call the request.get method. The response variable in the below
code, stores the returned values, and the JSON output is parsed using the json( )
method.
What is a Nested-JSON ?
A JSON object is a collection of key and value pairs.
For example, { ‘data’ : [ {‘screen_ID’ : ‘63’, in this instance ‘data’ and ‘screen_ID’
are the keys, and ‘63’ is the value. Keys must be strings, and values must be a
valid JSON data type (string, number, object, array, boolean or null).
The API request to rapidapi.com produces a nested JSON output with Javascript
arrays, structured as follows:
Our problem statement here is to extract the keys and corresponding values of
‘color’, ‘date’, ‘high’, ‘low’, ‘open’, ‘perc_chg’, ‘price’, ‘vol’ to a data table format.
These values are contained in ‘screen_data.data’( To access nested fields,
concatenate the field names with a . (dot) as separator) column of the df,
dataframe, using below code we extract this column to a datatable:
dt stores df[‘screen_data.data’]
But here all the values are stored as a list. To transform each element of this list
to a row in the dataframe, we can use pd.dataframe.explode. This returns
exploded lists as rows of the subset columns; index will be duplicated for these
rows as below.
Now that we have a dictionary in each row of the datatable dt, we can use
pandas.Series to convert the dictionaries to a Pandas series format and apply
pandas.Dataframe to further convert it into type dataframe.
“Data extraction through API requests and web scraping is especially useful for
analyzing or automating data service on web-based applications.”
The JSON format is already compatible in itself and pandas functions add
extra flexibility in making the parsing and readability of this data more user-
friendly.
133
New Python content every day. Follow to join 500k+ monthly readers.