JSON to Rust
Generate Rust structs from JSON online with serde Serialize and Deserialize derives, snake_case fields renamed to JSON keys and Option for nulls.
Something not working, or missing a feature? Send feedback
About the JSON to Rust
The JSON to Rust converter generates Rust structs that derive serde Serialize and Deserialize from a JSON sample. Fields use snake_case with #[serde(rename)] for the original keys, arrays become Vec<T>, null and missing values become Option<T>, and unknown values use serde_json::Value.
Conversion happens in your browser, so your files and records stay private.
How to use the JSON to Rust
- Paste a JSON sample and set the root struct name.
- Add serde (with the derive feature) and serde_json to Cargo.toml.
- Copy the structs and parse with serde_json::from_str::<User>(&text).
Example
Input
{
"id": 7,
"fullName": "Grace Hopper",
"email": null,
"roles": ["admin"],
"team": { "name": "Compilers", "size": 4 },
"projects": [
{ "name": "COBOL", "year": 1959 },
{ "name": "A-0", "year": 1952, "archived": true }
]
}Result
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
pub id: i64,
#[serde(rename = "fullName")]
pub full_name: String,
pub email: serde_json::Value,
pub roles: Vec<String>,
pub team: Team,
pub projects: Vec<Project>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Project {
pub name: String,
pub year: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub archived: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Team {
pub name: String,
pub size: i64,
}camelCase keys become snake_case fields with #[serde(rename)]. serde treats a missing Option field as None, and skip_serializing_if keeps it out of the output again.
Common mistakes
- Forgetting the derive feature. Without features = ["derive"] on serde, #[derive(Serialize, Deserialize)] does not compile. Add it in Cargo.toml.
- Unexpected nulls. A field typed String fails to parse when the API sends null. Wrap fields that can be null in Option<T>.
Works well with the JSON to Rust
- JSON Formatter: tidy and check the JSON before generating types.
- JSON to TypeScript: generate matching TypeScript interfaces for the front end.
- JSON Validator: find syntax errors in the sample first.
Frequently asked questions
Which crates do I need?
serde with the derive feature and serde_json: serde = { version = "1", features = ["derive"] } and serde_json = "1" in Cargo.toml.
What happens to Rust keywords like type?
A key that is a Rust keyword becomes a field with a trailing underscore (type_), and #[serde(rename = "type")] maps it back to the JSON key.
Why i64 and f64?
They hold any JSON number safely. Change them to smaller types such as u32 or f32 when you know the range; serde reports an error if a value does not fit.
Related tools
JSON to CSV
Convert JSON arrays to CSV online with automatic column detection, nested object flattening and custom delimiters. Open the result in Excel or Sheets.
CSV to JSON
Convert CSV to a JSON array online with header detection, type inference and delimiter auto-detection. Turn spreadsheet exports into data for APIs and code.
JSON to XML
Convert JSON to well-formed XML online with a configurable root element and attribute support. Validate the input and download the XML file.
XML to JSON
Convert XML to JSON online, keeping attributes and turning repeated elements into arrays. Malformed XML is reported with its location.
JSON to YAML
Convert JSON to clean, human-readable YAML online. Turn API responses and package files into config for Kubernetes, Docker Compose or CI pipelines.
YAML to JSON
Convert YAML documents to formatted JSON online with syntax validation. Errors are reported by line, so broken indentation is quick to find and fix.