JSON to Rust Struct
Generate serde Serialize/Deserialize Rust structs from any JSON object.
What is Rust serde?
serde is Rust's de-facto serialization and deserialization framework. It is used by virtually every Rust project that works with JSON, TOML, YAML, MessagePack, or other data formats. The serde_json crate adds JSON support on top of serde.
To deserialize a JSON response into a Rust type, you derive Deserialize on your struct and call serde_json::from_str() or serde_json::from_value(). Writing these structs manually for complex API responses is time-consuming — this tool generates the complete struct hierarchy from any JSON with proper #[serde(rename)] attributes for keys that do not follow Rust snake_case conventions.
JSON to Rust Struct Example
Input JSON:
{
"id": 1,
"user_name": "alice",
"is_active": true,
"score": 9.5,
"address": { "city": "Berlin" }
}Generated Rust structs:
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Address {
pub city: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Root {
pub id: i64,
pub user_name: String,
pub is_active: bool,
pub score: f64,
pub address: Address,
}Cargo.toml Setup
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"Parse JSON into your struct with serde_json::from_str::<Root>(&json_str)?.
Type Mapping
| JSON Type | Rust Type | Notes |
|---|---|---|
| string | String | Owned heap string |
| integer | i64 | All whole numbers |
| float | f64 | Any decimal number |
| boolean | bool | true / false |
| null | Option<serde_json::Value> | Unknown nullable type |
| array | Vec<T> | T inferred from first element |
| object | Struct | New named struct generated |
| key with non-snake_case | #[serde(rename)] | Automatic rename attribute |