Skip to content
Devtoolry

JSON to Rust

Generate Rust structs from JSON online with serde Serialize and Deserialize derives, snake_case fields renamed to JSON keys and Option for nulls.

Something not working, or missing a feature? Send feedback

About the JSON to Rust

The JSON to Rust converter generates Rust structs that derive serde Serialize and Deserialize from a JSON sample. Fields use snake_case with #[serde(rename)] for the original keys, arrays become Vec<T>, null and missing values become Option<T>, and unknown values use serde_json::Value.

Conversion happens in your browser, so your files and records stay private.

How to use the JSON to Rust

  1. Paste a JSON sample and set the root struct name.
  2. Add serde (with the derive feature) and serde_json to Cargo.toml.
  3. Copy the structs and parse with serde_json::from_str::<User>(&text).

Example

Rust structs for serde

Input

{
  "id": 7,
  "fullName": "Grace Hopper",
  "email": null,
  "roles": ["admin"],
  "team": { "name": "Compilers", "size": 4 },
  "projects": [
    { "name": "COBOL", "year": 1959 },
    { "name": "A-0", "year": 1952, "archived": true }
  ]
}

Result

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    pub id: i64,
    #[serde(rename = "fullName")]
    pub full_name: String,
    pub email: serde_json::Value,
    pub roles: Vec<String>,
    pub team: Team,
    pub projects: Vec<Project>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Project {
    pub name: String,
    pub year: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub archived: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Team {
    pub name: String,
    pub size: i64,
}

camelCase keys become snake_case fields with #[serde(rename)]. serde treats a missing Option field as None, and skip_serializing_if keeps it out of the output again.

Common mistakes

  • Forgetting the derive feature. Without features = ["derive"] on serde, #[derive(Serialize, Deserialize)] does not compile. Add it in Cargo.toml.
  • Unexpected nulls. A field typed String fails to parse when the API sends null. Wrap fields that can be null in Option<T>.

Works well with the JSON to Rust

Frequently asked questions

Which crates do I need?

serde with the derive feature and serde_json: serde = { version = "1", features = ["derive"] } and serde_json = "1" in Cargo.toml.

What happens to Rust keywords like type?

A key that is a Rust keyword becomes a field with a trailing underscore (type_), and #[serde(rename = "type")] maps it back to the JSON key.

Why i64 and f64?

They hold any JSON number safely. Change them to smaller types such as u32 or f32 when you know the range; serde reports an error if a value does not fit.