Developer / Types

JSON to Rust Struct Converter

Turn a JSON sample into Rust structs that derive serde's Serialize and Deserialize, with snake_case fields, renames, Option for missing or null keys, and Vec for arrays.

JSON to Rust Struct Converter: Each object becomes a struct with public fields. Keys become snake_case, with #[serde(rename = "…")] when the JSON spelling differs, and Rust keywords such as type become raw identifiers (r#type), which serde maps back automatically. Keys that are missing or null in some records become Option<T>, and values with no clear type become serde_json::Value. The output for several samples was compiled and deserialized its sample with serde 1. Runs 100% locally in your browser with zero server file uploads.

Runs
In your browser
Cost
Free · no sign-up
Availability
Ready to use
JSON to RustLocal processing

Runs entirely in your browser

Rust

use serde::{Deserialize, Serialize};

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

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    pub id: i64,
    pub user_name: String,
    pub avatar_url: String,
    pub score: f64,
    pub tags: Vec<String>,
    pub address: Address,
    pub manager_id: Option<i64>,
    #[serde(default)]
    pub nickname: Option<String>,
}

pub type UserList = Vec<User>;

Needs the serde (with the derive feature) and serde_json crates. Field names become snake_case with #[serde(rename)] where the key differs; keys missing or null in some objects become Option. Numbers are i64 unless a sample has a fraction.

Reading and writing the data

Deserialize with serde_json::from_str::<Root>(&text)? and serialize with serde_json::to_string(&value)?. Unknown keys in the input are ignored by default; add #[serde(deny_unknown_fields)] to a struct to reject them.

For the same sample in Go, see JSON to Go; to tidy the sample first, use the JSON formatter.

Option and missing keys

A missing key and a null value both deserialize to None for an Option field. If you need to tell them apart, use Option<Option<T>> with the serde_with crate, or keep serde_json::Value for that field.

How to use it

  1. Paste a JSON sample, ideally an array with several typical records.
  2. Name the top-level type, and choose whether None fields are left out when serializing.
  3. Copy the structs into your crate, which needs serde and serde_json.

Privacy & limitations

The JSON is converted in your browser and never uploaded.

Related tools

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.

Why i64 and f64?

They hold any JSON number safely. Narrow them to u32, u8, or f32 where you know the range.

Can I use #[serde(rename_all)] instead?

Yes: if every key uses the same convention, such as camelCase, one #[serde(rename_all = "camelCase")] on the struct replaces the individual renames.

Free tool · runs in your browser · no account required