JSON to Rust Struct Generator
Generate Rust struct definitions with serde Serialize and Deserialize derives from JSON. Infers Rust types from JSON values and produces idiomatic structs ready for use with serde_json.
Generates Rust structs with #[derive(Serialize, Deserialize)] attributes.
Maps JSON types to Rust primitives: String, i64, f64, bool, Vec<T>, Option<T>.
Uses serde rename attributes to map JSON keys to idiomatic Rust field names.
Creates nested struct definitions for nested JSON objects.
Output is ready to use with serde_json::from_str and serde_json::to_string.
How to Use
Paste a JSON object into the input field
The generator produces Rust struct definitions with serde attributes
Add serde and serde_json to your Cargo.toml
Paste the structs into your Rust source file and use with serde_json
Rust API Development
- Deserialize REST API responses with reqwest
- Define Axum or Actix-Web handler types
- Parse JSON config files in Rust services
Systems Programming
- Type JSON telemetry events in Rust workers
- Deserialize webhook payloads
- Parse structured log entries
Rust Ecosystem
- serde
- serde_json
- reqwest
- axum
- actix-web
- tokio
Use Option<T> for JSON fields that can be null or absent — it maps to JSON null on serialize and handles missing keys on deserialize.
Add #[serde(rename_all = "camelCase")] to the struct instead of per-field renames if all JSON keys follow camelCase.
For unknown or dynamic JSON fields, add a catch-all field: #[serde(flatten)] extra: HashMap<String, serde_json::Value>
Add #[serde(deny_unknown_fields)] to structs during development to catch unexpected JSON fields from API responses.
Use #[serde(default)] for optional fields to handle cases where the key may be absent entirely from the JSON object.
Add Clone and Debug derives alongside Serialize and Deserialize for structs that will be logged or cloned in application code.
Frequently Asked Questions
Find answers to common questions about our tools and services.
Understanding JSON to Rust Struct
Rust's ownership model and strict type system make it one of the safest languages for handling untrusted external data like JSON API responses and webhook payloads. The serde framework — short for serialization and deserialization — is the de facto standard for JSON handling in the Rust ecosystem, providing highly optimized, compile-time-verified serialization through procedural macro derives. serde_json, the JSON backend for serde, is one of the most downloaded crates on crates.io. For Rust developers building API clients, web services, or data processing pipelines, defining accurate serde-annotated struct types from JSON examples is a foundational task, and generating them automatically from real data samples dramatically accelerates development.
The serde derive macros (#[derive(Serialize, Deserialize)]) generate all serialization and deserialization code at compile time based on the struct definition and its field attributes. The serde(rename) attribute maps Rust's idiomatic snake_case field names to the camelCase or other casing conventions used by JSON APIs. The serde(rename_all) struct attribute applies a naming convention transformation to all fields at once (e.g., rename_all = "camelCase" converts all snake_case fields to camelCase JSON keys automatically). The optional feature skips fields that are not present in the JSON being deserialized, and the default attribute provides default values for absent fields. These attributes give developers fine-grained control over the serialization behavior without writing any manual parsing code.
Rust's type system expresses JSON's optional and nullable semantics through the Option<T> enum type. A JSON field that can be null maps to Option<T> where None represents null and Some(value) represents any non-null value. serde handles this mapping automatically: when serializing, None becomes null; when deserializing, null becomes None. Fields that may be entirely absent from the JSON object (rather than explicitly null) can also use Option<T> combined with the serde(skip_serializing_if = "Option::is_none") attribute for clean round-trip behavior. Distinguishing between null (explicit absence of value) and missing (key not present) requires more nuanced treatment with serde(default) combined with Option.
This JSON to Rust Struct generator produces struct definitions with #[derive(Serialize, Deserialize)] macros, PascalCase struct names, snake_case field names with serde rename attributes for the original JSON key names, and appropriate Rust types for each JSON value type. String values become String, integer values become i64, floating-point values become f64, booleans become bool, arrays become Vec<ElementType>, nested objects become nested struct types, and null values become Option<T>. The output is ready to compile once serde and serde_json dependencies are added to Cargo.toml. For Rust developers integrating with external APIs, building microservices, or processing JSON event streams, this generator eliminates the most tedious part of initial type definition work.