Function jsonpath_lib::selector[][src]

pub fn selector<'a>(
    json: &'a Value
) -> impl FnMut(&str) -> Result<Vec<&'a Value>, JsonPathError>
Expand description

It is a high-order function. it returns a closure that has a jsonpath string as argument. you can use diffenent jsonpath for one JSON object.

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

assert_eq!(json, vec![
    &json!({"name": "친구3", "age": 30}),
    &json!({"name": "친구1", "age": 20})
]);

let json = selector("$..friends[1]").unwrap();

assert_eq!(json, vec![
    &json!({"name": "친구4"}),
    &json!({"name": "친구2", "age": 20})
]);