JSON to Rust Converter

Rust structs with serde derives, generated from a JSON sample

JSON Input

Rust Output

Paste your JSON on the left and the Rust code shows up here.

Or drop a file on this box, , or .

What does the JSON to Rust converter generate?

With serde, reading JSON into Rust types is one line, as long as the structs exist. Give this page a sample and it writes the structs for you: 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, any field that showed up as null, or was absent from at least one object, becomes 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. Nothing gets compiled here; the code is produced as text in the page.

How to use it

The structs update as you type.

1

Paste a JSON sample

Paste into JSON Input or pick a .json or .txt via Upload. The Sample button loads a telecom subscriber. A flight status record with a nested crew list shows the renames serde needs:

{"flightNo":"SK1427","gate":"B12","delayMinutes":0,"cancelled":false,"crew":[{"name":"Sigrid Lund","role":"captain"}]}

Struct Name sets the root struct. Name it Flight and the crew array gets its own CrewItem struct, while flightNo turns into flight_no with #[serde(rename = "flightNo")] above it. An Invalid JSON message means the input has a syntax slip; the JSON Validator finds it.

2

Read the structs

In the Rust Output panel each nested struct appears before the struct that uses it, so the root always sits at the bottom. The built-in Sample produces Account, Device and Subscriber above RootObject, ready to drop into a module.

3

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?

Each integer is typed from the digits you pasted, never from a rounded double. An ICCID like 8901240544102066246, nineteen digits long, still sits below the i64 ceiling of 9223372036854775807; positive values between that and 18446744073709551615 get u64, and anything larger gets i128. If a field lands on i128, confirm your deserializer accepts 128-bit integers.

What does Option Types change?

With it on, a key that held null somewhere, or that one array element left out, is typed Option<T>. With it off the plain type stays, and serde reports a missing field error the first time a payload omits it. A key that was null in every object can't be typed at all, so it is serde_json::Value in both modes.

Does it handle a top-level array of records?

Yes. The array's objects become the root struct, and a comment names the type to deserialize into, for example // The JSON root is an array: deserialize it as Vec<Flight>. A bare [] is different: there is nothing to infer from, so instead of code you get a message saying the root is empty and asking for an object with at least one key.

Is my JSON uploaded?

No. The structs are written in your browser and the payload isn't sent to a server. Beyond 256 KB you get a size message instead of code; serde only cares about the shape, so a trimmed sample with a few array entries gives you identical structs.

Other JSON Tools You Might Need

When serde rejects a payload, the Schema Generator helps you see which field broke the contract. The TypeScript and Java pages cover the clients that send or receive the same JSON.