Skip to content

How to Access and Extract Nested Data in JSON in Python

Learn how to safely traverse, extract, and process deeply nested JSON structures in Python using dictionaries, lists, and methods like .get() to handle missing keys.

JSON (JavaScript Object Notation) is the standard format for data exchange on the web. In Python, JSON structures are mapped directly to standard data types: objects become dictionaries and arrays become lists.

Accessing deeply nested data requires navigating through layers of these dictionaries and lists. This guide explains how to traverse these structures safely and efficiently.

Loading JSON Data

Before accessing data, you must parse the JSON string or file into a Python object (usually a dictionary).

import json

json_data = '''
{
    "user": {
        "id": 101,
        "name": "Alice",
        "contacts": [
            {"type": "email", "value": "alice@example.com"},
            {"type": "phone", "value": "555-0199"}
        ]
    }
}
'''

data = json.loads(json_data)

Method 1: Direct Access (Chaining)

If you are certain about the structure of your data, you can chain dictionary keys and list indices together.

  • Use ['key'] for objects/dictionaries.
  • Use [index] for arrays/lists.
user = data['user']

user_name = data['user']['name']

first_contact_type = data['user']['contacts'][0]['type']

print(f"User: {user_name}")
print(f"Contact Type: {first_contact_type}")

Output:

User: Alice
Contact Type: email

Method 2: Safe Access (.get())

When dealing with API responses, fields might be missing. Using .get() prevents your script from crashing by allowing you to return a default value (like None or {}) if a key isn't found.


address = data.get('user', {}).get('address', {})
zip_code = address.get('zip', '00000') # Default fallback

print(f"Zip Code: {zip_code}")

Output:

Zip Code: 00000

Method 3: Iterating Through Lists

JSON arrays often contain multiple objects that need to be processed. A for loop is the standard way to handle this.

contacts = data['user']['contacts']

for contact in contacts:
    c_type = contact['type']
    c_value = contact['value']
    print(f"Found {c_type}: {c_value}")

Output:

Found email: alice@example.com
Found phone: 555-0199

Advanced: List Comprehension Extraction

You can extract specific fields from a list of objects in one line.

emails = [c['value'] for c in contacts if c['type'] == 'email']
print(f"Emails found: {emails}")

Conclusion

Navigating nested JSON in Python is simply navigating nested dictionaries and lists.

  1. Parse using json.loads().
  2. Chain keys (['a']['b']) for direct access to known paths.
  3. Use .get() for optional fields to avoid KeyError.
  4. Loop over lists to process arrays of objects.
Navigation