Output will appear here
Output will appear here
Paste JSON and this tool writes Rust structs that derive Serialize and Deserialize, ready for serde_json::from_str. Field names are converted to snake_case and keep the original key with #[serde(rename)]. Nulls become Option<T>, keys missing from some array elements get #[serde(default)], and a field whose type varies falls back to serde_json::Value.
By Ankit Aglawe · Updated · Runs entirely in your browser
crate is a Rust keyword, so the field becomes crate_ with #[serde(rename = "crate")]. The two versions are merged: rust_version is a string in one and null in the other, so it becomes Option<String>. The array element struct is named Version, singular.
{
"crate": {
"id": "serde",
"max_version": "1.0.210",
"downloads": 512340987,
"recent_downloads": 48211002,
"repository": "https://github.com/serde-rs/serde",
"updated_at": "2024-09-06T21:47:14.613Z"
},
"versions": [
{ "num": "1.0.210", "yanked": false, "rust_version": "1.31" },
{ "num": "1.0.209", "yanked": false, "rust_version": null }
]
}use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Crate {
pub id: String,
pub max_version: String,
pub downloads: i64,
pub recent_downloads: i64,
pub repository: String,
pub updated_at: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Version {
pub num: String,
pub yanked: bool,
pub rust_version: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Root {
#[serde(rename = "crate")]
pub crate_: Crate,
pub versions: Vec<Version>,
}Each camelCase key gets its own rename, so the struct reads like Rust and still matches the wire format. exitCode is missing from the first event and is a number in one and a string in another, so it becomes Option<serde_json::Value> with default. A top-level array also gets pub type RootArray = Vec<Root>;.
[
{ "eventType": "deploy.started", "service": "api", "durationMs": 0, "meta": { "commit": "a1b2c3d" } },
{ "eventType": "deploy.finished", "service": "api", "durationMs": 84213, "meta": { "commit": "a1b2c3d" }, "exitCode": 0 },
{ "eventType": "deploy.failed", "service": "worker", "durationMs": 1290, "meta": { "commit": "9f8e7d6" }, "exitCode": "OOMKilled" }
]use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Meta {
pub commit: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Root {
#[serde(rename = "eventType")]
pub event_type: String,
pub service: String,
#[serde(rename = "durationMs")]
pub duration_ms: i64,
pub meta: Meta,
#[serde(rename = "exitCode", default, skip_serializing_if = "Option::is_none")]
pub exit_code: Option<serde_json::Value>,
}
pub type RootArray = Vec<Root>;Every code generator on this site is run on the same awkward input: a 64-bit id, a float written as 1.0, a null, a date string, a reserved word as a key, an empty array, a nested array and an array of objects whose keys differ. This is the exact output for Rust, generated when the page was built.
{
"id": 9007199254740993,
"price": 19.99,
"ratio": 1.0,
"deleted_at": null,
"created_at": "2024-01-15T10:30:00Z",
"class": "premium",
"tags": [],
"matrix": [[1, 2], [3, 4.5]],
"items": [{ "sku": "A1", "qty": 1 }, { "sku": "B2", "qty": 2, "note": "gift" }]
}use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Item {
pub sku: String,
pub qty: i64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Root {
pub id: i64,
pub price: f64,
pub ratio: i64,
pub deleted_at: serde_json::Value,
pub created_at: String,
pub class: String,
pub tags: Vec<serde_json::Value>,
pub matrix: Vec<Vec<f64>>,
pub items: Vec<Item>,
}A key present with value null becomes Option<T>. A key missing from some objects also becomes Option<T>, plus #[serde(default, skip_serializing_if = "Option::is_none")] so it deserializes when absent and stays absent when you serialize it back. In the edge-case output, note gets both attributes because one item lacks it.
If every sample is null there is nothing to infer a type from, so the field becomes serde_json::Value, which can hold null and anything else. deleted_at in the edge-case output shows this. Replace it with Option<String> (or whatever the real type is) once you know it.
Whole numbers become i64 and anything with a fraction becomes f64. serde_json parses integers exactly, so a 64-bit id like 9007199254740993 fits in i64 with no precision loss. But the browser parses 1.0 as 1, so ratio comes out i64, and serde_json will then refuse a real 1.5 (it does not coerce floats into integers). If a field can hold fractions, change it to f64.
Reserved words get a trailing underscore and a rename: class stays class because it is not a Rust keyword, while type, crate or self become type_, crate_ and self_. The r# raw identifier form is avoided because it is illegal for crate, self and super. Keys that start with a digit get a field_ prefix.
Timestamps stay String. The output has no dependency beyond serde and serde_json. If you use chrono with its serde feature, you can change created_at to DateTime<Utc> by hand and serde will parse the RFC 3339 string for you.
[[1, 2], [3, 4.5]] becomes Vec<Vec<f64>>: every inner element is merged, so one float widens the whole matrix. An empty array has no element type, so tags: [] becomes Vec<serde_json::Value>.
serde ignores JSON keys that have no matching field unless you add #[serde(deny_unknown_fields)]. So the structs keep working when the API adds a field later.
| JSON value | Rust type | Notes |
|---|---|---|
| string | String | Dates stay String too |
| integer | i64 | Exact up to 2^63, no 2^53 limit in serde_json |
| number with a fraction | f64 | Mixed int and float arrays widen to f64 |
| true / false | bool | |
| null alongside a value | Option<T> | |
| only ever null | serde_json::Value | |
| key missing in some objects | Option<T> + #[serde(default)] | Also skipped on serialize when None |
| object | pub struct Name | Named after its key, PascalCase |
| array | Vec<T> | All elements merged |
| mixed types or empty array | serde_json::Value |
Paste a JSON object or array into the input panel. The converter supports nested structures, mixed-type arrays, and optional fields with null values.
The output shows Rust structs annotated with #[derive(Serialize, Deserialize, Debug)] from the serde crate. Fields use idiomatic snake_case names with #[serde(rename = "...")] where the JSON key differs.
Copy the output into your src/ directory. Add serde, serde_json, and serde_derive to your Cargo.toml dependencies, then call serde_json::from_str to parse JSON into your new structs.
Paste the JSON into the left pane and copy the structs from the right. Add serde = { version = "1", features = ["derive"] } and serde_json = "1" to Cargo.toml, then call serde_json::from_str::<Root>(&text).
Yes. The structs derive Deserialize, so they work with serde_json::from_str, reqwest's .json::<Root>(), and axum or actix-web Json<Root> extractors.
Rust warns on non-snake_case field names. Each field that had to change gets #[serde(rename = "originalKey")], so the struct follows Rust style and still reads and writes the exact JSON keys.
Nullable and missing keys both become Option<T>. Keys missing from some array elements also get #[serde(default, skip_serializing_if = "Option::is_none")], so serializing the struct does not write null for a key that was never there.
Rust has no anonymous union type. When the samples show more than one type for a field, or only nulls, or an empty array, the honest type is serde_json::Value. Write an enum with #[serde(untagged)] if you want something stricter.
The converter always uses i64 for whole numbers. If an id can exceed 9,223,372,036,854,775,807, change it to u64. Negative numbers never fit u64, so i64 is the safe default.
Need several formats at once? Convert All shows every output from one paste. Or browse all JSON converters.