JSON to Rust Struct

v1.0.0
Conversion SettingsDerive OptionsField Options
  • Encode and decode base64 strings or files, commonly used for encoding binary data in text format.

  • JSON Viewer
    Similar tool

    Visualize JSON data in a tree structure, making it easier to understand complex JSON data.

  • JSON Formatter
    Similar tool

    Quickly beautify or minify JSON data, making it easier to share or work with.

  • Case Converter
    Similar tool

    Convert text to different letter cases, such as upper case, lower case, and more.

  • Convert between Celsius, Fahrenheit, and Kelvin temperature scales with ease.

  • Length Converter
    Similar tool

    Convert between different units of length, such as meters, feet, and miles.

  • Weight Converter
    Similar tool

    Convert between different units of weight, such as kilograms, pounds, and ounces.

  • Volume Converter
    Similar tool

    Convert between different units of volume, such as liters, gallons, and cubic meters.

  • Convert between different units of data storage, such as bytes, kilobytes, and gigabytes.

  • Convert Markdown files to HTML format for web publishing and sharing.

  • Convert CSV files to JSON format for easier data manipulation and integration.

  • Convert JSON data into CSV format for easier data analysis and sharing.

  • Convert JSON data into TypeScript interfaces for easy integration with your codebase.

What is the JSON to Rust Struct Tool?

The JSON to Rust Struct Tool is designed to help Rust developers quickly and accurately convert any JSON data into fully-typed, idiomatic Rust struct definitions. Whether you're working with API responses, configuration files, or data serialization, this tool makes generating Rust structs effortless and follows Rust best practices.

Understanding JSON to Rust Conversion

When working with Rust, defining the structure of your data using struct helps with:

  • Type safety at compile time
  • Memory efficiency
  • Clear documentation of expected data shapes
  • Seamless serialization/deserialization with serde

But manually writing Rust structs from large JSON data can be tedious and error-prone. That's where this tool comes in.

Features and Options

Our generator goes beyond a basic conversion. It offers several customization options to match your coding style or project needs:

🏷️ Root Struct Name

Define the name of the top-level struct (e.g., User, ProductResponse, AppConfig). This helps ensure clarity and consistency across your codebase.

πŸ”  Prefix

Add a prefix to all generated types. This is useful for grouping or avoiding name collisions.

Example:

pub struct ApiUser { ... }
pub struct ApiAddress { ... }
πŸ“¦ Serde Integration

Enable this option to automatically add Serialize and Deserialize derives from the serde crateβ€”essential for JSON serialization in Rust.

Example:

#[derive(Serialize, Deserialize)]
pub struct User {
    pub name: String,
    pub age: f64,
}
πŸ› Debug Derive

Add Debug derive to all structs for easy debugging and printing.

πŸ“‹ Clone Derive

Add Clone derive to allow deep copying of struct instances.

🟰 PartialEq Derive

Add PartialEq derive to enable equality comparisons between struct instances.

πŸ”“ Public Fields

Make all struct fields public by adding pub modifier. Useful for simple data structures.

❓ Option for Null

Wrap potentially null values in Option<T> for better null safety.

🐍 Snake Case Fields

Convert field names to snake_case following Rust naming conventions. When enabled with serde, automatically adds #[serde(rename = "originalName")] attributes.

Common Use Cases

🌐 API Response Modeling

Perfect for modeling REST API responses. The tool generates structs that work seamlessly with reqwest, tokio, and other async HTTP clients.

βš™οΈ Configuration Files

Generate structs for configuration files (JSON, YAML with serde_yaml, TOML with serde_toml).

πŸ’Ύ Data Persistence

Create structs for database models, file formats, or any structured data storage.

Rust-Specific Features

🎯 Type Mapping

Our tool intelligently maps JSON types to appropriate Rust types:

  • string β†’ String
  • number β†’ f64 (configurable)
  • boolean β†’ bool
  • array β†’ Vec<T>
  • object β†’ Custom struct
  • null β†’ Option<T> (when enabled)
πŸ”„ Derive Macros

Automatically generates appropriate derive macros based on your selections, following Rust best practices.

Getting Started

  1. Paste your JSON data into the editor
  2. Configure the generation options according to your needs
  3. Click on the "Rust Struct" tab to see the generated code
  4. Copy the generated Rust code to your project
  5. Add necessary dependencies to your Cargo.toml if using serde

Required Dependencies

If you enable serde features, add these to your Cargo.toml:

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

Example Output

Given this JSON:

{
  "name": "John Doe",
  "age": 30,
  "is_active": true,
  "address": {
    "street": "123 Main St",
    "city": "New York"
  }
}

The tool generates:

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Address {
    pub street: String,
    pub city: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RootStruct {
    pub name: String,
    pub age: f64,
    #[serde(rename = "is_active")]
    pub is_active: bool,
    pub address: Address,
}

This tool bridges the gap between JSON data and Rust's powerful type system, making it easier to work with external data sources while maintaining Rust's safety guarantees.