Open In App

Parsing Json Nested Dictionary Using Python

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

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'])


Output

Name: 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) 


Output

City: New York

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


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

Similar Reads