How to use the json to rust struct converter
- Paste your JSON object or array into the JSON input field.
- Set the Root struct name to choose the identifier for the top-level struct.
- Adjust the Derive macros field if you need specific traits like
SerializeorDeserialize. - Copy the generated Rust code directly from the output area.
How the conversion works
The converter parses the JSON using the browser's built-in parser and walks the resulting tree. Each JSON object becomes a Rust struct, and its keys become fields. Keys are transformed from snake_case, camelCase or kebab-case into valid Rust identifiers. If a key matches a Rust reserved word like type or match, the tool appends an underscore to the field name and, when Serialize or Deserialize is in the derive list, adds a #[serde(rename = "original")] attribute so serialization continues to work correctly. The same attribute is added whenever the Rust name differs from the JSON key, such as isOk becoming is_ok.
Nested objects generate additional structs named after the capitalized key. For example, a profile object inside a user object produces a Profile struct, and the parent field uses that type. Arrays map to Vec<T>, where T is inferred from all elements, so objects are merged and keys missing from some become Option. Null values map to Option<serde_json::Value> since the underlying type cannot be determined without more data.
Type mapping reference
Understanding how JSON types translate to Rust types helps when writing deserialization logic. Here is the mapping used by this json to rust struct converter:
| JSON Type | Rust Type | Notes |
|---|---|---|
| String | String | Unicode is preserved exactly. |
| Integer number | i64 | Whole numbers within safe range. |
| Fractional number | f64 | IEEE 754 double precision. |
| Boolean | bool | true or false. |
| Null | Option<serde_json::Value> | Type is unknown without schema. |
| Array | Vec<T> | T inferred from all elements. |
| Object | Named struct | Generates a new struct definition. |
Working with Serde
The generated structs include #[derive(...)] attributes based on your input. To use Serialize and Deserialize, add the serde crate to your Cargo.toml with the derive feature enabled:
serde = { version = "1.0", features = ["derive"] }
You will also typically need serde_json for parsing. When the converter encounters a key that requires renaming, it emits the #[serde(rename = "...")] attribute automatically. This ensures that round-tripping through serde_json::to_string and serde_json::from_str preserves the original JSON keys exactly, even when they contain hyphens or spaces.
Handling edge cases
Real-world JSON often contains inconsistencies that require attention. Mixed-type arrays, such as [1, "two", true], fall back to Vec<serde_json::Value> because Rust vectors must hold a single type. Empty arrays produce Vec<serde_json::Value> as well, since there is no element to infer from.
If your JSON root is an array of objects, the converter merges all the objects into one struct and wraps it in a type alias like type RootList = Vec<Root>;. Deeply nested structures generate multiple structs, each defined before it is referenced so the output compiles without reordering. Unicode keys and values pass through unchanged, as both JSON and Rust strings support full UTF-8.
Worked examples
| Example | Input | Result |
|---|---|---|
| Simple user | JSON input: {"name": "Ada", "age": 36, "active": true}; Root struct name: User; Derive macros: Debug, Clone, Serialize, Deserialize | use serde::{Deserialize, Serialize}; / #[derive(Debug, Clone, Serialize, Deserialize)] / pub struct User { / pub name: String, / pub age: i64, / pub active: bool, / } |
| Nested object | JSON input: {"id": 1, "profile": {"bio": "Hello", "score": 9.5}}; Root struct name: Account; Derive macros: Debug, Serialize, Deserialize | use serde::{Deserialize, Serialize}; / #[derive(Debug, Serialize, Deserialize)] / pub struct Profile { / pub bio: String, / pub score: f64, / } / #[derive(Debug, Serialize, Deserialize)] / pub struct Account { / pub id: i64, / pub profile: Profile, / } |
| Array of items | JSON input: [{"sku": "A1", "price": 12.5}]; Root struct name: Product; Derive macros: Debug, Clone | #[derive(Debug, Clone)] / pub struct Product { / pub sku: String, / pub price: f64, / } / pub type ProductList = Vec<Product>; |
FAQ
Does this tool upload my JSON to a server?
No. The json to rust struct conversion happens entirely in your browser using JavaScript. Nothing is sent over the network.
How are reserved Rust keywords handled?
If a JSON key matches a Rust keyword like type, struct or match, the field is renamed with an underscore suffix and a Serde rename attribute is added to preserve the original key during serialization.
What happens with null values in the JSON?
Since the actual type behind a null is unknown, the converter maps it to an optional serde_json::Value. If you know the expected type, you can manually change it to an optional String or another appropriate type.
Can I convert an array of JSON objects?
Yes. Paste a JSON array and the tool merges all the objects into one struct, marking keys that are missing or null in some objects as Option, then creates a type alias wrapping it in a Vec.
Which crates do I need to compile the output?
You need the serde crate with the derive feature enabled. If you included Serialize or Deserialize in the derive list, you will also need serde_json for parsing and emitting JSON.
How are nested objects named?
Each nested object generates a new struct whose name is the PascalCase version of the JSON key. For example, a billing_address key produces a BillingAddress struct.