How to convert JSON to Rust
- Paste a JSON object or array, for example a response from the API you are calling.
- Set the name of the root type.
- Copy the generated Rust code into your project.
What you get
- Structs deriving
SerializeandDeserializefrom serde. - snake_case fields with
#[serde(rename = "…")]where the JSON key differs. Option<T>for optional and nullable fields andserde_json::Valuefor mixed data.
Tips for accurate results
The generator only knows the sample you paste. Include arrays with several items and responses where optional fields are present, so it can tell which keys are always there, which can be null and which numbers have decimals.
Frequently asked questions
Is my JSON sent to a server?
No. The code is generated in your browser, so API responses and other private data never leave your device.
Which crates do I need?
Add serde with the derive feature and serde_json: cargo add serde --features derive and cargo add serde_json. Then parse with serde_json::from_str::<Root>(&text).
How are Rust keywords handled?
Keys like type or match become raw identifiers (r#type), which serde maps back to the original name. self, super and crate cannot be raw identifiers, so they get a trailing underscore and a rename.
Why i64 and f64?
Whole numbers become i64 and numbers with decimals f64, which cover any value JSON parsers usually produce. Narrow them (u32, i16…) if you know the range.