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.

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.

Rust + Serde JSON Example

Serde is the de-facto standard for serialization in Rust. Combined with serde_json, it enables zero-overhead JSON parsing with full type safety at compile time.

JSON Input → Rust Struct

// JSON
{"user_id": 1, "name": "Alice", "is_active": true, "score": 98.5}

// Cargo.toml
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"

// Rust struct
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct User {
    user_id: u32,
    name: String,
    is_active: bool,
    score: f64,
}

fn main() {
    let json = r#"{"user_id":1,"name":"Alice","is_active":true,"score":98.5}"#;
    let user: User = serde_json::from_str(json).unwrap();
    println!("{:?}", user);
    
    // Serialize back
    let json_out = serde_json::to_string_pretty(&user).unwrap();
    println!("{}", json_out);
}

Serde Attributes Reference

AttributeEffect
#[serde(rename = "userId")]Rename field for JSON key
#[serde(skip_serializing_if = "Option::is_none")]Omit None fields from output
#[serde(default)]Use Default::default() if field missing
#[serde(flatten)]Inline nested struct fields
#[serde(deny_unknown_fields)]Error on unknown JSON keys
#[serde(tag = "type")]Tagged enum serialization

How to Convert JSON to Rust Structs

Converting JSON to Rust structs manually is tedious and error-prone for any real-world data shape. The process involves inspecting every field, choosing the correct Rust primitive type, wrapping optional fields in Option<T>, and adding the necessary Serde derive macros. This generator automates all of that.

Here is exactly how JSON maps to Rust struct types with Serde:

// JSON input
{
  "id": 42,
  "username": "alice_dev",
  "email": "alice@example.com",
  "score": 98.5,
  "is_active": true,
  "tags": ["rust", "backend"],
  "profile": {
    "bio": "Rustacean since 2019",
    "avatar_url": null
  }
}

// Generated Rust structs with Serde derive macros
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct Root {
    pub id: i64,
    pub username: String,
    pub email: String,
    pub score: f64,
    pub is_active: bool,
    pub tags: Vec<String>,
    pub profile: Profile,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Profile {
    pub bio: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub avatar_url: Option<String>,
}

Key type mapping rules applied during conversion:

Rust Serde: Serialize and Deserialize JSON

Serde is Rust's standard serialization framework. With the serde_json crate, you can serialize any Rust struct to a JSON string and deserialize any JSON string back into a typed struct — all with full compile-time type safety and zero runtime reflection overhead.

Cargo.toml Dependency

[package]
name = "my-app"
version = "0.1.0"
edition = "2021"

[dependencies]
# Core Serde framework with derive macro support
serde = { version = "1", features = ["derive"] }
# JSON format support for Serde
serde_json = "1"

Complete Working Example: Deserialize and Re-serialize JSON

use serde::{Deserialize, Serialize};

// Derive both Serialize and Deserialize to enable both directions
#[derive(Debug, Serialize, Deserialize)]
struct ApiUser {
    id: u64,
    #[serde(rename = "userName")]          // Map JSON "userName" → Rust field user_name
    user_name: String,
    email: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    phone: Option<String>,
    #[serde(default)]                       // Use false if field is absent in JSON
    email_verified: bool,
    roles: Vec<String>,
}

fn main() -> Result<(), serde_json::Error> {
    // --- Deserialize: JSON string → Rust struct ---
    let json_input = r#"{
        "id": 101,
        "userName": "alice_dev",
        "email": "alice@example.com",
        "roles": ["admin", "editor"]
    }"#;

    let user: ApiUser = serde_json::from_str(json_input)?;
    println!("Loaded user: {} ({})", user.user_name, user.email);
    println!("Phone: {:?}", user.phone);         // None — absent in JSON
    println!("Verified: {}", user.email_verified); // false — default

    // --- Serialize: Rust struct → JSON string ---
    let json_output = serde_json::to_string_pretty(&user)?;
    println!("Serialized:\n{}", json_output);

    // --- Serialize to serde_json::Value for dynamic access ---
    let value: serde_json::Value = serde_json::to_value(&user)?;
    println!("Username from Value: {}", value["userName"]);

    Ok(())
}

serde_json API Quick Reference

Function What it does Return type
serde_json::from_str(&s)Deserialize JSON string into typed structResult<T, Error>
serde_json::from_slice(&b)Deserialize from byte slice (&[u8])Result<T, Error>
serde_json::from_reader(r)Deserialize from any Read source (file, socket)Result<T, Error>
serde_json::to_string(&v)Serialize struct to compact JSON stringResult<String, Error>
serde_json::to_string_pretty(&v)Serialize struct to indented JSON stringResult<String, Error>
serde_json::to_value(&v)Convert struct to dynamic serde_json::ValueResult<Value, Error>
serde_json::json!({ })Create a serde_json::Value literal inlineserde_json::Value

Also useful: JWT Decoder | JSON Validator | JSON Formatter | JSON to Ruby | JSON to SQL