How it works
Paste a JSON sample and get Rust structs that derive serde’s Serialize and Deserialize:
- Every object becomes a
pub structnamed after its property (customer→Customer,items→Item). The root isRootunless you rename it. - Fields are snake_case. When keys follow camelCase consistently the struct gets
rename_all; otherwise each differing field gets arename. - Arrays of objects are merged: keys that only some items have become
Option<T>fields. - Arrays become
Vec<T>, empty objectsHashMap<String, serde_json::Value>, and values with no single typeserde_json::Value. - A root array or primitive becomes a type alias, for example
pub type Root = Vec<RootItem>;.
Example
{
"user_id": 42,
"name": "Linh",
"tags": ["admin"],
"addresses": [
{ "city": "Hanoi", "zip": "100000", "note": null },
{ "city": "HCMC", "zip": "700000", "primary": true, "note": "Gate 2" }
]
}
becomes
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Root {
pub user_id: i64,
pub name: String,
pub tags: Vec<String>,
pub addresses: Vec<Address>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Address {
pub city: String,
pub zip: String,
pub note: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub primary: Option<bool>,
}
A camelCase API such as {"userId": 42, "displayName": "Linh"} gives a single attribute:
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Root {
pub user_id: i64,
pub display_name: String,
}
Options
- Root name: name of the top-level struct.
- Derive Default: add
Defaultto the derive list, handy for builders and tests. - pub fields: make fields public (structs are always
pub).
Reserved words and odd keys
Keywords become raw identifiers, which serde maps back to the plain name: type → r#type. self, super and crate cannot be raw identifiers, so they become self_ with a rename. Keys starting with a digit get a field_ prefix (2fa → field_2fa), and keys with spaces are joined with underscores (first name → first_name). Structs that would shadow String, Vec, Option or Value get a Type suffix.
Deserializing
let root: Root = serde_json::from_str(&text)?;
serde ignores unknown keys by default. Add #[serde(deny_unknown_fields)] to a struct if extra keys should be an error.
Common mistakes
- Generating from a single object. No field is optional, and a later response without a key fails with “missing field”. Paste several real responses.
- Integers that are really floats.
25becomesi64, and a later25.5fails with “invalid type: floating point”. Change the field tof64. - Forgetting the derive feature. Without
features = ["derive"]on serde,#[derive(Serialize)]cannot be found.
Same payload in other languages: JSON to Swift, JSON to Kotlin, JSON to Go or JSON to TypeScript.
Frequently asked questions
Which crates do I need?
`serde` with the `derive` feature and `serde_json`: `cargo add serde --features derive` and `cargo add serde_json`. Unknown values use the fully qualified `serde_json::Value`, and empty objects use `std::collections::HashMap`, which is imported only when needed.
When is rename_all used instead of rename?
When every key in a struct is exactly the camelCase (or PascalCase) form of its snake_case field, as serde computes it, the struct gets one `#[serde(rename_all = "camelCase")]`. If even one key does not follow the rule, each differing field gets its own `#[serde(rename = "...")]` instead, so the mapping is always exact.
What is the difference between a nullable and an optional field?
Both become `Option<T>`, and serde reads a missing `Option` field as `None`. Optional fields (keys missing from some objects) also get `skip_serializing_if = "Option::is_none"`, so serializing leaves the key out instead of writing `null`. Fields that were null but always present keep writing `null`.
How are numbers typed?
`i64` for integers, `u64` when a value is above `i64::MAX`, and `f64` for fractions. Integers outside both ranges become `f64` with a warning, because serde_json reads them as floats unless its `arbitrary_precision` feature is on.
Is my JSON uploaded?
No. The structs are generated entirely in your browser; nothing is sent to a server.
Last updated