JSON to Rust Structs
Generate Rust structs from JSON with Serde `Serialize/Deserialize` macros. 100% client-side. Automatically handles snake_case conversion and Option<T>.
Convert any JSON payload into idiomatic Rust structs ready for the
serde_json crate.
This generator automatically converts JSON camelCase keys to Rust's required snake_case, mapping the original keys using
#[serde(rename)].
It also extracts nested objects into separate structs and supports wrapping types in
Option<T> for safe deserialization of missing fields.
Related tools
Frequently asked questions
What is Serde and why use it for Rust JSON?
Serde is the standard framework for serializing and deserializing Rust data structures efficiently and generically. When combined with the serde_json crate, it allows you to automatically parse JSON strings directly into strongly-typed Rust structs using the #[derive(Serialize, Deserialize)] macros.
How do I use the generated structs to deserialize JSON?
First, ensure you have serde and serde_json in your Cargo.toml. Copy the generated code into your Rust project. Then use serde_json::from_str(&json_string) to parse the JSON directly into your Root struct: let data: Root = serde_json::from_str(&json_string).unwrap();
How does this handle Option<T> for missing fields?
If you enable the "Use Option<T>" setting, all fields will be wrapped in Rust's Option enum (e.g., Option<String>). This is crucial when dealing with JSON APIs where certain fields might be null or completely missing from the payload, preventing deserialization errors.
Can I rename fields to camelCase in Serde?
Yes! Rust convention strictly enforces snake_case for struct fields, while JSON often uses camelCase. The generator automatically converts your JSON keys to snake_case for the Rust fields and attaches a #[serde(rename = "originalKey")] attribute so Serde knows how to map them during deserialization.
How do I add Serde as a dependency in Cargo?
Run the command cargo add serde -F derive followed by cargo add serde_json. This adds both crates to your Cargo.toml and enables the derive feature required for the macros to work.