JSON to Rust SerdeStructs Conversion — Numio
Generates Rust structs with serde Serialize and Deserialize derives from a JSON sample. Fields become snake_case with a rename_all attribute, keys missing from some array items become Option<T>, and null values become serde_json::Value.
Typing a third-party API response before deserialising it with serde_json in a reqwest client.
JSON
Rust Serde
When to use it
- Typing a third-party API response before deserialising it with serde_json in a reqwest client.
- Writing structs for a JSON config or fixture file in a Rust CLI or Axum service.
How to use
- Paste the JSON sample; if a field is optional, include array items both with and without it.
- Open the settings and choose Property Name Format to match the keys in your JSON (default camelCase). It sets #[serde(rename_all = ...)]; keys that do not fit get #[serde(rename = ...)].
- Copy the code and add serde (with derive), serde_derive and serde_json to Cargo.toml, as the generated use lines expect.
JSON input
{
"orderId": "A-1001",
"total": 19.5,
"paid": true,
"coupon": null,
"items": [
{ "sku": "PEN-01", "qty": 2 },
{ "sku": "BOOK-7", "qty": 1, "giftWrap": true }
]
}Rust structs (serde)
use serde_derive::Deserialize;
use serde_derive::Serialize;
use serde_json::Value;
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Root {
pub order_id: String,
pub total: f64,
pub paid: bool,
pub coupon: Value,
pub items: Vec<Item>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Item {
pub sku: String,
pub qty: i64,
pub gift_wrap: Option<bool>,
}Details and limitations
Generation runs in your browser with the json_typegen WebAssembly module; nothing is sent to a server.
Integers become i64, decimals f64. null and empty arrays give serde_json::Value and Vec<Value>; replace them with concrete types such as Option<String>.
Rust keywords are renamed: a key named type becomes type_field with #[serde(rename = "type")].
Every struct derives Default, Debug, Clone and PartialEq as well as Serialize and Deserialize; remove the ones you do not need.
FAQ
What does the Property Name Format setting change?
It describes how the keys in your JSON are written, not the Rust field names. Fields are always snake_case; the setting picks the rename_all value so serde maps them back to camelCase, kebab-case, PascalCase and so on.
How are optional fields detected?
When objects in the same array have different keys, keys missing from some items become Option<T>. A field present in every sample item is not optional, even if your API sometimes omits it.
Can the root of the JSON be an array?
Yes. A root array produces pub type Root = Vec<Root2>; with a struct Root2 for the items.