JSON to Rust Struct Generator

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 →

Serde: Rust's JSON Serialization Framework

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.

Derive Macros: Serialize and Deserialize

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:

How to Use the JSON to Rust Generator

Our tool inspects your JSON structure and generates ready-to-use Rust struct definitions. Here is how to use it effectively:

  1. Click "Open JSON to Rust Generator" above to open the tool
  2. Paste your JSON object or array into the input field
  3. The tool generates Rust structs with #[derive(Debug, Serialize, Deserialize)]
  4. Nested objects become separate named structs
  5. Copy the output and paste it into your Rust source file
  6. Add the Serde dependencies to your Cargo.toml and compile

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.

Handling Option Types for Nullable Fields

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.

Frequently Asked Questions

What is Serde in Rust?

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.

How do I deserialize JSON into a Rust struct?

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.

How do I handle optional fields in Rust JSON deserialization?

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.

What Rust types map to JSON types?

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>.

Generate Rust Structs from Your JSON

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