JSON to Rust Struct

Generate serde Serialize/Deserialize Rust structs from any JSON object.

What is Rust serde?

serde is Rust's de-facto serialization and deserialization framework. It is used by virtually every Rust project that works with JSON, TOML, YAML, MessagePack, or other data formats. The serde_json crate adds JSON support on top of serde.

To deserialize a JSON response into a Rust type, you derive Deserialize on your struct and call serde_json::from_str() or serde_json::from_value(). Writing these structs manually for complex API responses is time-consuming — this tool generates the complete struct hierarchy from any JSON with proper #[serde(rename)] attributes for keys that do not follow Rust snake_case conventions.

JSON to Rust Struct Example

Input JSON:

json
{
  "id": 1,
  "user_name": "alice",
  "is_active": true,
  "score": 9.5,
  "address": { "city": "Berlin" }
}

Generated Rust structs:

rust
use serde::{Deserialize, Serialize};

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

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Root {
    pub id: i64,
    pub user_name: String,
    pub is_active: bool,
    pub score: f64,
    pub address: Address,
}

Cargo.toml Setup

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

Parse JSON into your struct with serde_json::from_str::<Root>(&json_str)?.

Type Mapping

JSON TypeRust TypeNotes
stringStringOwned heap string
integeri64All whole numbers
floatf64Any decimal number
booleanbooltrue / false
nullOption<serde_json::Value>Unknown nullable type
arrayVec<T>T inferred from first element
objectStructNew named struct generated
key with non-snake_case#[serde(rename)]Automatic rename attribute

Frequently Asked Questions

Yes — add serde = { version = "1", features = ["derive"] } and serde_json = "1" to your [dependencies]. The tool generates the structs; Cargo handles the library.

Keys are converted to snake_case. If the snake_case form differs from the original key, a #[serde(rename = "original")] attribute is added automatically.

JSON null values map to Option<serde_json::Value>. If you know the underlying type, you can manually change it to Option<T> after generation.

Yes — the generated structs are framework-agnostic. They work with axum, actix-web, reqwest, and any other library that uses serde.

The type is inferred from the first element. For truly heterogeneous arrays you may need to change Vec<T> to Vec<serde_json::Value> manually.

Related Tools