Skip to content
shellhacks.dev
Tooling

JSON to Rust Struct Generator

Generate Rust structs with serde Serialize and Deserialize derives from a JSON sample, with snake_case fields, rename attributes and Option for nullable keys.

Free · No sign-up · Your inputs stay in your browser

Input

Generate Rust types from JSON

Output

Rust

704 characters

Derives serde's Serialize and Deserialize. Keys are converted to snake_case with #[serde(rename)] where the JSON name differs, nullable or missing keys become Option<T>, and mixed values fall back to serde_json::Value. Types come from this one sample: give fields that are null here a real value to type them.

Runs in this browser. Nothing you paste is sent to a server.

The types chosen

Whole numbers become i64 and fractions f64; strings are owned String so the struct does not borrow from the input. Values whose type changes between items fall back to serde_json::Value. Narrow integers (u32, u8) by hand where the API documents a range, and add #[serde(rename_all = "camelCase")] if you prefer one attribute on the struct to one per field.

Missing versus null

With #[serde(default)] an absent key deserialises to None instead of failing, and skip_serializing_if = "Option::is_none" leaves it out again when writing. The serde attribute reference lists the rest, including deny_unknown_fields for strict parsing.

Share

Frequently asked questions

Why is a field written as r#type?
type is a reserved word in Rust, so it cannot be a field name as is. The r# prefix makes it a raw identifier, and the serde rename keeps the JSON key as type.
Which crates does the output need?
serde with the derive feature and serde_json: cargo add serde --features derive and cargo add serde_json.
Is what I paste sent anywhere?
No. The tool runs entirely in your browser with JavaScript. Nothing you type or paste is sent to our server or stored.

Go deeper

Suggested reading

Keep planning

Similar tools