formatjson.pro
Validate, pretty-print or minify JSON in your browser — nothing is uploaded.

JSON to Rust Struct

Paste a sample JSON object below and generate Rust structs annotated with `#[derive(Serialize, Deserialize)]`. The tool infers a type for every field and creates a named struct for each nested object.

Runs entirely in your browser — your JSON is never uploaded to a server. No ads injected into the output, no sign-up. Emits serde-ready structs with rename attributes for non-identifier keys, so from_str works immediately.

How to generate a Rust struct from JSON

  1. Paste your JSON. Paste one representative JSON object into the box above.
  2. Generate the struct. Click Generate Rust struct. The tool infers a type for every field and emits a struct with serde derives for the root and each nested object.
  3. Parse with serde_json. Add serde and serde_json to Cargo.toml, copy the structs in, and call serde_json::from_str to deserialize.

How the type mapping works

Each JSON value maps to a Rust type: a whole number becomes i64, a decimal becomes f64, a string becomes String, true/false becomes bool, and null becomes Option<serde_json::Value>. Arrays become Vec<T>. Keys that are not valid Rust identifiers get a #[serde(rename)].

Arrays and nested objects

An array becomes a typed collection using the type of its first element, and every nested object gets its own named type so the result compiles as-is. Requires the serde and serde_json crates. Add optionality with Option for fields that may be missing.

Using the result

Parse with serde_json: let root: Root = serde_json::from_str(json)?;.

FAQ

Do I need extra crates?

Yes — add serde with the derive feature and serde_json to Cargo.toml. The generated structs use #[derive(Serialize, Deserialize)] from serde.

How do I handle optional fields?

A single sample cannot show which fields are missing in other responses. Wrap fields that may be absent in Option<T>; serde will deserialize a missing or null key to None.

Why i64 and f64?

JSON has one number type. The tool picks i64 for whole numbers and f64 for decimals. Narrow to u32, i32, or a fixed-precision type where the real range is known.