JSON to Rust Structs
Paste JSON to generate Rust structs with serde derive macros. 100% client-side.
How to Use the JSON to Rust Converter
- Paste your JSON into the input area on the left. The tool accepts any valid JSON object or array.
- Choose a mode — Serde (default), Serde + Option (all fields wrapped in Option), or Public Fields (adds
pubvisibility). - View the generated Rust structs on the right with proper field names and type mappings.
- Copy or download the output as a
.rsfile ready to paste into your project.
What This Tool Generates
This converter analyzes your JSON structure and generates idiomatic Rust structs with #[derive(Debug, Serialize, Deserialize)] macros from the serde framework. It handles nested objects as separate structs, arrays as Vec<T>, and null values as Option<T>.
JSON to Rust Type Mapping
- JSON string →
String - JSON integer →
i64 - JSON float →
f64 - JSON boolean →
bool - JSON array →
Vec<T>(type inferred from first element) - JSON object → nested
struct - JSON null →
Option<serde_json::Value> - Mixed arrays →
Vec<serde_json::Value>
Field Name Conventions
JSON keys are converted to snake_case for Rust field names following the Rust naming convention. If the original JSON key differs from the snake_case conversion, a #[serde(rename = "original_key")] attribute is added automatically, ensuring correct deserialization. Struct names are generated in PascalCase, with the root struct named Root and nested structs named after their parent field.
Cargo.toml Dependencies
To use the generated code, add these dependencies to your Cargo.toml:
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Mode Differences
- Serde — generates the most idiomatic code. Fields are the exact types inferred from the JSON. Use this when your JSON structure is fixed and reliable.
- Serde + Option — wraps all fields in
Option<T>. Use this when consuming APIs that may omit fields or return null values. Prevents deserialization panics when fields are absent. - Public Fields — adds
pubvisibility to all struct fields. Use this when the struct is in a library module and fields need to be accessed from outside the module.
Working with the Generated Code
After generating your structs, use serde_json::from_str() to deserialize JSON into the struct and serde_json::to_string() to serialize back to JSON. For API clients built with reqwest, you can use .json::<Root>() directly on the response, which handles deserialization automatically. The generated structs are also compatible with actix-web, axum, and other Rust web frameworks that support serde types.
Handling Complex JSON Structures
When your JSON contains deeply nested objects, the converter generates a separate struct for each level of nesting. This produces clean, reusable code but may result in many struct definitions for complex data. For simple cases where you need flexibility, consider using serde_json::Value directly, which can represent any valid JSON value without a predefined schema. The generated code is a starting point — you should review and adjust types (for example, changing i64 to u32 if values are always non-negative) based on your specific use case.