Skip to content
Mediawand

JSON to Rust Structs (serde)

Parse JSON with serde without writing every struct by hand. Paste an example and get structs for each nested object, ready for serde_json::from_str.

How to generate Rust structs from JSON

  1. 1Paste a JSON example, ideally with several array items.
  2. 2Name the root struct.
  3. 3Copy the code or download types.rs.

How the types are worked out

Every item of an array is merged into one type, so a key that's missing from some items becomes optional and a value that's sometimes null becomes nullable. Whole numbers and decimals are told apart, nested objects get their own named types (an “items” array gets an “Item” type), and objects with the same shape share one type.

Rust specifics

Fields use snake_case with #[serde(rename)] when the JSON key differs, keywords become raw identifiers like r#type, nullable and optional fields are Option<T>, whole numbers are i64, decimals f64, and mixed values are serde_json::Value.

Dependencies

Add serde with the derive feature, and serde_json, to Cargo.toml: serde = { version = "1", features = ["derive"] } and serde_json = "1".

Frequently asked questions

Is my data uploaded?
No. Everything runs in your browser, so the JSON never leaves your device.
What happens to missing keys?
Optional fields have #[serde(default)], so a missing key becomes None instead of an error, and None isn't written back out.
Can I use other number types?
Yes. Edit the generated code, for example u32 for counts or Decimal for money.

Related tools

Loading tools…