Python JSONPath Rust Bindings
Python bindings for the high-performance `jsonpath-rust` library, enabling fast JSONPath querying of Python dictionaries. It's currently at version 1.1.1 and has a relatively stable release cadence, focusing on minor updates and bug fixes.
Common errors
-
AttributeError: module 'jsonpath_rust' has no attribute 'parse'
cause Attempting to use the pre-1.0.0 module-level `parse` function after upgrading to version 1.0.0 or later.fixUse `JsonPath.parse()` and `jsonpath_object.find()` methods instead. Example: `jsonpath = JsonPath.parse('$.your.path'); result = jsonpath.find(data)`. -
jsonpath_rust.errors.JsonpathError: Parse error at position X: Expected Y, found Z
cause Invalid JSONPath syntax provided to `JsonPath.parse()`.fixReview your JSONPath string for syntax errors. Ensure it matches the JSONPath specification supported by `jsonpath-rust`. Common issues include unclosed brackets, incorrect operators, or invalid segment names. -
ModuleNotFoundError: No module named 'jsonpath_rust'
cause The `jsonpath-rust-bindings` library is not installed, or the Python environment where it was installed is not active.fixInstall the library using `pip install jsonpath-rust-bindings`. If using virtual environments, ensure the correct environment is activated.
Warnings
- breaking The API for parsing and querying JSONPaths changed significantly in version 1.0.0. Module-level functions like `jsonpath_rust.parse()` and `jsonpath_rust.select()` were removed.
- gotcha Ensure your JSONPath syntax adheres strictly to the `jsonpath-rust` implementation. It may differ subtly from other JSONPath engines or custom implementations, especially concerning advanced features or edge cases.
- gotcha While highly performant due to Rust, querying extremely large or deeply nested JSON documents repeatedly may still incur performance costs within Python's object model.
Install
-
pip install jsonpath-rust-bindings
Imports
- JsonPath
from jsonpath_rust import JsonPath
Quickstart
from jsonpath_rust import JsonPath
data = {
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
jsonpath = JsonPath.parse('$.store.book[*].author')
result = jsonpath.find(data)
print(result)