[][src]Crate jsonpath_lib

JsonPath implementation written in Rust.

Example

extern crate jsonpath_lib as jsonpath;
#[macro_use] extern crate serde_json;
let json_obj = json!({
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
});

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

assert_eq!(selector("$.store.book[*].author").unwrap(),
            vec![
                "Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"
            ]);

assert_eq!(selector("$..author").unwrap(),
            vec![
                "Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"
            ]);

assert_eq!(selector("$.store.*").unwrap(),
            vec![
                &json!([
                    { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 },
                    { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 },
                    { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 },
                    { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 }
                ]),
                &json!({ "color": "red", "price": 19.95 })
            ]);

assert_eq!(selector("$.store..price").unwrap(),
            vec![
                8.95, 12.99, 8.99, 22.99, 19.95
            ]);

assert_eq!(selector("$..book[2]").unwrap(),
            vec![
                &json!({
                    "category" : "fiction",
                    "author" : "Herman Melville",
                    "title" : "Moby Dick",
                    "isbn" : "0-553-21311-3",
                    "price" : 8.99
                })
            ]);

assert_eq!(selector("$..book[-2]").unwrap(),
            vec![
                &json!({
                    "category" : "fiction",
                    "author" : "Herman Melville",
                    "title" : "Moby Dick",
                    "isbn" : "0-553-21311-3",
                    "price" : 8.99
                })
            ]);

assert_eq!(selector("$..book[0,1]").unwrap(),
            vec![
                &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
                &json!({"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99})
            ]);

assert_eq!(selector("$..book[:2]").unwrap(),
            vec![
                &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
                &json!({"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99})
            ]);

assert_eq!(selector("$..book[:2]").unwrap(),
            vec![
                &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
                &json!({"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99})
            ]);

assert_eq!(selector("$..book[?(@.isbn)]").unwrap(),
            vec![
                &json!({"category" : "fiction","author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99}),
                &json!({"category" : "fiction","author" : "J. R. R. Tolkien","title" : "The Lord of the Rings","isbn" : "0-395-19395-8","price" : 22.99})
            ]);

assert_eq!(selector("$.store.book[?(@.price < 10)]").unwrap(),
            vec![
                &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
                &json!({"category" : "fiction","author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99})
            ]);

Structs

Compiled

A pre-compiled expression.

Parser
Selector
SelectorMut

Enums

JsonPathError

Functions

compileDeprecated

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.

delete

Delete(= replace with null) the JSON property using the jsonpath.

replace_with

Select JSON properties using a jsonpath and transform the result and then replace it. via closure that implements FnMut you can transform the selected results.

select

It is a simple select function. but it compile the jsonpath argument every time.

select_as

It is the same to select function but it deserialize the the result as given type T.

select_as_str

It is the same to select function but it return the result as string.

selector

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.

selector_as

It is the same to selector function. but it deserialize the result as given type T.