Skip to main content

Python bindings for fast JSONPath resolve and value-to-path search implemented in Rust

Project description

JSONPath Sleuth

Fast Python bindings (via Rust + PyO3) for:

  • Resolving JSONPath expressions against Python dict/list JSON
  • Finding JSONPath-like paths for all occurrences of a target value

Install / Build

  • Option A: editable dev install
    • pip install maturin
    • maturin develop -m pyproject.toml
  • Option B: build wheel
    • maturin build -m pyproject.toml
    • pip install dist/*.whl

Requires Python 3.8+.

Run Tests

  • Rust unit tests (no Python needed)

    • cargo test
    • Also compile PyO3 bindings: PYO3_PYTHON=$(which python3) cargo test --features python
  • Python tests (pytest)

    • Option A (quick):
      • python3 -m venv .venv && source .venv/bin/activate
      • pip install -U pip pytest maturin
      • maturin develop -m Cargo.toml --features python
      • pytest -q
    • Option B (dev extra):
      • python3 -m venv .venv && source .venv/bin/activate
      • pip install -U pip
      • pip install -e .[dev]
      • maturin develop -m Cargo.toml --features python
      • pytest -q

Notes

  • Re-run maturin develop after Rust changes to refresh the extension in your venv.
  • If pytest cannot import jsonpath_sleuth, ensure you activated the same venv used for maturin develop.

Publish

  • Build wheels + sdist

    • maturin build -m Cargo.toml --features python --release --sdist
  • TestPyPI (requires separate TestPyPI account and token)

    • Publish: maturin publish -m Cargo.toml --features python --repository-url https://test.pypi.org/legacy/ -u __token__ -p <pypi-TEST_TOKEN>
    • Install to verify: pip install -i https://test.pypi.org/simple jsonpath-sleuth
  • PyPI

    • Publish: maturin publish -m Cargo.toml --features python -u __token__ -p <pypi-PROD_TOKEN>
    • Install to verify: pip install jsonpath-sleuth

Tips

  • Bump version in both Cargo.toml and pyproject.toml before publishing a new release.
  • Tokens begin with pypi-. Avoid committing tokens; pass on the command line or configure ~/.pypirc.

Python API

Module: jsonpath_sleuth

  • resolve_jsonpath(data: dict | list, path: str) -> list[Any]
    • Returns a list of matched values for the given JSONPath. The path may omit the leading $ (it is added automatically).
  • find_jsonpaths_by_value(data: dict | list, target: Any) -> list[str]
    • Returns string paths like foo.bar[0].baz where value equals target.

Examples

from jsonpath_sleuth import resolve_jsonpath, find_jsonpaths_by_value

obj = {
    "store": {
        "book": [
            {"category": "fiction", "title": "Sword"},
            {"category": "fiction", "title": "Shield"},
        ],
        "bicycle": {"color": "red", "price": 19.95},
    }
}

# 1) Resolve JSONPath (prefix not required)
print(resolve_jsonpath(obj, "store.book[*].title"))
# -> ["Sword", "Shield"]
# Also works with explicit JSONPath:
print(resolve_jsonpath(obj, "$.store.book[*].title"))
# -> ["Sword", "Shield"]

# 2) Find paths by target value
print(find_jsonpaths_by_value(obj, "fiction"))
# -> ["store.book[0].category", "store.book[1].category"]

Notes

  • JSONPath is powered by jsonpath_lib crate.
  • Paths produced by value search:
    • Use . between object keys and [idx] for arrays.
    • If the entire input equals the target, no paths are returned (empty list).

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

jsonpath_sleuth-0.1.1.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

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

jsonpath_sleuth-0.1.1-cp38-abi3-macosx_11_0_arm64.whl (331.8 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file jsonpath_sleuth-0.1.1.tar.gz.

File metadata

  • Download URL: jsonpath_sleuth-0.1.1.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.10.1

File hashes

Hashes for jsonpath_sleuth-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d76b273c6bd54a0d98b447e61d2187514c992cc6b026032fba01ffa14391ecdc
MD5 18f3e35f8254d316825b203084ed574a
BLAKE2b-256 5788304ef21a3d96a42f81b0fddcdc61a95070f69107a44fd8fa0097773a53ef

See more details on using hashes here.

File details

Details for the file jsonpath_sleuth-0.1.1-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for jsonpath_sleuth-0.1.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d806c1c27faa9385575b196a93b60df5042fcac6910dea2b8944c4c0d0e7c856
MD5 130979dcf3426d47e19be44396574c6a
BLAKE2b-256 e190c1f29d7e914bfba7ab4af0a44bfb5cd84612a7f076f3fa6d5c6c6ff77a3e

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