JSON to Rust Struct
Generate serde-annotated Rust structs from sample JSON.
Last updated: August 2026
Generates serde-annotated Rust structs. Add the serde crate to your project.
Was this tool helpful?
Serde structs from a JSON sample
In Rust, JSON work almost always goes through serde and serde_json. That means defining structs that derive Deserialize (and often Serialize) with field names that match the JSON. This generator reads a sample and produces those structs, adding #[serde(rename)] when a JSON key is not a valid Rust identifier.
Example
Input:
{ "userId": 7, "name": "Ada", "verified": true }Generated Rust:
use serde::Deserialize;
#[derive(Deserialize)]
struct Root {
#[serde(rename = "userId")]
user_id: i64,
name: String,
verified: bool,
}Rust convention is snake_case fields, so userId maps to user_id with a rename. For a whole payload in camelCase, #[serde(rename_all = "camelCase")] on the struct is cleaner than one rename per field.
Ownership and optionality decisions
The generator defaults to owned types like String, which is the right call for deserialization since the data must be owned somewhere. The real manual work is optionality: a field that may be missing needs Option<T>, otherwise serde errors on absent keys. Integers default to i64 to be safe, but a value that can exceed that range needs u64 or a bignum type.
FAQ
Why i64 instead of i32? JSON does not bound integer size, so a wide default avoids overflow on deserialize. Narrow it only if you know the range.
How do I handle fields that may be absent? Wrap them in Option and add #[serde(default)] so missing keys deserialize to None rather than failing.
What about an untagged union? A field that is one of several shapes maps to a serde enum with #[serde(untagged)], which a sample-based generator cannot infer on its own.