Home »
Python »
Python Programs
How to flatten multilevel/nested JSON?
Learn, how to flatten multilevel/nested JSON in Python?
Submitted by Pranit Sharma, on November 30, 2022
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
A JSON file is just like a python dictionary which holds the elements in the form of a key:value pair. JSON allows programmers to store different data types in the form of human-readable code, with the keys representing the names and the values containing the related data to it.
Problem statement
Suppose we are given a multi-level or nested JSON file and we need to convert this file to CSV file so that we can use it for further analysis. Now the issue with this structure is we have nested dictionaries or lists when we convert our JSON file.
Flattening multilevel/nested JSON
We need to find a way so we can flatten the nested JSON file. For this purpose, we will use the pandas json.normalize() for method breaking and appending down the elements of the nested JSON file. This method requires a specific structure in which we want our data to be represented.
Let us understand with the help of an example,
Python program to flatten multilevel/nested JSON
# Importing pandas package
import pandas as pd
# Importing numpy package
import numpy as np
# Defining a JSON file
json = [
{
"state": "Florida",
"shortname": "FL",
"info": {"governor": "Rick Scott"},
"county": [
{"name": "Dade", "population": 12345},
{"name": "Broward", "population": 40000},
{"name": "Palm Beach", "population": 60000},
],
},
{
"state": "Ohio",
"shortname": "OH",
"info": {"governor": "John Kasich"},
"county": [
{"name": "Summit", "population": 1234},
{"name": "Cuyahoga", "population": 1337},
],
},
]
# Display JSON file
print("Nested JSON File:\n",json,"\n")
# Flattening this json file
res = pd.json_normalize(json, "county", ["state", "shortname", ["info", "governor"]])
# Display result
print("Result:\n",res)
Output
The output of the above program is:
Python Pandas Programs »