Parsing Json Nested Dictionary Using Python
Last Updated :
28 Apr, 2025
We are given a JSON string and we have to parse a nested dictionary from it using different approaches in Python. In this article, we will see how we can parse nested dictionary from a JSON object using Python.
Example:
Input: json_data = '{"name": "John", "age": 30, "address": {"city": "New York", "zipcode": "10001"}}'
Output: Name: John, Address: New York
Explanation: We have parsed name and address from the nested dictionary given as JSON string.
Parsing Json Nested Dictionary Using Python
Below, are the methods of Parsing JSON nested Dictionary Using Python:
Parsing Json Nested Dictionary Using json
Module
In this example, In below code, the `json` module is used to parse a JSON-formatted string (`json_data`). The `json.loads()` function is employed to convert the string into a Python dictionary (`parsed_data`).
Python3
import json
json_data = '{"name": "John", "age": 30, "address": {"city": "New York", "zipcode": "10001"}}'
parsed_data = json.loads(json_data)
print('Name:',parsed_data['name'])
print('Address:',parsed_data['address']['city'])
OutputName: John
Address: New York
Parsing Json Nested Dictionary Using jsonpath-ng
Library
In this example, below code uses the `jsonpath_ng` library to perform a JSONPath query on parsed JSON data, extracting and printing the value associated with the 'city' key within the 'address' sub-dictionary.
Python3
from jsonpath_ng import jsonpath, parse
json_data = '{"name": "John", "age": 30, "address": {"city": "New York", "zipcode": "10001"}}'
parsed_data = json.loads(json_data)
jsonpath_expr = parse('$.address.city')
match = jsonpath_expr.find(parsed_data)
if match:
print('City:',match[0].value)
Output
City: New York
Parsing Json Nested Dictionary Using Recursion
In this example, below recursive function (`recursive_parser`) extracts nested values from a Python dictionary (`json_data`) based on a list of keys (`keys_to_extract`). The result is the value associated with the last key in the list, demonstrating a method for selectively accessing nested data structures in a flexible and recursive manner.
Python3
def recursive_parser(data, keys):
if not keys:
return data
return recursive_parser(data.get(keys[0], {}), keys[1:])
json_data = {"name": "John", "age": 30, "address": {"city": "New York", "zipcode": "10001"}}
keys_to_extract = ['address', 'city']
result = recursive_parser(json_data, keys_to_extract)
print('City:',result)
Parsing Json Nested Dictionary Using Pandas
Library
In this example, below code uses the `pandas` library to parse JSON data into a DataFrame (`parsed_data`) and then extracts and prints the value associated with the 'city' key within the 'address' column. The `pd.json_normalize()` function helps to flatten nested JSON structures into a tabular format.
Python3
import json
import pandas as pd
json_data = '{"name": "John", "age": 30, "address": {"city": "New York", "zipcode": "10001"}}'
parsed_data = pd.json_normalize(json.loads(json_data))
print'City:',(parsed_data['address.city'].values[0])
Output
City: New York
Similar Reads
How to Parse Nested JSON in Python 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 JSONNested JSON refers to a JSON object that cont
3 min read
Python - Print dictionary of list values In this article, we will explore various ways on How to Print Dictionary in Python of list values. A dictionary of list values means a dictionary contains values as a list of dictionaries in Python. Example: {'key1': [{'key1': value,......,'key n': value}........{'key1': value,......,'key n': value}
4 min read
How to Print a Dictionary in Python Python Dictionaries are the form of data structures that allow us to store and retrieve the key-value pairs properly. While working with dictionaries, it is important to print the contents of the dictionary for analysis or debugging.Example: Using print FunctionPython# input dictionary input_dict =
3 min read
Loop Through a Nested Dictionary in Python Working with nested dictionaries in Python can be a common scenario, especially when dealing with complex data structures. Iterating through a nested dictionary efficiently is crucial for extracting and manipulating the desired information. In this article, we will explore five simple and generally
3 min read
Python Get All Values from Nested Dictionary In this article, we will learn how we can Get all Values from Nested Dictionary in Python Programming. A nested Dictionary in Python is a dictionary that contains another dictionary as its values. Using this, we can create a nested structure where each of the key-value pairs is in the outer dictiona
5 min read