JSON to Rust Converter – Free, Online, Private
Generate Serde structs from a JSON sample, with rename attributes when the Rust field name cannot match the key.
How to convert JSON to Rust
- Paste JSON. Objects become structs that derive Serialize and Deserialize.
- Copy the snippet under your use serde line. The header import is included.
- Wrap fields in Option if the API omits them more often than this sample shows.
Serde shape
The generator emits pub struct types with Debug, Clone, Serialize and Deserialize. Field names are snake_case. A json key that already matches the field needs no attribute. A camelCase key gets #[serde(rename = "...")]. Integers are i64, other numbers f64, strings String, booleans bool. Arrays are Vec. Nested objects are their own structs. A null or a key missing from some array elements becomes Option. Optional fields also get default and skip_serializing_if so a None does not have to appear on the way back out.
Names Rust will not accept
A field cannot be named type, self or other keywords. Those names are raw identifiers or renamed. The JSON key is always preserved via rename when the Rust name differs, so the wire format does not change. The root struct is Root. There is no lifetime, no generic, and no untagged enum. If the payload is sometimes an object and sometimes a string, Serde needs an enum you write by hand. The sample-based struct only covers the object shape you pasted.
Worked example
Root { id: i64, name: String, active: bool, tags: Vec<String> } matches the sample. tags is a vector of owned strings. No rename attributes appear because the keys are already snake-free identifiers. Deserialize with serde_json::from_str::<Root>(&text). The error type will point at the first mismatched field, which is the usual way you discover that the sample was incomplete. Add the missing key to a new sample and run this page again rather than editing Option in by guesswork if you have a second payload.
Numbers and privacy
f64 and i64 cannot both be right for a field that is sometimes 1 and sometimes 1.5; the merge prefers f64. JSON numbers bigger than 2^53 are not safe in the browser's parser, so a snowflake id should be a string on the wire before you generate a struct. The page does not call crates.io and does not upload the sample. Paste the struct into the crate that already depends on serde and serde_json.
Frequently asked questions
Which Serde features are assumed?
Derive macros for Serialize and Deserialize. You need serde with the derive feature, which is the default in current serde.
Why Option?
Null in the sample, or a key missing from one object in an array, becomes Option. A key present on every object stays required.
Can I use a borrowed &str?
The fields own their strings. Borrowed deserialization needs lifetimes this snippet does not add.
Is the JSON uploaded?
No. Rust source is generated in the page.