UTILS.
100% in-browser
🦀

JSON to Rust Serde Structs

Generate Rust structs with serde derives from a JSON sample, ready to Serialize/Deserialize, with Option for nullable fields and rename attributes.

output

About this tool

The JSON to Rust Serde Structs tool converts a pasted JSON sample into Rust structs annotated for serde, so you can deserialize the same payload without hand-writing every field. It parses the JSON, infers Rust types recursively, and emits a #[derive(Serialize, Deserialize, ...)] struct per object: strings become String, integers i64 (or u64 when all samples are non-negative), other numbers f64, booleans bool, arrays Vec<T>, and nested objects their own PascalCase structs defined before their parents.

All work happens locally in your browser with JSON.parse and plain JavaScript — nothing is uploaded. Struct fields are converted to snake_case, and whenever a JSON key differs from the snake_case field the tool adds #[serde(rename = "originalKey")] so the struct still maps to the wire format. Keys that are absent in some sibling samples, or seen as null, become Option<T> with #[serde(default)] so deserialization tolerates their absence. Empty arrays fall back to Vec<serde_json::Value>.

The integer-width toggle switches between 64-bit (i64/u64, default) and 32-bit (i32/u32) integers, promoting to 64-bit automatically when a sampled value exceeds the 32-bit range. The derive toggle chooses between the full set (Serialize, Deserialize, Debug, Clone) and a minimal Serialize, Deserialize. The file starts with use serde::{Serialize, Deserialize};. Copy it or download a .rs file.

Frequently asked questions

How are JSON types mapped to Rust?
string to String, integer to i64 (or u64 when every sample is non-negative), non-integer number to f64, boolean to bool, array to Vec<T>, and object to a named struct with serde derives. Null or missing fields become Option<T> with #[serde(default)].
When is #[serde(rename)] added?
Whenever the JSON key does not equal its snake_case field name — for example a key userName produces field user_name with #[serde(rename = "userName")], so the struct still deserializes the original JSON exactly.
Can I choose the integer width?
Yes. The width toggle emits 64-bit (i64/u64) or 32-bit (i32/u32) integers. Signedness follows the data: all-non-negative samples use the unsigned type. Values beyond the 32-bit range are promoted to 64-bit automatically.
What derives are included?
The default is #[derive(Serialize, Deserialize, Debug, Clone)]; the minimal option emits just Serialize, Deserialize. Child structs are declared before the parents that reference them so the module compiles as-is.

More tools