Open In App

Extract Nested Data From Complex Json

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

JSON (JavaScript Object Notation) is a widely used data interchange format due to its simplicity and human-readable structure. In many scenarios, JSON data can be complex, containing nested structures and arrays. Extracting specific information from such complex JSON becomes essential for data analysis, integration, and various programming tasks. In this article, we will explore methods to efficiently extract nested data from complex JSON in Python.

JSON Data

In this article, we have used the following complex JSON and we will extract nested data from this by using different approaches.

Python3




import json
 
# Complex dictionary with nested structures
data = {
    "location": {
        "country": "India",
        "state": "Uttar Pradesh",
        "city": "Greater Noida"
    },
    "organizations": [
        {
            "name": "GeeksforGeeks",
            "type": "Educational",
            "departments": ["Computer Science", "Mathematics", "Physics"]
        },
        {
            "name": "TechCorp",
            "type": "Technology",
            "departments": ["Software Development", "Hardware Design"]
        }
    ],
    "projects": {
        "ongoing": ["ProjectA", "ProjectB"],
        "completed": ["ProjectX", "ProjectY"]
    }
}
 
# Convert dictionary to JSON with indentation for readability
json_data = json.dumps(data, indent=2)


Python Extract Nested Data From Complex Json

Below are some of the ways by which we can extract nested data from complex JSON in Python:

  1. Using dot Notation
  2. Using List Comprehension
  3. Using Recursion

Using dot Notation

In this approach, we directly access the nested elements by chaining the keys using dot notation. For instance, data["location"]["country"] retrieves the country value.

Python3




# Extract nested data using dot notation
country_dot = data["location"]["country"]
city_dot = data["location"]["city"]
 
print(f"Extracted Data using Dot Notation:")
print(f"Country: {country_dot}")
print(f"City: {city_dot}")


Output

Extracted Data using Dot Notation:
Country: India
City: Greater Noida

Using List Comprehension

List comprehension is employed here to filter and extract specific nested data. For example, it gathers educational departments for the organization “GeeksforGeeks” and completed projects.

Python3




# Extract nested data using list comprehension
edu_departments = [dept for org in data["organizations"]
                   if org["name"] == "GeeksforGeeks" for dept in org["departments"]]
completed_projects = [project for status, projects in data["projects"].items(
) if status == "completed" for project in projects]
 
print(f"Extracted Data using List Comprehension:")
print(f"Educational Departments: {edu_departments}")
print(f"Completed Projects: {completed_projects}")


Output

Extracted Data using List Comprehension:
Educational Departments: ['Computer Science', 'Mathematics', 'Physics']
Completed Projects: ['ProjectX', 'ProjectY']

Using Recursion

A recursive function is utilized to traverse nested structures dynamically. The function extract_data_recursive takes a list of keys and retrieves the nested data accordingly. In this case, it extracts the first department of the first organization.

Python3




# Extract nested data using recursion
def extract_data_recursive(data, keys):
    return data[keys[0]] if len(keys) == 1 else extract_data_recursive(data[keys[0]], keys[1:])
 
 
edu_department_recursive = extract_data_recursive(
    data, ["organizations", 0, "departments", 0])
 
print(f"Extracted Data using Recursion:")
print(f"Educational Department (Recursive): {edu_department_recursive}")


Output

Extracted Data using Recursion:
Educational Department (Recursive): Computer Science


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads