Nested Lookup

0.2.25 · maintenance · verified Wed Apr 15

Nested-lookup is a Python library providing functions for working with deeply nested documents, typically a mix of dictionaries and lists, often derived from JSON, YAML, or XML. It simplifies tasks like performing key lookups, updating values, deleting keys, and altering values within these complex structures. The library is currently at version 0.2.25, with its last release in July 2022, indicating a maintenance cadence rather than active rapid development.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `nested_lookup` to find values, `nested_update` to change values, and `nested_delete` to remove keys from a deeply nested document. It also highlights the `in_place` argument for mutation.

from nested_lookup import nested_lookup, nested_update, nested_delete

document = [
    {'taco': 42},
    {
        'salsa': [
            {'burrito': {'taco': 69}},
            {'nachos': 100}
        ]
    },
    {'drink': {'soda': 'coke'}}
]

# Lookup a key
tacos = nested_lookup('taco', document)
print(f"Found tacos: {tacos}")
# Expected: Found tacos: [42, 69]

# Update a value (returns a copy by default)
updated_document = nested_update(document, key='burrito', value='test_burrito_update')
print(f"Updated document (copy): {updated_document[1]['salsa'][0]}")
# Expected: Updated document (copy): {'burrito': 'test_burrito_update'}

# Delete a key (returns a copy by default)
deleted_document = nested_delete(document, 'taco')
print(f"Document after deleting 'taco' (copy): {deleted_document[0]}, {deleted_document[1]['salsa'][0]}")
# Expected: Document after deleting 'taco' (copy): {}, {'burrito': {'taco': 69}} (Note: original 'taco' in burrito is not deleted here as the key 'taco' at the top level of the inner dict is not directly deleted.)

# Demonstrate in_place update
print(f"Original document before in_place update: {document[0]}")
nested_update(document, key='taco', value='new_taco_val', in_place=True)
print(f"Original document after in_place update: {document[0]}")
# Expected: Original document after in_place update: {'taco': 42}\nOriginal document after in_place update: {'taco': 'new_taco_val'}

view raw JSON →