Skip to main content

Find dictionary path by key and value

Project description

dict-path-finder

dict-path-finder is a Python library designed to find the paths to specific keys or values within nested dictionaries and lists. This can be particularly useful for working with complex JSON data structures.

Features

  • Find all paths to a specified key in dictionaries and lists.
  • Find all paths to a specified value in dictionaries and lists.
  • Outputs all paths to the key and value you are looking for as a list.

Installation

You can install dict-path-finder using pip:

pip install dict-path-finder

Usage

Finding Paths by Key

from dict_path_finder import find_paths_by_key

data = {
    "items": [
        {
            "type": "book",
            "title": "Inception",
            "authors": [
                {
                    "name": "F. Scott Fitzgerald"
                }
            ]
        },
        {
            "type": "movie",
            "title": "Inception",
            "directors": [
                {
                    "name": ["Christopher Nolan", "heaven"]
                }
            ]
        }
    ]
}

input_key = "title"
results = find_paths_by_key(data, input_key)

for result in results:
    print("Output:", result)

# result
# Output: data["items"][0]["title"]
# Output: data["items"][1]["title"]

Finding Paths by Value

from dict_path_finder import find_paths_by_value

data = {
    "items": [
        {
            "type": "book",
            "title": "Inception",
            "authors": [
                {
                    "name": "F. Scott Fitzgerald"
                }
            ]
        },
        {
            "type": "movie",
            "title": "Inception",
            "directors": [
                {
                    "name": ["Christopher Nolan", "heaven", "Inception"]
                }
            ]
        }
    ]
}

input_value = "Inception"
results = find_paths_by_value(data, input_value)

for result in results:
    print("Output:", result)

# result
# Output: data["items"][0]["title"]
# Output: data["items"][1]["title"]
# Output: data["items"][1]["directors"][0]["name"][2]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dict_path_finder-0.0.3.tar.gz (2.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dict_path_finder-0.0.3-py3-none-any.whl (2.6 kB view details)

Uploaded Python 3

File details

Details for the file dict_path_finder-0.0.3.tar.gz.

File metadata

  • Download URL: dict_path_finder-0.0.3.tar.gz
  • Upload date:
  • Size: 2.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.9

File hashes

Hashes for dict_path_finder-0.0.3.tar.gz
Algorithm Hash digest
SHA256 9c7e6650522e9bb53c4bdf3878e062479f619fe01f52217295024a3ee938f516
MD5 b97c418a1d2b0a036ee117eb05807dc4
BLAKE2b-256 7161daa2d8aa5927600391c8fd5378a6abf866facb0580b0c3bedece6455210b

See more details on using hashes here.

File details

Details for the file dict_path_finder-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for dict_path_finder-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f1ffb330c06c48d74bf491d5de68229404cc72276a57fb33d73ca631fba923a3
MD5 5cfd142b9bf516cf63cc12dcf7f547be
BLAKE2b-256 ae040d359bbcdd4d257add1071d1218ec229ef9fe9b43f8f699e552a9fea97e8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page