JSON to Rust Struct Generator — Free Online Tool
Convert JSON to Rust struct definitions with serde Deserialize/Serialize derives and proper types using AI. Free, no sign-up.
JSON to Rust Struct Generator — Free Online Tool
About JSON to Rust Struct Generator — Free Online Tool
JSON to Rust Struct converts JSON objects into Rust struct definitions annotated with serde's Deserialize and Serialize derives, mapping each JSON field to its correct Rust type. Developers use it to eliminate hand-writing boilerplate when integrating with APIs, config files, or any JSON data source in Rust projects.
JSON to Rust Type Mapping
| JSON Type | Example Value | Rust Type | Notes |
|---|---|---|---|
| string | "hello" | String | Owned heap string; use &str only with lifetime annotations |
| number (integer) | 42 | i64 | Defaults to i64; use u64 for unsigned, i32/u32 to save space |
| number (float) | 3.14 | f64 | Rust has no JSON float ambiguity; f64 matches JSON number spec |
| boolean | true | bool | Direct 1-to-1 mapping |
| null | null | Option<T> | Nullable fields become Option<T> wrapping the inner type |
| array | [1,2,3] | Vec<T> | Homogeneous arrays become Vec<T>; mixed arrays need serde_json::Value |
| object | {"k":"v"} | struct / HashMap<String,T> | Named objects become structs; dynamic keys become HashMap<String,T> |
| nested object | {"a":{"b":1}} | Nested struct | Each nested object generates a separate named struct definition |
serde Attribute Reference for Generated Structs
| Attribute | Usage | Effect |
|---|---|---|
| #[derive(Serialize, Deserialize)] | On struct | Enables serde JSON encode/decode for the type |
| #[serde(rename = "fieldName")] | On field | Maps a camelCase or hyphenated JSON key to a Rust snake_case field |
| #[serde(rename_all = "camelCase")] | On struct | Automatically renames all fields on serialize/deserialize |
| #[serde(default)] | On field | Uses Default::default() when the JSON key is missing |
| #[serde(skip_serializing_if = "Option::is_none")] | On field | Omits null/None fields from serialized JSON output |
| #[serde(flatten)] | On field | Merges a nested struct's fields into the parent JSON object |
| #[serde(untagged)] | On enum | Deserializes union types without a discriminator key |
| #[serde(alias = "alt_name")] | On field | Accepts multiple JSON key names for the same Rust field |
Frequently Asked Questions
How do I deserialize JSON into a Rust struct with serde?
Add serde = { version = "1", features = ["derive"] } and serde_json = "1" to your Cargo.toml dependencies. Annotate your struct with #[derive(Serialize, Deserialize)], then call serde_json::from_str(&json_string) to parse JSON into the struct. The tool generates the struct definition and derive annotations automatically so you can paste and compile immediately.
How does serde handle optional or nullable JSON fields in Rust?
Fields that can be null or absent in JSON should be typed as Option<T> in Rust. Use #[serde(default)] if the key may be missing entirely, and #[serde(skip_serializing_if = "Option::is_none")] to omit None values when serializing back to JSON. The generator wraps any field whose sampled value is null in Option<T> automatically.
How do I map camelCase JSON keys to snake_case Rust field names?
Add #[serde(rename_all = "camelCase")] at the struct level to automatically convert between camelCase JSON keys and snake_case Rust fields during both serialization and deserialization. For individual fields with irregular names, use #[serde(rename = "originalKey")]. The generated structs include the appropriate attribute based on the input JSON key casing.
What Rust type should I use for JSON numbers?
serde_json maps JSON integers to i64 and JSON floats to f64 by default, which matches the JSON specification's number range. You can manually narrow types to i32, u32, or f32 after generation if memory or FFI constraints require it. Avoid u64 for values that may be negative; the deserializer will return an error at runtime if the JSON value overflows the chosen type.
Can I deserialize JSON arrays with mixed types into Rust?
Rust's type system requires homogeneous Vec<T> for arrays, so mixed-type JSON arrays (e.g., [1, "text", true]) cannot be directly typed as Vec<T>. Use Vec<serde_json::Value> to accept any JSON value, then match on the variant at runtime. If the array is always one type at runtime but the sample was mixed, override the generated type to Vec<YourType> after inspection.
If jsondecode.com saved you time, share it with your team
Free forever. No ads. No sign-up. Help other developers find it.