jsonpath-rust
The library provides the extensive functionality to find data sets according to filtering queries. Inspired by XPath for XML structures, JsonPath is a query language for JSON. The specification is described in RFC 9535.
Important note
The version 1.0.0 has a breaking change. The library has been rewritten from scratch to provide compliance with the RFC9535.
The changes are:
- The library is now fully compliant with the RFC 9535.
- New structures and apis were introduced to provide the compliance with the RFC 9535.
Queryable
instead ofJsonLike
Queried<Queryable>
instead ofResult<Value, JsonPathParserError>
JsonPath#{query_with_path, query_only_path, query}
to operate with theQueryable
structureJsonPathError
instead ofJsonPathParserError
QueryRef
to provide the reference to the value and path
- The functions in, nin, noneOf, anyOf, subsetOf are now implemented as custom filter expressions and renamed to
in
,nin
,none_of
,any_of
,subset_of
respectively. - The function length was removed (the size can be checked using rust native functions for using it in filter there is length expression).
The compliance with RFC 9535
The library is fully compliant with the standard RFC 9535 To check the compliance with the standard head to rfc9535 subfolder
Examples
Given the json
JsonPath | Result |
---|---|
$.store.book[*].author |
The authors of all books |
$..book[?@.isbn] |
All books with an ISBN number |
$.store.* |
All things, both books and bicycles |
$..author |
All authors |
$.store..price |
The price of everything |
$..book[2] |
The third book |
$..book[-2] |
The second to last book |
$..book[0,1] |
The first two books |
$..book[:2] |
All books from index 0 (inclusive) until index 2 (exclusive) |
$..book[1:2] |
All books from index 1 (inclusive) until index 2 (exclusive) |
$..book[-2:] |
Last two books |
$..book[2:] |
Book number two from tail |
$.store.book[?@.price < 10] |
All books in store cheaper than 10 |
$..book[?@.price <= $.expensive] |
All books in store that are not "expensive" |
$..book[?@.author ~= '(?i)REES'] |
All books matching regex (ignore case) |
$..* |
Give me every thing |
Library Usage
Extensions
The library provides the following extensions:
-
in
Checks if the first argument is in the array provided as the second argument. Example:$.elems[?in(@, $.list)]
Returns elements from$.elems
that are present in$.list
. -
nin
Checks if the first argument is not in the array provided as the second argument. Example:$.elems[?nin(@, $.list)]
Returns elements from$.elems
that are not present in$.list
. -
none_of
Checks if none of the elements in the first array are in the second array. Example:$.elems[?none_of(@, $.list)]
Returns arrays from$.elems
that have no elements in common with$.list
. -
any_of
Checks if any of the elements in the first array are in the second array. Example:$.elems[?any_of(@, $.list)]
Returns arrays from$.elems
that have at least one element in common with$.list
. -
subset_of
Checks if all elements in the first array are in the second array. Example:$.elems[?subset_of(@, $.list)]
Returns arrays from$.elems
where all elements are present in$.list
.
Queryable
The library provides a trait Queryable
that can be implemented for any type.
This allows you to use the JsonPath
methods on your own types.
Queried with path
Queried without path
Queried with only path
Update the Queryable structure by path
The library does not provide the functionality to update the json structure in the query itself.
Instead, the library provides the ability to update the json structure by the path.
Thus, the user needs to find a path for the JsonLike
structure and update it manually.
There are two methods in the Queryable
trait:
reference_mut
- returns a mutable reference to the element by the pathreference
- returns a reference to the element by the path
They accept a JsonPath
instance and return a Option<&mut Self>
or Option<&Self>
respectively.
The path is supported with the limited elements namely only the elements with the direct access:
- root
- field
- index
Python bindings
Python bindings (jsonpath-rust-bindings) are available on pypi:
How to contribute
TBD
How to update version
- update files
- commit them
- add tag
git tag -a v<Version> -m "message"
- git push origin <tag_name>