JSON to Rust Converter
Rust structs with serde derives, generated from a JSON sample
JSON Input
Rust Output
What does the JSON to Rust converter generate?
With serde, reading JSON into Rust types is one line, as long as the structs exist. Paste a JSON sample and this page writes them: one pub struct per object with #[derive(Debug, Clone, Serialize, Deserialize)], the use serde::{Deserialize, Serialize}; line on top, and snake_case pub fields. Where a field name differs from the JSON key, a #[serde(rename = "subscriberId")] attribute sits on the line above it.
Keys that are Rust keywords become raw identifiers (pub r#type: String), which serde serializes back as plain type. The few that can't be raw, like self and crate, get a trailing underscore and a rename. Whole numbers are i64, u64 for positive values past the signed range, and i128 past that. Arrays become Vec<T> (nested arrays Vec<Vec<T>>), and array item structs get singular names, so devices holds Vec<Device>.
With Option Types on, fields that were null or missing in some objects become Option<T>, which serde fills with None when the key is absent. Values that were only null, or whose type varied, become serde_json::Value, and an empty object becomes HashMap<String, serde_json::Value>; both need the serde_json crate, and a comment says so. Untick Use Serde for bare structs with just Debug and Clone. It all runs in your browser.
How to use it
The structs update as you type.
Paste a JSON sample
Paste into the JSON Input panel, Upload a .json or .txt file, or click Sample. Example input:
{"subscriberId":"SUB-1001","plan":"Unlimited 5G","dataUsage":45.2,"status":"active"}Set Struct Name for the root struct. If the input won't parse, check it with the JSON Validator.
Read the structs
The Rust Output panel lists the nested structs first and the root struct last. For the sample you get Account, Device, Subscriber and RootObject.
Copy or download
Copy the code or Download an .rs file. Add serde = { version = "1", features = ["derive"] } and serde_json = "1" to Cargo.toml, then let root: RootObject = serde_json::from_str(&text)?;.
Common Questions
Does the output compile?
The page Sample and our edge-case inputs (keywords, nested arrays, nulls, colliding names, big numbers) compile with rustc, checked against stand-in derive macros rather than the real serde crate. Build it in your project and review any serde_json::Value fields.
Why is a field called r#type?
type is a Rust keyword, so it can only be a field name as a raw identifier. Serde strips the r# when it maps the field, so it still reads and writes the JSON key type.
How are large integers handled?
The page reads integers exactly. An ICCID like 8901240544102066246 fits in i64; positive values up to 18446744073709551615 become u64, and anything bigger becomes i128. Check that your JSON library reads 128-bit integers if you hit that case.
What does Option Types change?
On: fields that were null, or missing from some objects in an array, become Option<T>. Off: they keep the plain type, so deserializing fails if the value really is missing. Null-only fields stay serde_json::Value either way.
What if the JSON root is an array?
The objects in it become the root struct, and a comment shows the Vec<RootObject> to deserialize into. A root that is null, a string or a list of plain values gives a short message instead of code.
Is my JSON uploaded?
No. The structs are generated by JavaScript in your browser and nothing is sent to a server. Input is capped at 256 KB.
Other JSON Tools You Might Need
Need the same payload in another language, or want to validate it first?