JSON to Rust serde Struct
serde structs with derive macros, rename mappings, Vec arrays and i64/f64 dispatch.
The standard answer to JSON in Rust is serde: structs carrying derive(Serialize, Deserialize) with deserialization synthesized at compile time. This tool expands JSON into a set of pub structs — nested objects modeled independently, fields public, and #[serde(rename)] wherever the JSON key diverges from Rust naming.
Type mapping follows Rust habits: integers i64, decimals f64, bool, String, Vec<T> for arrays; JSON null maps to serde_json::Value to stay honest (Rust has no implicit null). Array roots produce a pub type alias.
camelCase conversion vs rename
Rust convention is snake_case fields while JSON APIs speak camelCase. Two routes: #[serde(rename_all = "camelCase")] on the struct (this tool's default, one line for every field) and per-field #[serde(rename = "...")] for the irregular keys.
null and Option — the manual polish
JSON null maps to serde_json::Value here ("anything can arrive" said honestly). When the field is really a "maybe-missing string", change the type to Option<String> and add #[serde(default)] — the one common case that deserves a human touch.
Frequently asked questions
- Which crates are required?
- serde with the derive feature and serde_json: serde = { version = "1", features = ["derive"] } in Cargo.toml compiles the generated code.
- Are nested arrays (Vec<Vec<T>>) supported?
- Yes — array types infer recursively: arrays of arrays become Vec<Vec<T>>, and nested objects still become their own structs.
- Do big integers overflow?
- Integers map to i64 (±2^63); integers beyond that range are rare in real APIs — switch those fields to i128 or serde_json::Number by hand.