jsontoolskit.org
JSON and JSON Schema utilities, in the browser
Say hi →

JSON to Rust

JSON sample → serde structs · updated 28 July 2026

Generate Rust structs from a JSON sample, ready for serde_json. Field names are converted to snake_case with a #[serde(rename)] attribute where the JSON key differs, and anything optional or nullable is wrapped in Option<T>.

Paste input to start.

How to generate Rust structs from JSON

  1. Paste a JSON sample — ideally an array of several records, so optional fields are detected.
  2. Set the Root type name. Nested objects are named after the key that holds them, singularised for arrays.
  3. Adjust the Derive list if you need PartialEq, Default, or fewer traits.
  4. Tick skip_serializing_if so None fields are omitted when serialising rather than written as null.
  5. Copy the code, or download it as a file.

Type mapping

Getting good types out of a sample

Type inference from a sample is only as good as the sample. Three habits make the output far more useful:

Privacy

Nothing is uploaded. Inference and code generation run in this tab. No server sees the payload you paste — which matters, because real API responses tend to contain real user data.

FAQ

Why is a field Option even though it is always present?

Because it held null in at least one record. In serde, null and "key absent" are different things: Option<T> covers both, so it is the safe choice.

Do I need serde_json in Cargo.toml?

You need serde with the derive feature always, and serde_json if the output mentions serde_json::Value or you are parsing JSON: serde = { version = "1", features = ["derive"] }.

Can it generate enums for variant shapes?

No. Tagged enums require knowing which field is the discriminator, which a sample does not reveal. Mixed shapes become serde_json::Value — replace that with a hand-written enum and #[serde(tag = "…")].