Skip to main content

JSON to Rust Struct

Convert JSON to Rust structs with serde Serialize/Deserialize, rename_all, Option for optional fields and raw identifiers for keywords. rustfmt-ready.

100% client-side · no data leaves your browser

skip_serializing_if on optional fields

Rust — 3 types
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Root {
    pub id: i64,
    pub username: String,
    pub email: String,
    pub is_active: bool,
    pub rating: f64,
    pub created_at: String,
    pub avatar_url: serde_json::Value,
    pub tags: Vec<String>,
    pub address: Address,
    pub orders: Vec<Order>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Address {
    pub street: String,
    pub city: String,
    pub zip_code: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Order {
    pub order_id: String,
    pub total: f64,
    pub items: i64,
    pub shipped: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coupon: Option<String>,
}

Arrays of objects are merged: a key missing from some items becomes optional, null makes a field nullable, and identical shapes share one type. Paste several examples in an array for the most accurate types.

About this tool

Paste JSON and get Rust structs that deserialize it with serde. Fields use snake_case, and the tool picks the #[serde(rename_all)] rule that fits your keys best (camelCase, PascalCase, kebab-case…) and adds #[serde(rename)] only where needed. Keys that are Rust keywords become raw identifiers such as r#type. Arrays of objects are merged, so missing keys become Option<T> (skipped when None), nulls are handled, integers and floats are told apart, nested objects get their own structs, and ID-keyed objects become HashMap. The output is formatted like rustfmt. Switch to Go, C#, Python or Kotlin from the same page.

How to convert JSON to Rust structs

Generate serde structs from a JSON sample.

  1. 1

    Paste JSON

    Paste a JSON object, or an array of examples.

  2. 2

    Choose options

    Set the root type name and whether fields are public.

  3. 3

    Copy the code

    Copy the structs into your crate; add serde (with the derive feature) and serde_json.