JSON Schema to Rust Converter - Generate Rust Structs Online
Paste a JSON Schema and get Rust structs and enums with serde attributes.
How to Convert JSON Schema to Rust - Step by Step Guide
Input Your JSON Schema
Start by providing your JSON Schema definition for Rust code generation. Whether you're building an API, validating configuration files, or defining data structures for your Rust application, you have several convenient options. AI tools like ChatGPT or Perplexity AI can help refine the schema and generated structs:
Example: JSON Schema Input
A typical JSON Schema defining a User object with validation rules:
{ "$schema": "http://json-schema.org/draft-07/schema#", "title": "User", "type": "object", "required": ["id", "email"], "properties": { "id": { "type": "integer" }, "email": { "type": "string", "format": "email" }, "name": { "type": "string" } } }
Automatic Rust Code Generation
Shortly after you stop typing, the converter generates Rust structs with proper serde annotations:
Option<T>#[derive(Serialize, Deserialize)] for JSON serializationExample: Generated Rust Struct
The same schema converted to idiomatic Rust code:
use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub struct User { pub id: i64, pub email: String, #[serde(skip_serializing_if = "Option::is_none")] pub name: Option<String>, }
Customize Generation Options
Tailor the generated Rust code to match your project's conventions and requirements:
Clone, PartialEq, Defaultpub and private fieldsExport Your Rust Code
When the structs look right, take them into your project:
Frequently Asked Questions
What is JSON Schema and why turn it into Rust?
JSON Schema describes what a JSON document is allowed to look like: which fields exist, their types and which ones are required. Turning it into Rust structs means the compiler knows that shape too, so an API client or a config loader built with serde fails at the parsing step instead of somewhere deep in your code.
Which JSON Schema keywords are read?
type (including lists like ["string", "null"]), properties, required, items, enum, const, integer formats such as int32 or uint64, minimum and maximum, additionalProperties with a schema, allOf (the parts are merged into one struct) and $ref pointing inside the same file, such as #/$defs/Name or #/definitions/Name. A oneOf or anyOf that is one type plus null becomes Option<T>. Anything with several real shapes becomes serde_json::Value, and a note at the top of the output says so.
How are optional and nullable fields handled?
A field that is not listed in required, or whose type allows null, becomes Option<T> with skip_serializing_if, so missing values are left out when you serialize. A field that points back to its own struct is wrapped in Box so the type has a fixed size.
Which integer type does a field get?
i64 by default. A format such as int32 or uint64 is used as written, and a maximum above the i64 range with a minimum of 0 or more gives u64. Larger ranges, or the i128 option, give i128. The limits are read from the exact digits in your schema, so a value like 18446744073709551615 is not rounded on the way.
What do I need in Cargo.toml?
serde with the derive feature and serde_json: serde = { version = "1", features = ["derive"] } and serde_json = "1". serde_json is used for fields that stay untyped (serde_json::Value), and HashMap comes from the standard library.
What happens to field names that are not valid in Rust?
Names are turned into snake_case. Keywords such as type or match get a _field suffix, names starting with a digit get a field_ prefix, and names that clash after cleaning get _2, _3 and so on. Whenever the Rust name differs from the JSON key, a serde rename keeps the original key. Struct names that would hide String, Option or Vec get a Type suffix.
Is my schema uploaded anywhere?
No. The conversion runs in your browser, so the schema you paste or upload stays on your machine.
Related Tools
JSON to Rust
Convert JSON data to Rust structs with serde Serialize/Deserialize traits
YAML to Rust
Convert YAML configuration to Rust structs with serde
TOML to Rust
Convert TOML configuration to Rust config structs
XML to Rust
Convert XML data to Rust structs with serde serialization support
CSV to Rust
Convert CSV data to Rust structs with field mapping
TypeScript to Rust
Convert TypeScript interfaces and types to Rust structs