JSON to Rust Struct
Paste JSON and generate clean, serde-ready Rust structs with nested types, Option for nullable fields, Vec for arrays, and rename handling.
Input JSON
Generated Rust
Your generated Rust structs will appear here.About JSON to Rust Struct
This free tool turns any JSON object or array into idiomatic Rust structs in one click. It inspects your JSON, infers the most specific type for every field, and generates strongly-typed structs you can paste straight into an Actix or Axum service, a CLI, or any Rust project that uses serde for JSON.
It picks the right primitive for each value — i32 or i64 depending on the number's size, f64 for decimals, bool, and String — and builds nested structs for inner objects and Vec<T> types for arrays. Fields that are null or only present in some array items become Option<T>. JSON keys are converted to snake_case field names following Rust conventions, and a #[serde(rename = "...")] attribute is added whenever the original key differs, so camelCase APIs still deserialize correctly.
Output is ready for serde: every struct gets a configurable #[derive(...)] with Serialize, Deserialize, Debug, and optionally Clone. You can map ISO-8601 strings to chrono date types, choose your integer width, and add skip_serializing_if for optional fields. Everything runs entirely in your browser, so your JSON never leaves your device.
How to Use JSON to Rust Struct
- Paste a JSON object or array into the input box, or click Load Sample to try it.
- Set the root struct name for the top-level type.
- Pick your integer width and a date type so ISO-8601 strings map to
Stringor achronotype. - Toggle the
derivemacros,pubvisibility, rename behaviour, andskip_serializing_ifto match your style. - Copy the generated Rust and paste it into your project (add
serdewith thederivefeature to yourCargo.toml).
serde_json::Value when a safe, more specific Rust type cannot be inferred.What it handles
- ✅ Nested structs
- ✅ Root arrays via
typealias - ✅
Option<T>for nullable fields - ✅
i32vsi64detection - ✅
serdederive & rename - ✅
snake_casefield names
FAQ
Does this tool send my JSON to a server?
No. All parsing and code generation happens in your browser, so your JSON stays on your device.
What do I need to compile the output?
Add serde = { version = "1", features = ["derive"] } to your Cargo.toml. If you use a chrono date type, also add chrono with its serde feature. serde_json::Value fallbacks require the serde_json crate.
How are JSON keys like createdAt handled?
They become snake_case fields (created_at), and a #[serde(rename = "createdAt")] attribute is added so deserialization still maps to the original key.
Does it support root-level JSON arrays?
Yes. A root array generates a type Root = Vec<Item>; alias plus a struct for the element shape.
When does a field become Option<T>?
When its value is null, or when it is missing from some objects in an array. You can also add skip_serializing_if = "Option::is_none" so empty options are omitted on serialize.
How does it choose between i32 and i64?
In Auto mode, whole numbers within the 32-bit range become i32 and larger values become i64. Decimals become f64. You can also force all integers to a single width.
What about Rust keywords as field names?
A JSON key like type would be an invalid Rust identifier, so it is emitted as type_ with a #[serde(rename = "type")] attribute to preserve the mapping.