![]() |
VOOZH | about |
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.
In this article, we have used the following complex JSON and we will extract nested data from this by using different approaches.
Below are some of the ways by which we can extract nested data from complex JSON in Python:
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.
Extracted Data using Dot Notation: Country: India City: Greater Noida
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.
Extracted Data using List Comprehension: Educational Departments: ['Computer Science', 'Mathematics', 'Physics'] Completed Projects: ['ProjectX', 'ProjectY']
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.
Extracted Data using Recursion: Educational Department (Recursive): Computer Science