JSON to Rust Serde Struct Generator

Generate typed Rust structs with Serde serialization & deserialization attributes from JSON.

  • 100% free
  • Private in browser
  • No signup
  • No watermarks

Rust Structs (1)

use serde::{Deserialize, Serialize};

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProjectConfig {
    pub project_id: i64,
    pub name: String,
    pub active: bool,
    pub dependencies: Vec<String>,
}

About JSON to Rust Serde Struct Generator

The JSON to Rust Struct Converter generates Rust structs with Serde derives from a sample JSON payload. Paste a response, set the root struct name, and the tool writes pub snake_case fields with Serialize, Deserialize, Debug, Clone, Default, and PartialEq derived. Whole numbers become i64, decimals f64, arrays Vec<T>, and each nested object gets its own struct. Every struct also carries rename_all = "camelCase", so remove that line when your JSON keys are snake_case. Types come from one sample only.

How to use JSON to Rust Serde Struct Generator

  1. Enter a root struct name, such as ProjectConfig.
  2. Paste your sample JSON into the Input JSON box.
  3. Review the fields and serde attributes in the Rust Structs panel.
  4. Click Copy Rust Code and add serde with the derive feature to Cargo.toml.

Key features

  • pub structs deriving Default, Debug, Clone, PartialEq, Serialize, and Deserialize
  • snake_case field names, with #[serde(rename)] for camelCase keys
  • Maps whole numbers to i64, decimals to f64, and booleans to bool
  • Creates a separate struct for each nested object
  • Types arrays as Vec<T> based on their first element
  • Types null values as Option<serde_json::Value>

Frequently asked questions

How do I convert JSON to a Rust struct?
Paste your JSON into the Input JSON box, set a root struct name, and the structs are generated instantly. Click Copy Rust Code to copy them into your crate.
Which crates does the generated code need?
It needs serde with the derive feature, for example serde = { version = "1", features = ["derive"] }. Add serde_json as well to parse the JSON, and because null values and empty arrays are typed as serde_json::Value.
Why does deserializing fail on my snake_case keys?
Because every struct is generated with #[serde(rename_all = "camelCase")]. With that attribute Serde looks for projectId when the field is project_id. If your JSON keys are snake_case, delete the rename_all line and the fields will match.
Which Rust types are used for JSON numbers?
Whole numbers become i64 and numbers with a decimal part become f64. Narrow them by hand where you know the range, for example u32 for a count or u64 for an unsigned id.
How do I handle keys that are sometimes missing?
Wrap the field type in Option<T> yourself. The generator makes every field required except those that are null in the sample, so a missing key causes a "missing field" error until you change the type.

Rate this tool

Be the first to rate — helps others and improves this page.