JSON to Rust Struct Generator (serde)
Paste JSON and get Rust structs with serde derives, nested types, Vec<T>, Option<T> and rename attributes. Runs in your browser, so your data is never uploaded.
JSON Input
Rust
What Is a JSON to Rust Converter?
In Rust, JSON is almost always read with serde and serde_json: you define structs, add #[derive(Serialize, Deserialize)], and serde does the parsing. A JSON to Rust converter writes those structs from an example, with the right number types, Vec for arrays, Option for values that can be missing and #[serde(rename)] where a key is not a valid Rust name.
How to Convert JSON to Rust Structs
- Paste a sample of your JSON, or drop a .json file on the box.
- Choose a struct name, and public or private fields.
- Click Generate Rust, or press Ctrl + Enter.
- Copy the code, or download it as a .rs file.
Example
{
"name": "rg-lite",
"version": "0.4.1",
"type": "binary",
"downloads": 125034,
"stable": true,
"repository": {
"url": "https://github.com/example/rg-lite",
"stars": 842
},
"releases": [
{ "tag": "v0.4.1", "size_mb": 3.2 },
{ "tag": "v0.4.0", "size_mb": 3.1, "yanked": true }
]
}use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Package {
pub name: String,
pub version: String,
pub r#type: String,
pub downloads: i64,
pub stable: bool,
pub repository: Repository,
pub releases: Vec<Release>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Repository {
pub url: String,
pub stars: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Release {
pub tag: String,
pub size_mb: f64,
pub yanked: Option<bool>,
}type is a Rust keyword, so the field is the raw identifier r#type; serde drops the r# and still reads the JSON key "type". yanked is Option<bool> because only one release has it, and serde sets it to None when the key is missing. size_mb is already snake_case, so it needs no rename.
How the Types Are Chosen
| JSON value | Rust type |
|---|---|
| Text | String |
| Whole number | i64. Use u64 or a smaller type like u32 if you know the range. |
| Number with decimals | f64 |
| true / false | bool |
| Object | Its own struct, like Repository |
| Array | Vec<T>, like Vec<Release> |
| Missing in some items, or null | Option<T> |
| Only null, or mixed types | serde_json::Value, which can hold any JSON. Replace it with the real type if you know it. |
Keys in camelCase or with dashes, like userName, become snake_case fields with #[serde(rename = "userName")], so the code follows Rust naming rules and still matches the JSON.
Parse JSON in Rust
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"fn main() -> Result<(), Box<dyn std::error::Error>> {
let text = std::fs::read_to_string("package.json")?;
let package: Package = serde_json::from_str(&text)?;
println!("{} has {} releases", package.name, package.releases.len());
// And back to JSON:
let json = serde_json::to_string_pretty(&package)?;
println!("{json}");
Ok(())
}serde ignores JSON keys that your struct does not have, so new fields in an API response do not break your code. Add #[serde(deny_unknown_fields)] to a struct if you want strict checking instead. If the JSON is an array at the top level, parse it as a Vec of the item struct.
Tips
- Numbers that are sometimes decimals: if a field is
3in your sample but can be3.5, parsing it asi64fails. Change the type tof64. - Hide missing values when writing JSON: add
#[serde(skip_serializing_if = "Option::is_none")]above anOptionfield soNoneis left out instead of written asnull. - Read the error. serde_json errors give the line and column and say which type was expected, for example
invalid type: string, expected i64. - Check the JSON first. Fix broken JSON with JSON Repair or find the exact error in the JSON Validator.
Your JSON never leaves your browser, so it is safe to paste real data. Need another language? Try JSON to Go, JSON to Swift or JSON to TypeScript.
Frequently Asked Questions
How do I convert JSON to a Rust struct?
Paste a sample of your JSON into the converter and click Generate Rust. It writes structs with serde Serialize and Deserialize derives, Vec<T>, Option<T> and rename attributes, ready for serde_json::from_str.
Which crates do I need?
Add serde with the derive feature and serde_json to Cargo.toml: serde = { version = "1", features = ["derive"] } and serde_json = "1".
How are keys like type or userName handled?
Rust keywords become raw identifiers such as r#type, which serde still reads as "type". camelCase keys become snake_case fields with #[serde(rename)] so they match the JSON.
Is my JSON uploaded?
No. The Rust code is generated in your browser and your JSON never leaves your device.