Developer Tools

JSON to Rust Struct

Paste JSON and get Rust structs that serde can serialize and deserialize. Fields follow Rust’s snake_case convention with #[serde(rename = "…")] for the original keys, optional or null values become Option<T> with default and skip_serializing_if, arrays become Vec<T>, and values with mixed types become serde_json::Value. Choose the derives and field visibility.

  • Runs in your browser
  • No sign-up
  • Free to use

How to use JSON to Rust Struct

  1. Paste a JSON sample.
  2. Set the root struct name.
  3. Choose the derives and pub fields.
  4. Copy the code and add serde to Cargo.toml.

JSON to Rust Struct features

serde ready

Serialize and Deserialize derives with Debug and Clone.

Rust naming

snake_case fields with rename attributes for the JSON keys.

Option handling

Option<T> with default and skip_serializing_if for optional fields.

Types

i64, f64, bool, String, Vec<T> and serde_json::Value.

Keyword safe

Raw identifiers such as r#type for reserved words.

Local

Generated in your browser.

When to use JSON to Rust Struct

  • Deserializing an API response with reqwest.
  • Defining request bodies for an Axum or Actix-web handler.
  • Reading JSON configuration into typed structs.
  • Writing integration tests with typed fixtures.

JSON to Rust Struct FAQ

What do I add to Cargo.toml?

serde = { version = "1", features = ["derive"] } and serde_json = "1". Then call serde_json::from_str::<Root>(&text).

Why #[serde(rename = …)] on some fields?

Rust fields use snake_case. When the JSON key is camelCase or contains hyphens, rename keeps the original key for both directions.

What does skip_serializing_if do?

It leaves None values out when serializing, so optional fields that were absent stay absent. default lets deserialization accept a missing key.

Why i64 and f64?

They hold any integer and decimal JSON is likely to contain. Use u32, u64 or f32 if you know the range.

What is serde_json::Value?

A type that holds any JSON value. It is used where the sample had mixed or unknown types.

Is my JSON uploaded?

No. The structs are generated in your browser.

Rust structs for JSON with serde

In Rust, JSON is almost always handled with serde, a framework that derives serialization code from struct definitions at compile time. Once a struct is annotated with #[derive(Serialize, Deserialize)], serde_json can convert it to and from JSON with full type checking. The work lies in writing structs that exactly match the JSON, which is what this generator does.

Field names follow Rust’s snake_case convention, so createdAt becomes created_at. Where the name differs from the key, #[serde(rename = "createdAt")] keeps the mapping for both directions. Keys that clash with Rust keywords, such as type, become raw identifiers like r#type, which compile without renaming the field.

Rust has no null, so absence is modelled with Option<T>. When the sample shows a field missing or null, it becomes Option, with #[serde(default)] so a missing key deserializes to None, and skip_serializing_if so None is left out when writing JSON again. Required fields stay plain types and make deserialization fail when missing, which is usually what you want.

Numbers become i64 or f64, strings String, arrays Vec<T>, and nested objects named structs. Values whose type changes between samples become serde_json::Value, which can be inspected at run time. Narrow the types afterwards if you know them, for example u16 for ports or enums for string fields with fixed values.

The derive list is configurable. Debug and Clone are convenient in most code, PartialEq helps in tests, and Deserialize alone suffices for read-only data. The generated code only needs serde and serde_json as dependencies.

Other useful tools