JSON to Rust struct (serde)

It writes a struct per object, ready to read with serde_json::from_str without edits.

Loading the tool…

Processing and privacy: The result is calculated inside your browser and your data is not sent to any server.

How to use it

  1. Enter json input and root type name.
  2. The result appears straight away and updates as you edit.
  3. Copy the result, or copy a link that keeps the same values to share it.

How it is calculated

String, i64, f64, bool, Vec<T> and serde_json::Value, with a struct per object deriving Serialize and Deserialize. Fields are snake_case, #[serde(rename)] is added when a name differs from the key, and a field that can be null or missing becomes Option<T>.

Example

Inputs: Root type name = Root. Result: Generated code: use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Root { pub id: i64, pub name: String, pub email: String, pub is_active: bool, pub balance: f64, pub tags: Vec<String>, pub address: Address, pub orders: Vec<Order>, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Address { pub city: String, pub postal_code: String, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Order { pub order_id: String, pub total: f64, pub coupon: Option<String>, }; Types generated: 3; Fields: 13.

{
  "id": 1024,
  "name": "Sara Ahmed",
  "email": "sara@example.com",
  "is_active": true,
  "balance": 1520.75,
  "tags": ["vip", "riyadh"],
  "address": { "city": "Riyadh", "postal_code": "12211" },
  "orders": [
    { "order_id": "A-1", "total": 99.5, "coupon": "EID10" },
    { "order_id": "A-2", "total": 120, "coupon": null }
  ]
}
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Root {
    pub id: i64,
    pub name: String,
    pub email: String,
    pub is_active: bool,
    pub balance: f64,
    pub tags: Vec<String>,
    pub address: Address,
    pub orders: Vec<Order>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Address {
    pub city: String,
    pub postal_code: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Order {
    pub order_id: String,
    pub total: f64,
    pub coupon: Option<String>,
}

Limits and caveats

  • Needs serde with the derive feature and serde_json in Cargo.toml.
  • Every whole number becomes i64 and every decimal f64.

Questions about this tool

What happens when an Option field is missing?

serde reads it as None without an error, while a missing non-optional field gives a missing field error.

Why did type become type_?

type is a Rust keyword, so an underscore is added and #[serde(rename)] keeps the original key.

Is my JSON sent to a server?

No. The conversion runs in your browser and the JSON never leaves your device.