Dev Tools

JSON → Rust Struct

Generate Rust structs from JSON with #[derive(Serialize, Deserialize, Debug)]. Snake_case fields with #[serde(rename)] where needed.

Root struct name
JSON input
Rust output
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct Address {
    pub street: String,
    pub city: String,
    pub zip: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Order {
    pub id: i64,
    pub total: f64,
    pub items: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Root {
    pub id: i64,
    #[serde(rename = "userName")]
    pub user_name: String,
    pub email: String,
    pub active: bool,
    pub score: f64,
    pub address: Address,
    pub tags: Vec<String>,
    pub orders: Vec<Order>,
    pub metadata: Option<serde_json::Value>,
}
Was this page helpful?

Related tools