Format Code Now
/

JSON to Rust Struct Converter

FV-23

Paste a JSON sample and press Convert to get Rust structs that derive Serialize and Deserialize, with serde rename attributes and Option for values that can be absent. It runs in your browser.

Runs in your browser. Nothing is uploaded.

JSON

Rust

How to use it

  1. Paste a JSON sample into the input box, or open a .json file.
  2. Choose the root struct name and whether to derive Debug and Clone.
  3. Press Convert and copy the structs into your crate.
  4. Add serde with the derive feature and serde_json, then call serde_json::from_str::<Root>(&text).

What the structs look like

Each object becomes a struct with #[derive(Serialize, Deserialize)], and nested objects become their own structs named after the key. Fields are snake_case. When the field name differs from the JSON key, #[serde(rename = "...")] keeps the original key. A name that is a Rust keyword, such as type, gets a trailing underscore and a rename attribute. Arrays become Vec<T>, with the item type merged from every item.

Numbers, Option and Value

Whole numbers become i64 and anything with a decimal point or exponent becomes f64. A whole number beyond i64 becomes f64 too, which can round it, and a comment marks the line. A key that is null in some items becomes Option<T>. A key missing from some objects also becomes Option<T>, with skip_serializing_if so it is left out when you serialize again. Mixed types, keys that are always null and empty arrays become serde_json::Value.

Limits of a sample

The structs come from the sample alone. A key that never appears has no field, a field that is always a whole number in the sample is typed i64 even if the API can send decimals, and dates stay String. A top-level array gives you the item struct and a comment saying the document is a Vec of it. Check the types against the API documentation before you depend on them.

Questions people ask

How do I convert JSON to a Rust struct?

Paste the JSON and press Convert, then copy the structs into your crate. Add serde with the derive feature and serde_json to Cargo.toml, and call serde_json::from_str::<Root>(&text) to read the JSON into the root struct.

Why do some fields have a rename attribute?

Rust field names are snake_case, while JSON keys are often camelCase or contain characters Rust names cannot. The attribute #[serde(rename = "key")] tells serde which JSON key the field reads and writes.

When does a field become Option<T>?

A field becomes Option<T> when the key is null in some items of the sample or missing from some objects. Missing keys also get skip_serializing_if, so serializing does not write null for them.

Why is a mixed field serde_json::Value?

A field that holds different types in different items has no single Rust type, so serde_json::Value stands in for any JSON value. Replace it with an enum if you know the possible shapes.

Why is a large number f64?

A whole number beyond the i64 range does not fit the integer type the tool uses, so it becomes f64, which can round it. A comment marks the field. Use u64, u128 or a string by hand if you need every digit.

What happens to a field named type?

type is a Rust keyword, so the field is written as type_ with #[serde(rename = "type")]. Other keywords get the same trailing underscore, and the JSON key is unchanged on the wire.

About this tool

Infers structs from the sample you paste, so it only knows what that sample shows. Each object becomes a struct that derives Serialize and Deserialize, with fields in snake_case. #[serde(rename = "...")] keeps the original key wherever the field name differs, and a name that is a Rust keyword gets a trailing underscore. Whole numbers become i64 and everything with a decimal point or exponent becomes f64; a whole number beyond i64 also becomes f64, which can round it, and a comment marks the line. A key that is null in some items becomes Option<T>. A key missing from some objects also becomes Option<T>, with skip_serializing_if so the key is left out again when you serialize. Arrays become Vec<T>. Mixed types, null-only keys and empty arrays become serde_json::Value, which needs the serde_json crate. A top-level array gets the item struct and a comment.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.