Open In App

How to Parse Nested JSON in Python

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a nested JSON object and our task is to parse it in Python. In this article, we will discuss multiple ways to parse nested JSON in Python using built-in modules and libraries like json, recursion techniques and even pandas.

What is Nested JSON

Nested JSON refers to a JSON object that contains another JSON object (or an array of objects) inside it.

Example:

{
"name": "John",
"age": 30,
"address": {
"city": "New York",
"zipcode": "10001"
}
}

In the above example, address is a nested JSON object.

In this article, we will discuss multiple ways to parse nested JSON in Python. Let's discuss them one by one:

Using the JSON module

In this example, we use the json module to parse a nested JSON string. Subsequently, we access specific values within the JSON structure using dictionary keys, demonstrating how to retrieve information such as the name, age, city and zipcode.


Output
Name: Prajjwal
Age: 23
City: Prayagraj
Zipcode: 20210

Explanation:

  • json.loads() converts the JSON string into a Python dictionary.
  • Nested values (like "city") are accessed using key chaining: data['address']['city'].
  • This approach is ideal when the structure of JSON is fixed and known in advance.

Using Recursion

In this example, the parse_json function uses recursion to traverse the nested JSON structure and create a flattened dictionary. The parsed data is then accessed using keys to retrieve specific values such as name, age, city and zipcode from the original nested JSON data.


Output
Name: Prajjwal
Age: 23
City: Prayagraj
Zipcode: 20210

Explanation:

  • parse_json() function recursively navigates each level of the JSON structure.
  • This method is helpful when we don't know how deep is the nesting.
  • It builds a dictionary with the same nested structure, making it easier to access specific values later.

Using the Pandas library

In this example, the pd.json_normalize function from the Pandas library is utilized to flatten the nested JSON data into a Pandas DataFrame. The resulting DataFrame, df, allows easy access to specific columns such as 'name' and 'age.'


Output
Names: ['Prajjwal', 'Kareena']
Ages: [23, 22]

Explanation:

  • json.loads() first converts the JSON string to a Python object.
  • pd.json_normalize() flattens the nested list into a table like structure.
  • This is particularly useful for structured data like logs, records or API results with multiple entries.

Also read: Recursion, Python, Pandas, Json.


Next Article

Similar Reads