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
- Extracting all JSONPath-like paths paired with their leaf values
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/activatepip install -U pip pytest maturinmaturin develop -m Cargo.toml --features pythonpytest -q
- Option B (dev extra):
python3 -m venv .venv && source .venv/bin/activatepip install -U pippip install -e .[dev]maturin develop -m Cargo.toml --features pythonpytest -q
- Option A (quick):
Notes
- Re-run
maturin developafter Rust changes to refresh the extension in your venv. - If pytest cannot import
jsonpath_sleuth, ensure you activated the same venv used formaturin 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
- Publish:
-
PyPI
- Publish:
maturin publish -m Cargo.toml --features python -u __token__ -p <pypi-PROD_TOKEN> - Install to verify:
pip install jsonpath-sleuth
- Publish:
Tips
- Bump version in both
Cargo.tomlandpyproject.tomlbefore 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).
- Returns a list of matched values for the given JSONPath. The path may omit the leading
find_jsonpaths_by_value(data: dict | list, target: Any) -> list[str]- Returns string paths like
foo.bar[0].bazwhere value equalstarget.
- Returns string paths like
extract_jsonpaths_and_values(data: dict | list) -> list[tuple[str, Any]]- Returns all JSONPath-like paths paired with their leaf values. Paths use
.for object keys and[idx]for arrays.
- Returns all JSONPath-like paths paired with their leaf values. Paths use
Examples
from jsonpath_sleuth import resolve_jsonpath, find_jsonpaths_by_value, extract_jsonpaths_and_values
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"]
# 3) Extract all leaf paths and values
print(extract_jsonpaths_and_values(obj))
# -> [
# ("store.book[0].category", "fiction"),
# ("store.book[0].title", "Sword"),
# ("store.book[1].category", "fiction"),
# ("store.book[1].title", "Shield"),
# ("store.bicycle.color", "red"),
# ("store.bicycle.price", 19.95),
# ]
Notes
- JSONPath is powered by
jsonpath_libcrate. - JSONPath keys with spaces or special characters must be quoted using bracket notation.
- Example: use
a['some key'].nextinstead ofa.some key.next. - You may omit the leading
$; the resolver adds it automatically.
- Example: use
- 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).
- Use
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jsonpath_sleuth-0.1.4.tar.gz.
File metadata
- Download URL: jsonpath_sleuth-0.1.4.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
824edf176201e9042d80a18810ff9bb4cb9b7162c18d3595530683ee0d8703c5
|
|
| MD5 |
fb6a7919f38159c02d82ed30891c9050
|
|
| BLAKE2b-256 |
acf1e81f1521d1d69e38f8747a4a21ccc2886428d90710bdedf990a1f31dc60b
|
File details
Details for the file jsonpath_sleuth-0.1.4-cp38-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: jsonpath_sleuth-0.1.4-cp38-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 334.2 kB
- Tags: CPython 3.8+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27ecbaa3524fde7ba6cdce1ec345421e8ba7988a33b99fcb7d3d16d8a94c2ab7
|
|
| MD5 |
6c294d2244db9c899cecc3cf8a9165ed
|
|
| BLAKE2b-256 |
a918ff5bad003baee6b56535797ca06688c00164c0ffd14ec3405d2385e1f11d
|