JSON to Rust Struct Generator (serde)
Convert JSON to Rust structs online with serde Serialize/Deserialize derives, Option<T> for optional fields and snake_case names. Free and runs in your browser.
π Runs entirely in your browser β nothing is uploadedRust structs for serde, generated from JSON
Serde makes JSON in Rust fast and safe, but only once you have written structs that match the data exactly. This JSON to Rust converter writes them for you. Paste a sample and every object becomes a struct deriving Serialize and Deserialize, keys become idiomatic snake_case field names with #[serde(rename)] attributes where needed, nested objects become separate structs, arrays become Vec<T>, and values that are missing or null somewhere in the sample are wrapped in Option<T>. Integers and floats are distinguished as i64 and f64.
The generator is built on quicktype, which merges all elements of every array before choosing a type. That matters in Rust more than most languages: a single unexpected key or null makes deserialization fail, so capturing optional fields up front saves debugging time.
Visibility and derives
Pick field visibility to match how the types will be used β pub for a shared models crate, pub(crate) for internal modules, or private fields when you want to add your own constructors. Debug and Clone derives are on by default because almost every model needs them; switch them off for leaner output. Enable Skip serializing None to add skip_serializing_if so None fields disappear from outgoing JSON instead of being written as null. Remember to add serde with the derive feature and serde_json to your Cargo.toml.
Getting accurate types
Use a sample with several array items and optional fields filled in. Empty arrays and nulls give no type information and fall back to serde_json::Value, which you will usually want to replace. After generating, run rustfmt, review integer widths (an ID may fit u32), and turn string fields with a fixed set of values into enums with #[serde(rename_all)]. Everything runs locally in your browser, so private payloads are never uploaded.
How to use
- Paste JSONPaste a JSON document or click Load sample.
- Tune derivesSet the root struct name, field visibility and which traits to derive.
- Copy the structsCopy the Rust code or download a .rs file and add serde to Cargo.toml.
Frequently asked questions
Does the output work with serde?
Serialize and Deserialize, and fields carry #[serde(rename = "...")] wherever the Rust name differs from the JSON key.How are optional fields represented?
Option<T>. Tick Skip serializing None to omit them when serializing.Can I change field visibility?
pub, pub(crate) or private fields, and toggle the Debug and Clone derives.