How to deserialize nested JSON

Use serde with derive macros and rename attributes to map nested JSON keys to Rust structs automatically.

Define a struct with #[derive(serde::Deserialize)] and use #[serde(rename_all = "kebab-case")] to map nested JSON keys to Rust fields. The serde crate automatically handles the nesting hierarchy based on your struct definition.

#[derive(serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
struct Config {
    output_mode: Mode,
}

let config: Config = serde_json::from_str(json_str)?;

For deeply nested paths like preprocessor.test-preprocessor, access the specific section from the parent context before deserializing:

let config: Config = ctx
    .config
    .get(&format!("preprocessor.{preprocessor_name}"))?
    .ok_or_else(|| Error::NoConfig(preprocessor_name.into()))?;