JSON to Rust Struct Generator
Generate Rust structs from JSON with #[derive(Serialize, Deserialize)] and proper serde attributes. Fields use snake_case with automatic rename annotations when the JSON key differs.
JSON to Code Generator
Generate typed code from JSON in 8 languages — Go structs, Python dataclasses, Java/C#/Dart/Kotlin classes, Rust/Swift structs. Handles nested objects, arrays, and null values.
Supported Languages
| Language | Output Type | Serialization |
|---|---|---|
| Go | Structs with json tags | encoding/json |
| Python | Dataclasses with type hints | dataclasses |
| Java | Classes with getters/setters | Jackson / Gson |
| C# | Classes with JsonPropertyName | System.Text.Json |
| Dart | Classes with fromJson/toJson | dart:convert |
| Rust | Structs with serde derive | serde |
| Swift | Codable structs | Codable |
| Kotlin | Data classes with @Serializable | kotlinx.serialization |
How It Works
- Nested objects — each nested object becomes a separate named type/class/struct, referenced by the parent.
- Arrays — element types are inferred from the first item. Object arrays merge all items for complete field coverage.
- Numbers — integers and floats are detected automatically (e.g.,
int64vsfloat64in Go). - Null values— mapped to each language's nullable/optional type (e.g.,
Any?in Kotlin,Optionin Rust). - Naming conventions— field names follow each language's idiom (camelCase, snake_case, PascalCase) with JSON key annotations.
- Everything runs in your browser — no data is sent over the network.
Serde Integration
The generated structs use serde's Serialize and Deserialize derive macros, the standard way to handle JSON in Rust. Fields that need renaming (e.g., camelCase JSON keys to snake_case Rust fields) get #[serde(rename = "...")] attributes automatically.
// JSON input
// { "name": "Alice", "age": 30, "active": true }
// Generated Rust struct
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
struct User {
name: String,
age: i64,
active: bool,
}
// Usage
let json = r#"{"name":"Alice","age":30,"active":true}"#;
let user: User = serde_json::from_str(json).unwrap();
println!("{:?}", user);Rust Type Mapping
JSON strings map to String, booleans to bool, integers to i64, floats to f64, arrays to Vec<T>, and null values to Option<serde_json::Value>. Nested objects become separate pub structs.
Ownership and Public Fields
All fields are generated as pub for easy access. String types use owned String rather than &str since JSON deserialization typically produces owned data. You can adjust visibility and lifetimes as needed.
Frequently Asked Questions
What crate dependencies do I need?
You need serde with the derive feature and serde_json in your Cargo.toml: serde = { version = "1", features = ["derive"] } and serde_json = "1".
Does this handle optional fields?
Null JSON values become Option<serde_json::Value>. For more specific optional typing, you may want to manually adjust to Option<String> or Option<i64> based on your schema.
Can I use this with nested JSON?
Yes. Each nested object becomes a separate named struct. The generator handles arbitrary nesting depth and creates properly ordered type definitions.
Related Convert Tools
cURL to Code
Convert cURL commands to JavaScript, Python, Go, PHP, Ruby, and Java code instantly
JSON to CSV Converter
Convert JSON arrays to CSV with nested object flattening, column selection, and .csv download
TOML ↔ JSON/YAML
Convert between TOML, JSON, and YAML — perfect for Cargo.toml and pyproject.toml
Encode / Decode Multi-Tool
Base64, Base32, Hex, Binary, URL, and HTML encoding & decoding all in one tool