rustHow to get a value by key from JSON in Rust?
Getting a value by key from JSON in Rust is easy with the serde_json crate.
Example code
extern crate serde_json;
fn main() {
    let data = r#"
        {
            "name": "John Doe",
            "age": 43
        }
    "#;
    let v: serde_json::Value = serde_json::from_str(data).unwrap();
    let name = v["name"].as_str().unwrap();
    println!("Name: {}", name);
}
Output example
Name: John Doe
The code above:
- imports the 
serde_jsoncrate (extern crate serde_json;) - creates a JSON string (
let data = r#"{...}"#;) - parses the JSON string into a 
serde_json::Value(let v: serde_json::Value = serde_json::from_str(data).unwrap();) - gets the value of the 
namekey (let name = v["name"].as_str().unwrap();) - prints the value of the 
namekey (println!("Name: {}", name);) 
Helpful links
Related
More of Rust
- How to use non-capturing groups in Rust regex?
 - Regex example to match multiline string in Rust?
 - How to match the end of a line in a Rust regex?
 - How to use regex captures in Rust?
 - How to use regex to match a double quote in Rust?
 - How to escape a Rust regex?
 - How to match all using regex in Rust?
 - How to perform matrix operations in Rust?
 - How to use regex lookbehind in Rust?
 - How to create a HashMap of structs in Rust?
 
See more codes...