JSON to Rust

Generate Rust structs from a JSON sample, with fields wrapped in Option if they may be absent. Review the number types before using them with real data.

JSON to Rust

Data & Format Tools

Runs locally in your browser.

Runs locally in your browser — your JSON is converted on your device and never uploaded.

About the JSON to Rust

Rust's reputation for memory safety comes with a cost, namely a very strict type system. If you need to consume an external API's JSON data in your Rust program, you need to write a struct with the same fields as in the JSON, and define what type each field is in Rust. Writing these structs for every large nested API response is repetitive work. That's why this JSON to Rust converter can come in handy; just paste your JSON input and it will generate the structs you need to deserialize the data in Rust. This tool was built with a strong focus on privacy, so your JSON data will always stay on your computer and never leave the browser.

Key Features of JSON to Rust

  • Automated type inferenceAnalyze your JSON values and select the correct Rust types and structure for the deserializer to use.
  • Smart optional fieldsWant to make fields optional in your deserialized struct? Toggle the "Fields" dropdown and select either "Required" or "Optionals".
  • Deep nesting resolutionPaste any JSON you want to deserialize in Rust and the tool will figure out all the nesting needed for the struct(s).
  • Custom root nameYou can customize the name of the root struct using the "Root struct name" field.
  • 100% local processingYour JSON data is parsed and converted to Rust code only in your browser.

How to Use JSON to Rust

  1. Open the JSON to Rust converter page.
  2. Paste your JSON in the "JSON input" textarea.
  3. Optionally, change the name of the root struct by typing in the "Root struct name" field. By default, the name will be "Root".
  4. Use the "Fields" dropdown to select either "Required" or "Optionals" for the struct fields.
  5. Click the purple "Generate" button and wait for the tool to generate the Rust code.
  6. Check the generated structs in the "Output" section underneath the "Rust code" tab.
  7. Click "Copy" to copy the code to your clipboard or "Download" to save it as a .rs file to your computer.

JSON to Rust Examples

A struct with Serde derives.

Input
{"id":7,"name":"Amal","active":true,"tags":["admin"]}
Result
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Root {
    pub id: i64,
    pub name: String,
    pub active: bool,
    pub tags: Vec<String>,
}

JSON to Rust — Frequently Asked Questions

What is a Rust struct?
A struct (structure) is a custom data type in Rust that lets you package together and name multiple related values. Because Rust is a statically typed language, you need to define a struct with fields of a specific data type in order to store values of these types in variables.
How do I use this with Serde?
The structs generated by this tool are ready to be used with Rust's popular serialization framework, Serde. Just make sure to add the derive macros in the structs' definition. The generated code already includes the necessary attribute for serde_json.
Is my API payload data safe here?
The JSON to Rust tool will only process your input data locally in your browser. Your JSON data never leaves your computer and is never sent to any server.