https://github.com/night-crawler/jsonpath-rust-bindings
Bindings for jsonpath-rust library
https://github.com/night-crawler/jsonpath-rust-bindings
jsonpath jsonpath-parser jsonpath-query maturin pyo3 python rust
Last synced: 7 months ago
JSON representation
Bindings for jsonpath-rust library
- Host: GitHub
- URL: https://github.com/night-crawler/jsonpath-rust-bindings
- Owner: night-crawler
- License: mit
- Created: 2023-11-04T16:09:36.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-08T17:06:07.000Z (over 1 year ago)
- Last Synced: 2025-04-19T18:34:02.895Z (9 months ago)
- Topics: jsonpath, jsonpath-parser, jsonpath-query, maturin, pyo3, python, rust
- Language: Rust
- Homepage:
- Size: 40 KB
- Stars: 9
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jsonpath-rust-bindings




This package contains Python bindings for [jsonpath-rust](https://github.com/besok/jsonpath-rust) library by [besok](https://github.com/besok).
The details regarding the JsonPath itself can be found [here](https://goessner.net/articles/JsonPath/).
## Installation
```bash
pip install jsonpath-rust-bindings
```
## Usage
```python
from jsonpath_rust_bindings import Finder
sample = {
"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,
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99,
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99,
},
],
"bicycle": {"color": "red", "price": 19.95},
},
"expensive": 10,
}
queries = [
'$.store.book[*].author',
'$..book[?(@.isbn)]',
'$.store.*',
'$..author',
'$.store..price',
'$..book[2]',
# '$..book[-2]',
'$..book[0,1]',
'$..book[:2]',
'$..book[1:2]',
'$..book[-2:]',
'$..book[2:]',
'$.store.book[?(@.price<10)]',
'$..book[?(@.price<=$.expensive)]',
"$..book[?(@.author ~= '.*Rees')].price",
'$..*',
]
f = Finder(sample)
for query in queries:
print(query, f.find(query), '\n')
# You will see a bunch of found items like
# $..book[?(@.author ~= '.*Rees')].price [JsonPathResult(data=8.95, path=Some("$.['store'].['book'][0].['price']"), is_new_value=False)]
```
`JsonPathResult` has the following attributes:
- data: the found value
- path: the path to the found value
`JsonPathResult` can't be constructed from Python; it is only returned by `Finder.find()`.
## Caveats
The current implementation is cloning the original `PyObject` data when converting it to the serde `Value`.
It happens each time you're creating a new `Finder` instance. Try to reuse the same `Finder` instance for querying if it's possible.
Also, It has yet another consequence demonstrated in the following example:
```python
>>> original_object_i_want_to_mutate = {'a': {'b': 'sample b'}}
>>> from jsonpath_rust_bindings import Finder
>>> f = Finder(original_object_i_want_to_mutate)
>>> b_dict = f.find('$.a')[0].data
>>> b_dict
{'b': 'sample b'}
>>> b_dict['new'] = 42
>>> original_object_i_want_to_mutate
{'a': {'b': 'sample b'}}
```
## Development
Clone the repository.
Update maturin if needed:
```bash
pip install maturin -U
```
Add your changes, then run:
```bash
maturin develop
```
Update CI:
```bash
maturin generate-ci github > ./.github/workflows/CI.yml
```