JSON to Rust Serde Struct

Rust / Serde

Generate idiomatic Rust structs with #[derive(Serialize, Deserialize)], automatic #[serde(rename)] attributes, and nested struct inference.

JSON Payload
Valid JSON JSON
models.rs
0 structs Rust 2021

About This Tool

Working with JSON APIs in Rust via serde and serde_json requires explicit struct definitions for every nested object level. Hand-writing #[serde(rename = "...")] attributes for camelCase fields and escaping Rust keywords like type or match is repetitive boilerplate.

This generator parses any JSON object or array in your browser and produces ready-to-compile Rust structs with Debug, Clone, PartialEq, Serialize, and Deserialize derives.

Key Features

  • Raw Identifier Escaping: Automatically prefixes Rust keywords (type, match, mod, fn, ref) with r# so generated code compiles without warnings.
  • Serde Rename Annotations: Translates camelCase and kebab-case keys to idiomatic snake_case fields while preserving wire compatibility via #[serde(rename)].
  • Nested Struct Extraction: Recursively extracts nested objects and object arrays into strongly typed child structs.

Frequently Asked Questions

How are camelCase or hyphenated JSON keys mapped to Rust fields?
Rust conventions require snake_case struct fields. Whenever a JSON key differs from its snake_case equivalent (such as "userId" or "content-type"), the generator emits #[serde(rename = "originalKey")] above the field.
What happens if a JSON key is a reserved Rust keyword like "type" or "match"?
Reserved keywords are escaped using Rust raw identifiers (for example, pub r#type: String) paired with #[serde(rename = "type")] when needed so the struct compiles cleanly without renaming the JSON wire key.
How are null values and untyped empty arrays represented?
Null fields map to Option<serde_json::Value> and empty arrays map to Vec<serde_json::Value>, automatically adding use serde_json::Value; at the top of the file.
What does #[serde(deny_unknown_fields)] do?
When enabled, Serde rejects any incoming JSON payload that contains extra keys not declared on the Rust struct, which is useful for strict API request validation.