Developer

JSON to Rust Struct

Generate Rust structs with serde derives from a JSON sample. Handles nested types, Option for nullable fields, Vec element types and serde rename attributes for non snake_case keys.

Last reviewed by the Radiatus Cloud team

Generated Rust appears here.

Need this built for your product?

We design, build & host secure software & APIs.

Talk to an engineer

Serde does the work once the types exist

Deserialising JSON in Rust is close to free once the struct is written: derive Deserialize, call serde_json::from_str, and the compiler guarantees every field access. Writing the struct is the slow part, particularly for a deeply nested payload where each level needs its own type and each optional field needs wrapping in Option. That transcription is mechanical, and mechanical work is what a generator is for.

Type mapping choices

Whole numbers map to i64 rather than i32, because a JSON number carrying a database identifier will eventually exceed 32 bits and serde will fail at runtime rather than at compile time. Fractional numbers map to f64. Strings map to String rather than a borrowed str, since a deserialised value must own its data unless you commit to lifetimes. Arrays become Vec of the unified element type, and objects become their own named struct. Any field that was missing or null anywhere in the sample becomes Option and receives a default attribute so a partial payload still deserialises.

Rename attributes and idiomatic names

Rust field names are snake_case and JSON keys frequently are not. Rather than leaving non idiomatic names in your code, the generator converts each field and attaches a serde rename attribute carrying the original key. When every key in a struct follows the same convention, it emits a single rename_all attribute on the struct instead, which is the form most Rust codebases use.

Related tools

  • JSON Formatter — Format and beautify JSON in your browser. Pinpoints syntax errors by line and column, flags unsafe integers, and never uploads your data to a server.
  • JSON Validator — Validate JSON syntax with precise line and column errors, and check documents against a JSON Schema. Runs locally in your browser, nothing uploaded.
  • Regex Tester — Test regular expressions against sample text with live match highlighting, capture groups and flag control. Runs entirely in your browser.
  • HTML Minifier — Minify HTML by removing comments and redundant whitespace, without breaking inline elements or pre blocks. Runs entirely in your browser.

Frequently Asked Questions

Why i64 rather than i32 or u32?

Because a JSON number has no declared width and identifiers routinely pass two billion. i64 deserialises every integer a typical API returns. Narrow it deliberately where you know the range.

What does the default attribute do on Option fields?

serde(default) makes a missing key deserialise to None rather than erroring. Without it, Option only tolerates an explicit null, not an absent key, which is a common source of confusing runtime failures.

Can I get borrowed types instead of String?

Not from this generator. Borrowed types require the struct to carry a lifetime and the input buffer to outlive it, which is a design decision rather than something inferable from a sample.

How are nested arrays of objects handled?

The element type is unified across every element in the array and emitted as its own struct, so you get Vec<Order> rather than Vec<serde_json::Value>. Fields absent from some elements become Option.

What if a key is a Rust keyword?

It is prefixed with r# in raw identifier form and given an explicit rename, so the original JSON key is still what serde reads and writes.

Privacy & Security

Everything runs in your browser; nothing is uploaded.

Data: None
Client-side-Side
Active
v1.0

How to Use

Paste JSON, set a root struct name, and copy the generated Rust with serde derives.

Disclaimer: This tool is provided "as is" without warranty of any kind. Results are for educational and utility purposes.