JSON To Rust Generator

Need to convert Rust into JSON Schema?

Rust Struct → JSON Schema

JSON Input

Loading editor…

Generated Rust

Configuration

Enter JSON data to generate Rust structs

Memory-safe structs with ownership

How to convert JSON to Rust – step-by-step guide

Use this JSON to Rust generator to create Rust structs from JSON samples for services and CLI tools with serde support.

  1. Step 1 – Paste a JSON sample

    • Paste a representative JSON object or array into the left editor.
    • Include nested objects, arrays, and nullable fields so types are inferred correctly.
    • Use Import to load JSON from a file, URL, or sample data.
  2. Step 2 – Choose Rust struct options

    • Set a root Struct Name (for example Root).
    • Decide how to handle optional fields (Option<T>) and unknown values (serde_json::Value).
    • Confirm naming and serde attributes so JSON keys map correctly.
  3. Step 3 – Review the generated code

    • Check field names, types, and how arrays/objects are modeled.
    • Adjust options like Root Type Name, null handling, and frameworks if available.
    • If a field is inferred incorrectly, tweak your sample JSON and regenerate.
  4. Step 4 – Use the structs with serde

    • Add serde and serde_json to your dependencies and enable derive macros.
    • Deserialize JSON into your root struct using serde_json::from_str.
    • Add validation after deserialization when you need stronger guarantees.
  5. Step 5 – Copy or download

    • Copy the output into your project or download it as a file.
    • Run your formatter/linter to match your code style.
    • Add JSON parsing/serialization libraries if your language requires them.

Quick tips

  • Use Option<T> for fields that can be missing or null.
  • Prefer chrono types only when your timestamp format is stable.
  • Keep DTO structs separate from internal domain types for flexibility.
Example output (simplified)
// JSON input
{
  "id": 123,
  "name": "Maeve Winters",
  "email": "maeve@example.com",
  "active": true,
  "roles": ["admin", "editor"],
  "metadata": { "plan": "pro" },
  "createdAt": "2024-03-01T10:15:00Z",
  "score": 99.5,
  "notes": null
}

// Generated Rust structs (simplified)
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct Metadata {
  pub plan: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Root {
  pub id: i64,
  pub name: String,
  pub email: Option<String>,
  pub active: bool,
  pub roles: Vec<String>,
  pub metadata: Metadata,
  pub created_at: String,
  pub score: f64,
  pub notes: serde_json::Value,
}

Related JSON & Rust tools

Explore more JSON and schema tools that work great alongside this JSON to Rust generator.

Frequently Asked Questions

What Rust features are supported?

The generator creates Rust structs with proper ownership, supports Serde serialization/deserialization, and follows Rust naming conventions with memory safety guarantees.

Can I generate Serde-compatible structs?

Yes! Select 'Serde' as the framework to generate structs with #[derive(Serialize, Deserialize)] attributes for automatic JSON serialization with the serde crate.

How does Option handling work?

Rust generator uses Option<T> for nullable fields by default, ensuring memory safety and explicit null handling following Rust's ownership model.

What about borrowing and lifetimes?

Generated Rust structs use owned types (String instead of &str) to avoid lifetime complexity, making them easier to use while maintaining Rust's safety guarantees.

JSON to Rust Struct Generator Online | JSONSwiss