About the JSON to Rust
Generate Rust structs that work with serde and serde_json. Fields are snake_case with rename attributes where needed, Rust keywords are avoided, and nullable or optional values become Option<T>.
Key features
- Structs with #[derive(Serialize, Deserialize)] for serde
- snake_case fields with #[serde(rename = "…")] where needed
- Option<T> for nullable and optional values
- Rust keywords avoided (type → type_)
- serde_json::Value for values of unknown type
- Optional PartialEq derive
Rust structs for serde
Rust's serde is the standard way to work with JSON, but writing structs and rename attributes for every field gets repetitive. Paste JSON and get serde-ready structs with idiomatic snake_case fields.
Example
{ "userId": 7, "type": "admin", "nickname": null, "tags": ["a"] }
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Root {
#[serde(rename = "userId")]
pub user_id: i64,
#[serde(rename = "type")]
pub type_: String,
pub nickname: serde_json::Value,
pub tags: Vec<String>,
}
let root: Root = serde_json::from_str(&json)?;
Type mapping
| JSON | Rust |
|---|---|
"text" | String |
42 | i64 |
3.14 | f64 |
true | bool |
[...] | Vec<T> |
{...} | struct |
| null alongside values / missing | Option<T> |
| unknown | serde_json::Value |
Tips
- Borrowed strings: for zero-copy parsing, change
Stringto&'a strand add lifetimes. - Unknown keys: add
#[serde(deny_unknown_fields)]for strict parsing. - Enums: replace string fields with
#[derive(Deserialize)] enumtypes for fixed sets of values.
Related tools
Generating for Go instead? Try JSON to Go. Need a C++ header? See JSON to C++.
How to use the JSON to Rust
- Paste your data into the input box, upload a file, or click Sample to try it out.
- Adjust the options above the editor if you need to (indentation, delimiter, and so on).
- The Rust appears instantly in the output panel. Any errors are shown with their line and column.
- Click Copy to copy the result, or Download to save it as a file.