JSON to Rust Struct
Generate Rust structs from JSON
About JSON to Rust Struct
JSON to Rust Struct generates structs for serde from a JSON sample, with quicktype-core running in your browser. Every struct derives Debug, Clone, Serialize, and Deserialize. Fields are snake_case, and #[serde(rename_all = "camelCase")] or #[serde(rename = "...")] keeps the JSON keys, so serde_json reads and writes them unchanged. Whole numbers are i64, decimals f64, arrays Vec, and a key missing from some elements is an Option with skip_serializing_if, so None is not written back. A top-level array also gets a type alias such as pub type Root = Vec<RootElement>.
Example
Only the first user has a bio, so bio becomes an Option.
JSON input
[{"userName": "ada", "followers": 120, "bio": "Hi"},
{"userName": "lin", "followers": 3}]Rust struct output
use serde::{Serialize, Deserialize};
pub type Root = Vec<RootElement>;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RootElement {
pub user_name: String,
pub followers: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub bio: Option<String>,
}How to convert JSON to Rust structs
- 1Paste a JSON sample, ideally an array of several records, into "JSON input".
- 2Click "Convert"; the structs appear in "Rust struct output".
- 3Add serde with the derive feature, and serde_json, to your Cargo.toml.
- 4Click "Copy" or "Download" ("model.rs") to put the code in your crate.
Formats and limits
- Input
- JSON text; arrays are merged element by element
- Output
- Rust structs with derives and serde attributes, saved as model.rs
- Types
- i64, f64, bool, String, Vec<T>, and Option<T>; an untagged enum when a value has two types; serde_json::Value when no type can be inferred (a key that is always null, an empty array)
- Dependencies
- serde with the derive feature; serde_json to parse, also named in the code for a key that is null in every sample
Where your JSON is processed
Quicktype is downloaded at the first conversion and turns your JSON into Rust inside the browser tab, without uploading the sample. When the page shows an error, the error text is reported to our error log, together with the tool name and your browser's name and version, after quoted fragments, long numbers, and addresses are removed.
Frequently asked questions
How are camelCase or hyphenated keys handled?
Fields are renamed to snake_case. When all keys of a struct follow camelCase, one #[serde(rename_all = "camelCase")] covers it; other keys get their own #[serde(rename = "...")]. serde_json therefore reads and writes the original keys.
Does a key missing from some items become an Option?
Yes, and so does a key that is null in some items. Each gets a skip_serializing_if attribute set to Option::is_none, so a field holding None is left out when you serialize, as it was absent in the original JSON.
Which integer type does it use?
i64 for whole numbers, and f64 for any number written with a decimal point or an exponent, including 10.0. Change i64 to u32 or another type by hand if you know the range of your values.
Which crates does the generated code need?
Two at most: serde with the derive feature, for the derives and rename attributes, and serde_json, which you need to parse JSON anyway and which the code names directly when a key is null in every sample.
Tips and tricks
- ✓Rename RootElement and Root after pasting; those names come from the tool, not from your JSON.