pub trait JsonPathQuery {
// Required method
fn path(self, query: &str) -> Result<Value, JsonPathParserError>;
}
Expand description
the trait allows to query a path on any value by just passing the &str of as JsonPath.
It is equal to
use jsonpath_rust::JsonPath;
let query = "$.hello";
let json_path = JsonPath::from_str(query).unwrap();
json_path.find(&json!({"hello": "world"}));
It is default implemented for Value.
#Note: the result is going to be cloned and therefore it can be significant for the huge queries. if the same &str is used multiple times, it’s more efficient to reuse a single JsonPath.
§Examples:
use std::str::FromStr;
use serde_json::{json, Value};
use jsonpath_rust::jp_v;
use jsonpath_rust::{JsonPathQuery, JsonPath, JsonPathValue};
fn test() -> Result<(), Box<dyn std::error::Error>> {
let json: Value = serde_json::from_str("{}")?;
let v = json.path("$..book[?(@.author size 10)].title")?;
assert_eq!(v, json!([]));
let json: Value = serde_json::from_str("{}")?;
let path = json.path("$..book[?(@.author size 10)].title")?;
assert_eq!(path, json!(["Sayings of the Century"]));
let json: Value = serde_json::from_str("{}")?;
let path = JsonPath::try_from("$..book[?(@.author size 10)].title")?;
let v = path.find_slice(&json);
let js = json!("Sayings of the Century");
assert_eq!(v, jp_v![&js;"",]);
}