JSON to Rust

Derive idiomatic Rust structs from any JSON object, with field names automatically converted to snake_case and #[serde(rename)] attributes added whenever the original key differs. Toggle serde::Serialize/Deserialize derives, Debug, and Option<T> wrapping for nullable fields to match your project's requirements. All code generation runs locally in your browser — no Rust toolchain or server needed.

Input

JSON

Struct Name

Code Style

Output

Rust Code

What is JSON to Rust Converter?

Rust's ownership model and strict type system make JSON handling more involved than in other languages — which is exactly why the serde ecosystem was built. But writing serde-compatible structs by hand from a JSON schema is still mechanical work. Paste your JSON here and get a set of pub struct definitions with #[derive(Debug, Serialize, Deserialize)] macros, correct Rust types (u64, f64, bool, String, Vec<T>, Option<T>), and serde rename attributes to handle camelCase or snake_case key differences. Null values generate Option<T> fields annotated with skip_serializing_if. The generated code compiles immediately with serde and serde_json in your Cargo.toml, and works with Actix-web web::Json<T> extractor and Axum axum::Json<T> without modification.

How to Use

  1. 1

    Paste Your JSON

    Paste a JSON object or array into the editor. Null values generate Option<T> types; arrays generate Vec<T>. Include a representative sample for accurate type inference.

  2. 2

    Configure Rust Struct Options

    Set the root struct name, choose derive macros (Debug, Serialize, Deserialize, Clone), and select the serde rename strategy (camelCase, snake_case, or field-level #[serde(rename)]).

  3. 3

    Generate Rust Structs

    Click "Generate Rust". The tool produces pub struct definitions with serde derive attributes, maps JSON types to Rust types (u64, f64, bool, String, Vec<T>, Option<T>), and handles nested objects as separate structs.

  4. 4

    Add to Your Rust Crate

    Copy the generated structs and paste them into your Rust source file. Add serde = { version = "1", features = ["derive"] } and serde_json = "1" to your Cargo.toml, then use serde_json::from_str::<Root>(&json_str) to deserialise.

Common Use Cases

Serde Deserialisation Structs

Generate Rust structs with serde::Deserialize derives from a JSON API response to enable type-safe JSON parsing with serde_json in Actix-web, Axum, or Rocket backend services.

CLI Tool Configuration

Convert JSON configuration schemas into Rust structs for CLI tools built with clap or config-rs, enabling strongly-typed configuration loading from JSON files at startup.

WebAssembly Data Types

Generate Rust structs for WASM modules that exchange JSON data with JavaScript. Use wasm-bindgen-compatible types derived from your JSON payload shape to handle serialisation at the boundary.

Embedded Systems Config

Define Rust structs from JSON configuration payloads for embedded systems using serde_json_core or serde-json-core for no_std environments, enabling type-safe config deserialization.

Conversion Examples

JSON Object → Rust Struct with Serde

A JSON object generates a Rust struct with serde Deserialize and Serialize derives.

Input JSON

{
  "id": 1,
  "username": "alice",
  "email": "alice@example.com",
  "is_active": true
}

Output CSV

use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
pub struct Root {
    pub id: u64,
    pub username: String,
    pub email: String,
    pub is_active: bool,
}

Nested JSON → Nested Rust Structs

Nested objects generate separate Rust structs with typed field references.

Input JSON

{
  "user": {"id": 1, "name": "Alice"},
  "items": [{"sku": "ABC", "qty": 2}]
}

Output CSV

#[derive(Debug, Deserialize, Serialize)]
pub struct User {
    pub id: u64,
    pub name: String,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Item {
    pub sku: String,
    pub qty: u64,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Root {
    pub user: User,
    pub items: Vec<Item>,
}

Frequently Asked Questions