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.2.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.2-py3-none-any.whl (2.6 kB view details)

Uploaded Python 3

File details

Details for the file dict-path-finder-0.0.2.tar.gz.

File metadata

  • Download URL: dict-path-finder-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 10f6899dd0d4b079e4854f507a9ed9c23cd0bd1b5463a456617a882dabe78300
MD5 9a59980925aa3f1c8f7a0085554bfdec
BLAKE2b-256 4ce72a6f6cf9f67af9fd1937431603c33408e9b9cbe60d1104d848c866269b2f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dict_path_finder-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ea1546ca73d32732474e9578f9e96195630b49c9d16f167431d6e99ef60e6fcf
MD5 7d4b5d0b9c3380f2e3b3fd8a8bebe818
BLAKE2b-256 26a105cec0f73cf59045124b94d3c312967b8bc3d1bc57d23ec61e2a12acf55e

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