Skip to main content

JSON to Rust Struct

Paste JSON and get Rust structs with serde derives. Option<T> for nullable fields and rename attributes. Private, runs in your browser.

Paste JSON on the left. Add a second sample to find optional fields.

JSON input

Stays in your browser

Rust code

The generated code appears here.

Shortcuts: Ctrl+Enter run · Ctrl+Shift+C copy result · / search (use ⌘ on Mac)

Next steps

Free Online JSON to Rust Struct Generator

Generate Rust structs that work with serde_json, with the derives and field names already in place. It gives developers a quick browser-based way to solve a focused json to rust struct task before moving the result into a project.

How to generate Rust code from JSON

Paste a JSON object or an array of objects into the input box and press Generate. Fields are snake_case with a #[serde(rename = "...")] where the JSON key differs, or use rename_all = "camelCase" for the whole struct. Nullable and optional fields become Option<T>.

One input, every language

Your JSON stays in the box when you switch the language tabs, and the code is generated again at once. Each language also has its own page, so you can bookmark the one you use.

How types are worked out

  • Whole numbers and numbers with decimals are told apart.
  • Add more samples with Add sample: a key missing from some samples becomes optional, and null makes a type nullable.
  • Arrays of objects are merged into one type, and nested objects become their own types named after the key.
  • Keys that are not valid names in the language (such as first-name or 2fa) are renamed and mapped back, never rejected.

Worked example

The sample JSON in the box above, generated with the default options:

{
  "id": 1,
  "name": "Ada",
  "email": null,
  "tags": ["x"],
  "address": { "city": "Pune", "pin": "411001" },
  "createdAt": "2026-01-15T10:30:00Z"
}
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Root {
    pub id: i64,
    pub name: String,
    pub email: Option<String>,
    pub tags: Vec<String>,
    pub address: Address,
    #[serde(rename = "createdAt")]
    pub created_at: String,
}

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

Common mistakes

  • A single sample cannot show which fields are optional. Paste two or three different responses for a realistic result.
  • A value that is always null gives no type information, so it is treated as a nullable string. Change it by hand if it is something else.
  • Numbers are typed from the samples: 1 and 1.0 look the same in JSON, so use a sample with decimals where you need a float.

Frequently asked questions

Which crates do I need?
serde with the derive feature, and serde_json to read the data. If you turn on Detect dates you also need chrono with its serde feature.
When is a field an Option?
When the value was null in a sample or the key was missing in some samples. Optional fields also get skip_serializing_if so they are left out when empty.
What are the numbers mapped to?
Whole numbers become i64 and numbers with decimals become f64. Change them by hand if you need smaller types.
Is my JSON uploaded?
No. The types are worked out in your browser. Nothing you paste is sent to a server.
How do I use this json to rust struct tool?
Use the interactive controls above; processing stays in your browser and the result is intended as a practical starting point for development work.

More Formatters & Parsers

View all Formatters & Parsers tools →