Skip to content
changseok han edited this page Mar 26, 2019 · 8 revisions

Simple usage

Documentation: https://docs.rs/crate/jsonpath_lib/

[dependencies]
jsonpath_lib = "0.1"
serde = "1.0"
serde_json = "1.0"
extern crate jsonpath_lib as jsonpath;
extern crate serde;
extern crate serde_json;

use serde::{Deserialize};

#[derive(Deserialize, PartialEq, Debug)]
struct Person {
    name: String,
    age: u8,
    phones: Vec<String>,
}

fn main() {
    let ret: Person = jsonpath::select_as(r#"
    {
        "person":
            {
                "name": "Doe John",
                "age": 44,
                "phones": [
                    "+44 1234567",
                    "+44 2345678"
                ]
            }
    }
    "#, "$.person").unwrap();

    let person = Person {
        name: "Doe John".to_string(),
        age: 44,
        phones: vec!["+44 1234567".to_string(), "+44 2345678".to_string()],
    };

    assert_eq!(person, ret);
}
Clone this wiki locally