Parsing Json Nested Dictionary Using Python
Last Updated :
24 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)