#json #json-query #filter #array #user #scalar

json_path

A Rust implementation of JsonPath

6 releases

0.1.5 Jun 12, 2023
0.1.4 Jun 12, 2023

#6 in #scalar

33 downloads per month
Used in json_path_bin

MIT/Apache

73KB
2K SLoC

jsonpath-rs

Crates.io docs.rs Rust CI

A Rust implementation of JsonPath.

Why

  1. Return correct result types (scalar vs. array) based on user queries
  2. Support a rich set of filters and functions (WIP), e.g. [?((@.id > 10 || @.id < -1) && @.msg contains 'jsonpath')]

To Use the Library

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);

To Use the Binary

$ cargo install json_path_bin
$ echo '{"msg": "hello!"}' | json_path_bin -j '$.msg'
"hello!"
$

Features

Operators

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.
  1. 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.
  2. 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)

Filters

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'))]

Similar Projects

Dependencies

~0.4–0.8MB
~18K SLoC