[][src]Function jsonpath_lib::selector

pub fn selector(json: &Value) -> impl FnMut(&str) -> Result<Value, String>

It returns highorder function that return a function.

this function has a jsonpath as argument and return a serde_json::value::Value. so you can use different JsonPath for one JsonObject.

extern crate jsonpath_lib as jsonpath;
#[macro_use] extern crate serde_json;

let json_obj = json!({
"school": {
   "friends": [
        {"name": "친구1", "age": 20},
        {"name": "친구2", "age": 20}
    ]
},
"friends": [
    {"name": "친구3", "age": 30},
    {"name": "친구4"}
]});

let mut selector = jsonpath::selector(&json_obj);

let json = selector("$..friends[0]").unwrap();
let ret = json!([
    {"name": "친구3", "age": 30},
    {"name": "친구1", "age": 20}
]);
assert_eq!(json, ret);

let json = selector("$..friends[1]").unwrap();
let ret = json!([
    {"name": "친구4"},
    {"name": "친구2", "age": 20}
]);
assert_eq!(json, ret);