Rust: Parse nested JSON

Mar 24, 2022

Not a whole lot new to cover here that isn't already implied from the previous parsing JSON lesson. It works exactly as you'd expect. So here's a bonus shorthand way to parse an object into a JSON structure:

use serde_json::json;

fn main() {
 let glenn = json!({
        "name": "Glenn Gillen",
        "age": 40,
        "phones": [
            "+61 448612567"
        ],
        "address": {
            "line1": "123 Main St",
            "state": "VIC",
            "country": "AU"
        }
    });

    println!("first phone number: {}", glenn["phones"][0]);
    println!("state: {}", glenn["address"]["state"]);
    println!("{}", glenn.to_string());
}
Hi, I'm Glenn! 👋 I'm currently Director of Product (Terraform) @ HashiCorp, and we're hiring! If you'd like to come and work with me and help make Terraform Cloud even more amazing we have multiple positions opening in Product ManagementDesign, and Engineering & Engineering Management across a range of levels (i.e., junior through to senior). Please send in an application ASAP so we can get in touch.