JSON to Rust struct: from JSON to code
Paste JSON and get Rust structs that derive Serialize and Deserialize for serde. Fields are snake_case, numbers are i32 or f64, lists Vec<T>, text String, and a field that may be missing becomes Option<T> with skip_serializing_if so that None is not written back as null. A value that can be anything stays serde_json::Value.
This page opens the converter on Rust. Paste your JSON, choose the root name and the flavour, and copy the result; everything is generated in your browser and the JSON is never sent anywhere.
A real conversion
The tool opens with this JSON, an order with a customer and two items:
{
"id": 48213,
"status": "paid",
"paid_at": "2026-11-21T14:03:00Z",
"total": 389.3,
"coupon": null,
"customer": { "name": "Ana Souza", "email": "ana@example.com", "vip": true },
"items": [
{ "sku": "KB-01", "qty": 2, "price": 149.9, "tags": ["usb", "keyboard"] },
{ "sku": "MS-07", "qty": 1, "price": 89.5 }
]
}and this is what it writes for Rust:
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Root {
pub id: i32,
pub status: String,
pub paid_at: String,
pub total: f64,
pub coupon: serde_json::Value,
pub customer: Customer,
pub items: Vec<Item>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Customer {
pub name: String,
pub email: String,
pub vip: bool,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Item {
pub sku: String,
pub qty: i32,
pub price: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<Vec<String>>,
}How each JSON value is typed
| In the JSON | In Rust |
|---|---|
| string | String |
| integer | i32 |
| decimal | f64 |
| true / false | bool |
| null | serde_json::Value |
| array of items | Vec<Item> |
| field missing in some items | Option<Vec<String>> |
The second item of the sample has no "tags" key, and the first has one: that is why the tool treats it as optional.
Awkward keys, null and empty lists
{"user_id": 7, "display name": "Ana", "tags": [], "address": null, "scores": [9.5, 8], "meta": {"2fa": true}, "mixed": [1, "a"]}use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Account {
pub user_id: i32,
#[serde(rename = "display name")]
pub display_name: String,
pub tags: Vec<serde_json::Value>,
pub address: serde_json::Value,
pub scores: Vec<f64>,
pub meta: Meta,
pub mixed: Vec<serde_json::Value>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Meta {
#[serde(rename = "2fa")]
pub f_2fa: bool,
}Keys that are not valid Rust names get #[serde(rename = "…")] ("display name" becomes display_name, "2fa" becomes f_2fa), so the JSON still maps back to the right field. null and the empty array become serde_json::Value and Vec<serde_json::Value>.
The other flavour
The same small JSON, {"userId": 1, "paidAt": "2026-11-21"}, in the default flavour and in the "rename-all" one:
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Order {
#[serde(rename = "userId")]
pub user_id: i32,
#[serde(rename = "paidAt")]
pub paid_at: String,
}use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Order {
pub user_id: i32,
pub paid_at: String,
}The rename-all flavour adds #[serde(rename_all = "camelCase")] to each struct, which is the right choice when the JSON uses camelCase keys, and drops the per-field renames.
Reading the JSON with serde_json
let order: Root = serde_json::from_str(&text)?;
println!("{}", order.customer.name);Add serde = { version = "1", features = ["derive"] } and serde_json to Cargo.toml. from_str returns an error that names the line and column of the first problem. For integers larger than i32 change the type to i64 or u64.
Common mistakes
- A missing field that is not an Option: serde fails with "missing field". Mark it optional, add #[serde(default)] or use the "all optional" switch.
- Using f64 for money: use a decimal type such as rust_decimal when cents must be exact.
How to use the converter
- Paste your JSON on the left (or keep the example); a mistake is reported with its line and column.
- Set the root name and the flavour; the code updates as you type.
- Copy the code or download it as a file.
Frequently asked questions
How do I convert JSON to a Rust struct?
Paste the JSON and copy the structs; add serde and serde_json and call serde_json::from_str.
Why is the integer type i32 and not i64?
The tool picks the smallest common type that fits the numbers in the sample. If your data can have bigger values, change it to i64 or u64 by hand.
Does it handle unknown fields?
serde ignores keys that have no field by default. Add #[serde(deny_unknown_fields)] on a struct if you want an error instead.