[][src]Function jsonpath_lib::compile

pub fn compile(
    path: &str
) -> impl FnMut(&Value) -> Result<Vec<&Value>, JsonPathError>

It is a high-order function. it compile a jsonpath and then returns a closure that has JSON as argument. if you need to reuse a jsonpath, it is good for performance.

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

let mut first_firend = jsonpath::compile("$..friends[0]");

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

let json = first_firend(&json_obj).unwrap();

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