Rust to JSON Converter

Paste Rust structs with Serialize derive. Get clean JSON back.

Input

Output

Paste your Rust code on the left and press Convert.

Or drop a file on this box, , or .

What this tool does

You have a struct with #[derive(Serialize)] on it and all you want is the JSON it would produce. Doing that the usual way means adding serde and serde_json to Cargo.toml, writing a main that calls to_string_pretty, and waiting for cargo run to compile. Paste the Rust here instead. One struct literal, a module with several types, or values wrapped in Vec, Option and HashMap all come back as one JSON document with the field values filled in.

The converter tracks how serde actually serializes values. String and &str become JSON strings. i32, u64, f32, f64 all become JSON numbers. bool becomes true/false. Vec<T>, &[T], and [T; N] become JSON arrays. HashMap<String, V> / BTreeMap<String, V> become JSON objects. Option<T> with None becomes JSON null; with Some(value) it serializes the inner type. Tuples come out as heterogeneous arrays: the exact behaviour documented at serde.rs/data-model.

Serde attributes are read from the source. #[serde(rename = "x")] changes the key, #[serde(skip)] drops the field and #[serde(flatten)] pulls a nested struct's fields up into the parent. Enums use serde's default externally tagged layout unless you set #[serde(tag = "type")]. Several struct instances in one paste each get a key of their own. The JSON is ready for an axum or actix-web integration test, or for any client built on the Rust standard library and reqwest.

How to use it

Three steps, and no cargo build anywhere, whether you paste one struct or a module of twelve.

1

Paste your Rust (or load the sample)

Paste the Rust into the left editor, use lines and derives included. A struct literal, a tuple struct, an enum with variants, several types or values inside Vec/Option/HashMap are accepted. Load Sample gives you an Order built in fn main, with a Vec<OrderItem> and notes: None.

You do not need to remove use statements, lifetimes, or derive attributes. Leave the Rust syntax as-is: the serde attributes are part of what the parser reads.

2

Hit Convert

Press Convert. The source is sent to the backend, which reads the struct literals (in main or wherever they sit), applies any serde attributes and returns JSON. Nothing is compiled, so a missing crate or a borrow-checker complaint does not get in the way. The "Converting..." overlay goes away when the response arrives.

3

Copy the JSON

The JSON appears in the right editor. Drop it into a reqwest call, an axum test driven by tower::ServiceExt::oneshot, an OpenAPI example, or the -d argument of a curl command.

When this actually comes in handy

Web service fixtures

An axum handler takes Json<CreateOrder> and you want to hit it from Postman. Paste the CreateOrder struct with a sample value and you have the body without writing a throwaway binary.

CLI config starters

A clap-driven CLI with a Config struct. Convert it to a JSON template users can edit, without writing a custom Default impl just for docs.

Integration test seeds

Struct literals in #[test] functions make good seed data. Convert them into JSON files and load those into wiremock, another mock server or an embedded database for integration runs.

Docs that track the code

Generate JSON examples for your README or OpenAPI spec straight from the real Rust types, so the docs stop drifting.

Common questions

Do I need serde and serde_json in my Cargo.toml?

Not for this page. The conversion happens on the backend from the source text alone, so nothing has to build. In your own crate you still need serde with the derive feature plus serde_json to get the same JSON at runtime.

Does it honour #[serde(rename)], #[serde(skip)], and #[serde(flatten)]?

Yes. #[serde(rename = "x")] renames the JSON key, #[serde(skip)] removes the field from output, and #[serde(flatten)] lifts nested struct fields to the parent: matching the serde field attributes documentation.

How are Option, Vec, and HashMap handled?

Option<T> with None becomes JSON null; with Some(v) it serializes the inner value. Vec<T>, slices, and arrays become JSON arrays. HashMap<String, V> and BTreeMap<String, V> become JSON objects; maps with non-string keys have their keys stringified.

What about enums with variants?

By default, enum variants use the externally-tagged representation ({ "VariantName": {...} }). The #[serde(tag = "type")], #[serde(untagged)], and #[serde(content = "data")] attributes switch to internally-tagged, untagged, or adjacently-tagged: the tool reads those attributes and outputs accordingly.

Is my code stored?

The Rust source is posted to the backend for this one conversion; nothing is persisted or logged. Snippets that embed secrets, say a const API_KEY: &str, deserve a quick scrub before you paste.

What if the Rust uses lifetimes or generics?

Lifetimes (&'a str) are ignored: JSON has no concept of them. Concrete generic types (Vec<User>) work. Uninstantiated generics come out as null. Macros that expand to real syntax work; macros that rely on a proc-macro crate not visible here are treated as opaque.

Other tools you may need

Converting other languages, or checking the output? These sit right next to this one: