About JSON to Rust Struct
JSON to Rust Struct produces a Rust struct skeleton from a sample JSON object: a derived Debug attribute, the struct declaration, and one field per key in the order they appeared. The field names and the shape of the payload are correct, which is the laborious part when a response has a dozen properties.
The type names, however, are placeholders rather than compilable Rust. Primitive values are labelled with generic lowercase names, nested objects are labelled as an object, and arrays are suffixed with square brackets. Rust expects String, bool, and either f64 or one of the sized integer types; a nested structure needs a named struct of its own or a generic JSON value type; and an array needs the Vec form. Generated as-is, the file will not compile, and that is a property of the tool rather than a mistake in your input.
So the sensible workflow is to take the skeleton for the field list and tags, then work through the types as a deliberate step. If you want a build that compiles immediately, a derive-macro crate that generates the struct from the JSON at compile time is a better fit for that job.
How to use JSON to Rust Struct
- 1 Paste a representative JSON object.
- 2 Take the generated field list and derive attribute as your starting point.
- 3 Replace the placeholder type names with real Rust types.
- 4 Define a named struct for each nested object, or use a generic JSON value type where the shape is not fixed.
Frequently asked questions
Why does the generated code not compile?
Because the type names are lowercase placeholders standing in for the value categories found in the sample — string, number, boolean and object. They carry the shape of the payload, not the Rust type system, so each one has to be mapped to a real type before the compiler will accept it.
What should nested objects become?
Either a named struct of their own, if the shape is stable, or a generic JSON value type if the payload varies. The generator cannot choose for you, because one sample does not reveal whether a nested object is consistent across responses.
How should I type numbers?
Choose deliberately rather than defaulting to a float. Use a sized integer type for identifiers and counts, and for monetary values avoid binary floating point altogether — store minor units as an integer, which is the standard remedy.
Does it handle arrays?
Arrays are marked with square brackets in the generated field, which is not Rust syntax. Rewrite them in the vector form and pick the element type from the contents of your sample.
Free, online, and private
Free to use
JSON to Rust Struct is completely free. There is no account to create, no login, and no hidden charge or usage limit.
Runs online in your browser
It is a free online tool that runs entirely in your browser. Nothing to download or install — open the page and start using it right away.
Your data stays private
All processing happens locally on your device. Your input is never uploaded, stored, or tracked, so it never leaves your browser.
How JSON to Rust Struct behaves in detail
These are the specifics of this implementation — the exact ranges, defaults and fixed settings you are working with.
| Parameter | Value |
|---|---|
| Output shape | #[derive(Debug)] followed by struct Root { ... }, one field per top-level key |
| Type names | the JSON type names are emitted literally: string, number, bool, null and object |
| Arrays | element type from the first element, written as string[], number[] or unknown[] when empty |
| Field naming | the raw JSON key unchanged — no snake_case conversion and no serde rename attribute |
| Empty input object | produces an empty result rather than an empty struct, because the code guards on a non-empty join |
Compiled from the implementation of this tool and checked against it by itoolnet.
What JSON to Rust Struct does not do
Knowing the edges of a tool saves more time than another list of its features. These are the cases where it will not do what you might expect.
- The types are the names of the JSON types rather than Rust types. A string becomes string, a number becomes number and an array becomes string[], none of which compile — bool is the only real Rust type produced, so treat the output as a sketch.
- Keys are copied verbatim, so a camelCase key yields a non-snake_case field and a key with a hyphen or a space yields an illegal identifier, while no #[serde(rename = "...")] is emitted to keep the original JSON key working.
- Nested objects collapse to object with their inner fields discarded and an empty array becomes unknown[], so a payload with any depth needs those placeholders replaced with real types before the struct describes the data.
Tested and documented by itoolnet, the team that builds and maintains this site.