JSON to Rust Struct Generator - Serde Converter
Typed serde structs, not serde_json::Value
You can read any JSON into serde_json::Value and index it with v["steps"][0]["name"],
but you give up the compiler checks. This tool writes #[derive(Serialize, Deserialize)] structs
for a sample payload. Fields are snake_case, and #[serde(rename)] is only added where the Rust name
and the JSON key differ.
Example: a CI deployment event
Input, with the root type name set to Deployment:
{
"type": "deployment",
"repoName": "devutil",
"durationMs": 48211,
"commit": { "sha": "9f1c2ab", "message": "bump deps" },
"steps": [
{ "name": "build", "ok": true },
{ "name": "test", "ok": false, "exitCode": 1 }
],
"meta": {}
} Output:
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Deployment {
pub r#type: String,
#[serde(rename = "repoName")]
pub repo_name: String,
#[serde(rename = "durationMs")]
pub duration_ms: i64,
pub commit: Commit,
pub steps: Vec<Step>,
pub meta: HashMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Step {
pub name: String,
pub ok: bool,
#[serde(rename = "exitCode")]
pub exit_code: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Commit {
pub sha: String,
pub message: String,
} type is a keyword, so the field is the raw identifier r#type. Serde strips the
r# when it matches keys, so no rename is needed. self, super and
crate can't be raw identifiers and get a trailing underscore plus a rename instead.
exitCode was only on the failed step, so it is Option<i64>. meta
was an empty object, so there is nothing to build a struct from and it becomes a
HashMap<String, serde_json::Value>.
Edits worth making
type with a fixed set of values is a good fit for an enum. If the payload shape depends on it,
#[serde(tag = "type")] on an enum of variants handles that, but it needs samples of each variant
and you'll have to write it by hand. Timestamps arrive as String; with the chrono crate's
serde feature you can change them to DateTime<Utc>. Add
#[serde(deny_unknown_fields)] to a struct if an unexpected key should be an error instead of being
ignored.
Values that change type between records, and fields that were only ever null, become
serde_json::Value. That compiles and parses anything, so look for it in the output and replace it
where you know the real type. An untagged enum (#[serde(untagged)]) is the usual replacement for a
field that is sometimes a number and sometimes a string.
For a schema that other services can validate against, see JSON to JSON Schema.
Same JSON, other languages
Every generator here reads the sample the same way, so a key that is optional or nullable in one language is optional or nullable in the others. Paste the same input into: TypeScript, Zod, Go, Python, Java, C#, JSON Schema.
Related Tools
JSON to YAML Converter
Convert JSON data to clean, readable YAML format instantly.
JSON to XML Converter
Transform JSON objects and arrays into well-formed XML documents.
JSON to CSV Converter
Flatten JSON arrays into downloadable CSV spreadsheets in one click.
YAML to JSON Converter
Convert YAML configuration files to JSON format instantly.