JSON to Rust
This JSON to Rust tool generates structs with serde derives and type annotations. Optional fields are wrapped in Option<T> automatically and nested objects and Vec<T> are expanded fully. Generation runs locally in your browser and the data is not uploaded.
How to use it
- 1
Paste your JSON
Paste JSON data; snake_case keys are converted to Rust naming style with serde rename preserving the mapping.
- 2
Set the root struct name
The default is User; change it to suit your domain. Field names follow the Rust snake_case convention.
- 3
Copy the Rust code
Structs with #[derive(Serialize, Deserialize)] are generated, ready to parse with serde_json.
- 4
Add the dependencies
Add serde with the derive feature and serde_json to your Cargo.toml.
Key features
Native serde ecosystem
Automatically derives Serialize/Deserialize/Debug/Clone, so serde_json works out of the box.
Precise Option annotations
Null and missing fields become Option<T>, forcing the type system to handle empty values.
Vec and nested structures
Arrays become Vec<T> and nested objects become separate structs, so complex JSON is modelled in one pass.
Key renaming
Non-snake keys keep their original mapping through #[serde(rename = "...")].
Dependency notes included
The header comment documents the required Cargo dependencies and a usage example.
Local and private
Generation runs in your browser and the data is not uploaded.
Common errors & troubleshooting
- ! serde errors on a missing non-Option field by default, so keep Option<T> for fields the API may omit.
- ! A mixed-type field is often better modelled as an enum, which is more idiomatic and safer than an untagged dynamic value.
- ! For a large integer that i64 cannot hold, switch to i128 or String by hand to avoid precision issues.
- ! Review Option fields against the API contract if you want to treat a field as required.
Frequently asked questions
Why are some field types Option?
Rust has no null, so nullable values must be expressed explicitly. When a sample contains null or the key is missing, the field is marked Option<T> and read with if let / match, eliminating null-pointer issues at compile time.
Which Cargo dependencies are needed?
serde (with features = ["derive"]) and serde_json are enough; the header comment in the generated code also lists the dependencies and a usage example.
Is the JSON key style preserved?
Rust fields use snake_case, and keys that differ from the JSON get an automatic #[serde(rename = "originalKey")] so serialisation and deserialisation stay consistent with the original JSON.
Is my data uploaded?
No. Generation runs locally in your browser.
Related tools
JSON to Go
Generate Go structs from JSON with json tags. Nested objects become well-named sub-structs, with a custom package name and configurable omitempty style. Runs locally in your browser.
JSON to Swift
Generate Swift models from JSON that conform to Codable. CodingKeys handle snake_case mapping, and optional properties are inferred. Choose struct or class. Runs locally in your browser.
JSON to Kotlin
Generate Kotlin data classes from JSON. Supports Jackson, kotlinx.serialization and Klaxon, with a custom package name and accurate nullable (?) type annotations. Runs locally.
JSON Formatter
Format, beautify and validate JSON in your browser. Errors point to the exact line and column, and you can indent with 2 spaces, 4 spaces or a tab.