JSON Input

Rust Output

What Is JSON to Rust?

If you're calling a REST API in Rust, you need matching struct definitions before you can deserialize anything with Serde. Writing those structs by hand — especially for deeply nested JSON — is tedious and error-prone. This tool generates Serde-compatible Rust structs automatically. Paste your JSON, get ready-to-use structs with #[derive(Serialize, Deserialize)], and drop them straight into your Cargo project.

This tool generates Rust structs from your JSON. Enable Serde for Serialize/Deserialize derives. Enable Option Types for optional fields. The output works with serde_json::from_str and serde_json::to_string.

Conversion runs entirely in your browser. Your JSON is never sent to a server.

When JSON to Rust Helps

When building Rust apps that consume REST APIs, you need typed structs. Paste a sample response here to generate matching structs with Serde support.

How to Use This Tool

1

Paste or Upload JSON

Copy your JSON and paste it into the left editor. You can also click Upload to load a file. Use the Sample button for example data. Set the struct name and options in the config panel.

2

Review the Generated Structs

The right panel shows the generated Rust structs. Nested objects become separate structs. Arrays become Vec<T>. If your JSON has invalid syntax, fix it first using the JSON Formatter or JSON Validator.

3

Copy or Download

Pull the generated code out with Copy or Download. Add serde and serde_json to your Cargo.toml. Paste into your Rust project.

JSON to Rust Examples

Here is an example of generating Rust structs from a JSON object.

Example: Subscriber record

JSON input:

Input

Generated Rust output:

Output

When JSON to Rust Helps

Most developers need this when integrating with REST APIs. Pasting it here gives you Serde-compatible structs you can use immediately.

If you need to merge two JSON files first, there's a separate JSON Merge tool for that.

API responses, config files, or data exports are often JSON. Running them through here helps you generate Rust structs.

Frequently Asked Questions

How do I deserialize JSON in Rust with Serde?

Add serde = { version = "1.0", features = ["derive"] } and serde_json = "1.0" to your Cargo.toml. Derive Deserialize on your struct, then call serde_json::from_str::<YourStruct>(&json_string). Generate the struct here by pasting your JSON.

What does #[serde(rename)] do in Rust?

#[serde(rename = "jsonKey")] tells Serde to map a specific JSON key to a Rust field name. This is useful when JSON uses camelCase (like planName) but your Rust struct uses snake_case (plan_name). The generator adds these automatically when needed. Serde lists rename next to default and skip_serializing_if on its field attributes page, which is worth a skim before you hand-edit the output.

How do I handle optional or nullable JSON fields in Rust?

Enable Option Types to wrap nullable fields in Option<T>. This means absent or null JSON fields deserialize to None instead of causing an error. You can also add #[serde(skip_serializing_if = "Option::is_none")] to omit null fields when serializing.

Is my JSON sent to a server?

No. The Rust structs are generated on this page itself; your JSON is never uploaded.

What about nested JSON objects and arrays?

Nested JSON objects become separate Rust structs, each with its own #[derive(Serialize, Deserialize)]. Arrays become Vec<T> of those struct types. The generator handles arbitrary nesting depth.

Related Tools

For Rust JSON, see Serde and serde_json. For JSON, see the JSON specification.