A Rust implementation of JsonPath.
- Return correct result types (scalar vs. array) based on user queries
- Support a rich set of filters and functions (WIP), e.g.
[?((@.id > 10 || @.id < -1) && @.msg contains 'jsonpath')]
use json_path::JsonPathQuery; use serde_json::json; let object = json!({"greetings": "hello, json_path"}); let result = object.query("$['greetings']"); assert_eq!(Ok(json!("hello, json_path")), result);$ cargo install json_path_bin $ echo '{"msg": "hello!"}' | json_path_bin -j '$.msg' "hello!" $| Operator | Description |
|---|---|
$ | The root element to query. This starts all path expressions. |
@ | The current node being processed by a filter predicate. |
* | Wildcard. Available anywhere a name or numeric are required. |
.. | Deep scan. Available anywhere a name is required. |
.<name> | Dot-notated child |
['<name>' (, '<name>')] | Bracket-notated child or children |
[<number> (, <number>)] | Array index or indexes |
[start:end] | Array slice operator |
[?(<expression>)] | Filter expression. Expression must evaluate to a boolean value. |
- Can use negative numbers for both array index or array slice. It indicates the evaluator to access an item from the end of the array.
- Array slice can support notions like:
[1:]slice from index 1 (inclusive) to the end[:-1]slice from begining to the last item (exclusive)[1:10]slice from 1 (inclusive) to 10 (exclusive)
| Operator | Description |
|---|---|
== | left is equal to right (note that 1 is not equal to '1') |
!= | left is not equal to right |
< | left is less than right |
<= | left is less or equal to right |
> | left is greater than right |
>= | left is greater than or equal to right |
=~ | WIP, left matches regular expression [?(@.name =~ /foo.*?/i)] |
! | Used to negate a filter: [?(!@.isbn)] matches items that do not have the isbn property. |
in | left exists in right [?(@.size in ['S', 'M'])] |
nin | left does not exists in right |
subsetof | left is a subset of right [?(@.sizes subsetof ['S', 'M', 'L'])] |
contains | Checks if a string contains the specified substring (case-sensitive), or an array contains the specified element |
anyof | left has an intersection with right [?(@.sizes anyof ['M', 'L'])] |
noneof | left has no intersection with right [?(@.sizes noneof ['M', 'L'])] |
size | size of left (array or string) should match right |
empty | left (array or string) should be empty, e.g.: [?(@.name empty false)] |
(<expressions>) | use parenthesis to group expressions, e.g. [?(!(@.sizes contains 'M'))] |