Paste any JSON and instantly generate Rust struct definitions with Serde Serialize and Deserialize derives. Stop writing boilerplate by hand. Free, no signup, runs entirely in your browser.
Open JSON to Rust Generator →Rust does not have a built-in JSON library, but the ecosystem provides Serde and serde_json — the most widely used serialization framework in the Rust ecosystem. Serde is a framework for serializing and deserializing Rust data structures efficiently and generically. Nearly every production Rust project that touches JSON depends on it.
To use Serde with JSON in your project, add the following to your Cargo.toml:
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
With these two crates in place, you can deserialize any JSON string directly into a typed Rust struct in a single function call. The generated code is fully type-safe and catches mismatches at compile time rather than at runtime.
The core of Serde's ergonomics is its derive macros. By annotating a struct with #[derive(Serialize, Deserialize)], the Rust compiler automatically generates all the serialization and deserialization logic at compile time. No reflection, no runtime overhead.
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
struct User {
id: u64,
name: String,
email: String,
active: bool,
}
fn main() {
let json = r#"{"id":1,"name":"Alice","email":"alice@example.com","active":true}"#;
// Deserialize JSON string into a Rust struct
let user: User = serde_json::from_str(json).unwrap();
println!("{} - {}", user.name, user.email);
// Serialize the struct back to a JSON string
let back_to_json = serde_json::to_string(&user).unwrap();
println!("{}", back_to_json);
}
The #[serde(...)] attribute provides fine-grained control over the mapping between Rust field names and JSON keys. Common attributes include:
#[serde(rename = "firstName")] - map a different JSON key to this field#[serde(rename_all = "camelCase")] - transform all field names automatically#[serde(skip_serializing_if = "Option::is_none")] - omit null fields from output#[serde(default)] - use Default::default() when a field is missing#[serde(flatten)] - inline nested struct fields into the parent JSON objectOur tool inspects your JSON structure and generates ready-to-use Rust struct definitions. Here is how to use it effectively:
#[derive(Debug, Serialize, Deserialize)]The generator infers Rust types from JSON values: strings become String, integers become i64, decimals become f64, booleans become bool, arrays become Vec<T>, and nested objects become new named structs.
Real-world JSON APIs often return fields that may be present in some responses and absent in others, or that can be null. In Rust, this is handled with the Option<T> type. Serde maps both missing fields and JSON null values to None.
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
struct ApiResponse {
id: u64,
name: String,
// This field may be absent or null in the JSON
#[serde(skip_serializing_if = "Option::is_none")]
avatar_url: Option,
// Use default if missing, skip if it is the default value
#[serde(default)]
verified: bool,
}
fn main() {
// Works even when avatar_url is missing from JSON
let json = r#"{"id":42,"name":"Bob"}"#;
let resp: ApiResponse = serde_json::from_str(json).unwrap();
println!("avatar: {:?}", resp.avatar_url); // None
println!("verified: {}", resp.verified); // false (default)
}
When serializing back to JSON, skip_serializing_if = "Option::is_none" ensures that fields with None values are completely omitted from the output rather than written as null, keeping the JSON clean and minimal.
Serde is Rust's standard serialization and deserialization framework. It uses compile-time derive macros to generate efficient, type-safe serialization code for your structs. The serde_json crate adds JSON format support on top of the core Serde traits.
Add serde and serde_json to your Cargo.toml, derive Deserialize on your struct, then call serde_json::from_str(&json). Serde maps JSON fields to struct fields by name automatically.
Wrap the field type in Option<T>. Absent or null JSON fields map to None. You can also use #[serde(default)] to apply the type's Default value when a field is missing, without requiring Option.
JSON strings map to String, integers to i64 or u64, decimals to f64, booleans to bool, null to Option<T>, arrays to Vec<T>, and objects to named structs or HashMap<String, V>.
Free, instant, 100% private. No account needed.
Open JSON to Rust Generator →Also useful: JWT Decoder | JSON Validator | JSON Formatter | JSON to Ruby | JSON to SQL