Shard Tools

JSON to Rust Struct

Generate Rust structs from a JSON sample in your browser, deriving Serialize and Deserialize.

Maintained by Roshan.

Use JSON to Rust Struct

Converts json sample to rust struct in this tab, with nothing sent anywhere.

Runs entirely on your deviceThe conversion happens in this browser tab. Nothing you type is uploaded, logged or stored, which is worth knowing for anything you would not paste into a search box.

Explore generators

Find related browser-local tools for nearby tasks without starting another search.

Browse all generators

About JSON to Rust Struct

Rust's approach to JSON runs through serde, and that means declaring a struct that mirrors the data and derives Serialize and Deserialize. Writing those by hand for a real response is exacting work - every field typed, every name in snake_case, the derives remembered. Generating from a sample does it in one step: paste the JSON, get the structs with their serde derives already attached. This infers the shape recursively, gives nested objects their own structs, and uses sensible Rust types - String, f64, bool, Vec for arrays - so the result compiles against serde with minimal editing. As with the other generators it types what the sample proves and leaves genuine optionality, which in Rust means Option, for you to add where the data allows nulls.

Using JSON to Rust Struct

  1. The JSON is parsed and inferred recursively into a set of structs.
  2. Each struct derives Serialize and Deserialize, so it works with serde immediately.
  3. Fields are public and named in snake_case from the original keys.
  4. Nested objects become their own structs referenced by the parent, and arrays become Vec of the element type.
  5. JSON numbers map to f64, the safe default; narrow to an integer type where you know the data.

Where json to rust struct helps

Deserialising an API in Rust

Paste a sample response and get serde-ready structs to deserialize into with serde_json.

Modelling a data file

Generate the structs, then narrow numeric types and add Option where fields may be absent.

Before you use the result

Questions about JSON to Rust Struct

Are serde derives included?

Yes. Every struct derives Serialize and Deserialize, ready for serde_json.

Why f64 for numbers?

JSON has one number type, so f64 is the safe default. Narrow to an integer type where the data allows.

What about optional fields?

Option is not added automatically, since a sample cannot prove it. Wrap fields in Option where nulls occur.

Does it handle nested objects?

Yes. Each becomes its own struct referenced by the parent.

Related tools

JSON to Go Struct

Generate Go structs from a JSON sample in your browser, complete with json struct tags.

JSON to TypeScript

Generate TypeScript interfaces from a JSON sample in your browser. Nested objects become their own interfaces.

JSON to JSON Schema

Generate a JSON Schema from a sample document in your browser, inferring types and required fields.