JSON to Rust
Rust structs with serde derives, Option and Vec, from JSON.
About this tool
Paste JSON and get Rust structs for serde. Choose whether to derive Clone and PartialEq alongside Debug, Serialize and Deserialize, and copy the code into your crate.
How the Rust is generated
Each JSON object becomes a pub struct and each key a snake_case field (userName โ user_name). #[serde(rename)] is added only where the JSON key differs. Nested objects become their own structs, arrays Vec<T>, whole numbers i64, decimals f64 and booleans bool. Array items are merged before types are chosen, so a key that only some items have, or that is sometimes null, is treated as optional or nullable rather than breaking decoding. A sometimes-null value becomes Option<T>, and a sometimes-missing key also gets #[serde(default, skip_serializing_if = "Option::is_none")]. Rust keywords such as type become raw identifiers (r#type), which serde maps back to the original name. Honest limits: types describe your sample. Always-null fields become serde_json::Value, and you need serde (with the derive feature) and serde_json in Cargo.toml. JSON to Go, JSON to TypeScript and the JSON formatter are related.
Frequently asked questions
How do I convert JSON to a Rust struct?
Paste the JSON here and copy the structs. Add serde with the derive feature and serde_json to Cargo.toml.
How are optional fields handled?
As Option<T> with serde's default attribute, so missing keys decode as None.
What about keys like 'type'?
Rust keywords become raw identifiers such as r#type, which serde maps back to 'type'.
Is my JSON uploaded?
No. Everything runs in your browser and nothing is stored or sent.