"category": "code-generator",
JSON to Rust Struct Generator
"tldr": Paste JSON and get Rust structs with serde derives - snake_case fields, #[serde(rename)] where needed, ready for serde_json.
Paste a JSON payload and get Rust struct definitions with #[derive(Serialize, Deserialize)] ready for serde_json. Field names are converted to snake_case with #[serde(rename)] preserving the original keys, integers map to i64, floats to f64, and nested objects become their own structs.
Rust makes you pay for JSON shape mismatches at parse time, which is exactly why generating structs from a real payload is so effective: the compiler and serde together guarantee that if it parses, the data is the shape you declared.
How to convert JSON to Rust structs
- 1Paste a representative JSON payload into the input panel.
- 2CamelCase keys are converted to snake_case fields with #[serde(rename)] attributes.
- 3Nested objects become separate structs, defined before the structs that use them.
- 4Copy the structs and parse with serde_json::from_str.
Convert JSON to Rust struct in code
use serde_json;
let order: Root = serde_json::from_str(&body)?;
println!("{} - {}", order.customer.name, order.total);#[derive(Debug, Serialize, Deserialize)]
pub struct Root {
#[serde(default)]
pub notes: Option<String>, // absent or null -> None
}Frequently asked questions
How do I handle fields that may be absent or null?
Wrap them in Option<T> - serde maps both a JSON null and a missing key (with #[serde(default)]) to None. A null in your sample generates serde_json::Value as a placeholder; replace it with Option of the real type once you know it.
Why i64 and f64 instead of i32 and f32?
JSON numbers are unbounded and IDs/timestamps overflow i32 routinely. i64/f64 parse safely; narrow specific fields deliberately when the range is known. For numbers beyond even i64 (or precise decimals), parse into String and convert with a crate like rust_decimal.
What about unknown extra fields in the JSON?
serde ignores unknown fields by default. Add #[serde(deny_unknown_fields)] to a struct to make extras a hard error - useful for config files where a typo should fail loudly rather than be silently ignored.
Can I generate these at build time instead?
For schemas that change, look at typify (JSON Schema to Rust) or write a build.rs step. For a stable API, pasting a sample here and committing the generated structs is simpler and keeps compile times down.
Last updated:
You might also need
Free and in-browser, like everything on JSON Console. Browse all tools →