01What is JSON to Rust Serde?
Rust won't let you be vague about data shapes, so parsing JSON with serde means writing structs first. This generator reads a sample and writes those structs for you, each with #[derive(Debug, Serialize, Deserialize)]. Type inference is simple and predictable. Strings become String, whole numbers i64, decimals f64 and booleans bool. A null becomes Option<String>, since one null can't reveal the real type. Arrays become Vec<T> when every item has the same type. Ints mixed with floats widen to Vec<f64>, and nulls among the items give Vec<Option<T>>. Empty arrays and genuinely mixed arrays fall back to Vec<serde_json::Value>. Each nested object becomes its own pub struct, named in PascalCase from its key. Structs with identical fields are shared, so an array of orders yields a single OrdersItem. When the same key shows up at different levels with different fields, the names get a number suffix to stay unique. Field names follow the Rust naming conventions. Keys are converted to snake_case. Rust keywords like type and self get a trailing underscore, and names starting with a digit get a field_ prefix. Whenever the Rust name differs from the JSON key, the tool adds #[serde(rename = "key")], which the serde docs describe as the way to map a field to a different serialized name. That keeps serialization and deserialization symmetric. Here's a quick run. The input {"2fa":true,"self":1,"vals":[1,null,2.5]} produces field_2fa: bool and self_: i64, each with a rename attribute, plus vals: Vec<Option<f64>>. A top-level array becomes a type alias such as pub type Root = Vec<RootItem>. The Struct name box sets the root name. The output starts with use serde::{Serialize, Deserialize}. You'll need serde with the derive feature, plus serde_json whenever a Value type appears. There are real limits. If the items in an array have different keys, the tool doesn't merge them with Option fields. It gives up and uses Vec<serde_json::Value> for the whole array. A field missing from some payloads still looks required. Integers are always i64, even for IDs that would fit a u32 or exceed i64. Nested struct names lowercase everything after the first letter of each word, so userProfile becomes Userprofile. The conversion runs as JavaScript in your browser, so the sample you paste never leaves the page.
Rust developers writing API clients, web handlers or CLI tools use it to turn a captured JSON payload into serde-ready structs without writing every field by hand. The tool runs entirely in your browser, so your data never leaves your device.
02How to use JSON to Rust Serde
- Paste a JSON sample into JSON Input, or load one of the examples
- Type the root type name into Struct name (it defaults to Root)
- Read the generated structs and attributes in Rust Output
- Click Copy and paste the code into your crate
- Add serde with the derive feature, and serde_json if a Value type appears, to Cargo.toml
03Worked examples
- Input:
{"user_id":1,"userName":"ann","active":true} - Result:
use serde::{Serialize, Deserialize}; #[derive(Debug, Serialize, Deserialize)] pub struct Root { pub user_id: i64, #[serde(rename = "userName")] pub user_name: String, pub active: bool, } - Keys already in snake_case pass through. userName becomes user_name with a rename so the JSON still round-trips.
- Input:
{"type":"a","price":9.5,"tags":[],"meta":null} - Result:
pub struct Root { #[serde(rename = "type")] pub type_: String, pub price: f64, pub tags: Vec<serde_json::Value>, pub meta: Option<String>, } - type is a keyword, so the field is type_. The empty array and the null are placeholders until you know the real types. Derive line and use line trimmed.
- Input:
{"2fa":true,"self":1,"vals":[1,null,2.5],"user_profile":{"x":1}} - Result:
pub struct UserProfile { pub x: i64, } pub struct Root { #[serde(rename = "2fa")] pub field_2fa: bool, #[serde(rename = "self")] pub self_: i64, pub vals: Vec<Option<f64>>, pub user_profile: UserProfile, } - Digit-first and keyword keys get safe names plus renames. The null and the float in vals give Vec<Option<f64>>. Derive lines trimmed.
- Input:
{"a":{"b":{"c":1}},"x":[{"y":1.0}]} - Result:
pub struct B { pub c: i64, } pub struct A { pub b: B, } pub struct XItem { pub y: i64, } pub struct Root { pub a: A, pub x: Vec<XItem>, } - Shown condensed. Inner structs come first. y is i64 because 1.0 parses as the integer 1.
- Input:
[{"id":1,"n":"x"},{"id":2,"n":"y"}] - Result:
pub struct RootItem { pub id: i64, pub n: String, } pub type Root = Vec<RootItem>; - A top-level array becomes a type alias for a Vec of one shared item struct. Derive line trimmed.
- Input:
[{"id":1,"n":"x"},{"id":2}] - Result:
use serde::{Serialize, Deserialize}; pub type Root = Vec<serde_json::Value>; - The items have different keys, so no struct is generated and the whole array falls back to serde_json::Value.
04Common use cases
- Modeling a REST API response for a reqwest client in a command-line tool
- Defining JSON request bodies for an Axum or Actix Web handler that uses the Json extractor
- Deserializing GitHub or Stripe webhook payloads into typed structs inside a Rust service
- Loading a JSON config file into structs at startup so a bad value fails early with a serde error
- Replacing a chain of serde_json::Value lookups in older code with real structs
- Learning how serde rename attributes bridge camelCase JSON and snake_case Rust fields
05Tips
- When every field is camelCase, you can put #[serde(rename_all = "camelCase")] on the struct and delete the per-field rename lines.
- If an array comes out as Vec<serde_json::Value>, check whether its items just differ by a missing key. Edit the sample so each item has every key, then wrap the rare field in Option.
- JSON.parse reads 1.0 as the integer 1, so a sample value of 1.0 becomes i64. Use 1.5 in the sample if the field is really a float.
- Add #[serde(default)] to an Option field if the key can be absent, not just null. Without it, serde rejects the missing key.
- Check the i64 choice for IDs. Large unsigned IDs may need u64, and some APIs send big IDs as strings.
06Related concepts
serde, serde_json, derive macros, Option<T>, snake_case.
Frequently Asked Questions
What is JSON to Rust Serde?
JSON to Rust Serde is a free, browser-based developer tool on AIDToolStack. Convert JSON to Rust structs with Serde It requires no installation or sign-up and works entirely in your browser.
Is JSON to Rust Serde free to use?
Yes. No sign-up, no fees and no usage cap.
Does JSON to Rust Serde store my data?
No. It runs in your browser; nothing is uploaded.