JSON to Rust Structs

Paste JSON to generate Rust structs with serde derive macros. 100% client-side.

JSON Input
Rust Output
Paste JSON above to generate Rust structs.

How to Use the JSON to Rust Converter

  1. Paste your JSON into the input area on the left. The tool accepts any valid JSON object or array.
  2. Choose a mode — Serde (default), Serde + Option (all fields wrapped in Option), or Public Fields (adds pub visibility).
  3. View the generated Rust structs on the right with proper field names and type mappings.
  4. Copy or download the output as a .rs file ready to paste into your project.

What This Tool Generates

This converter analyzes your JSON structure and generates idiomatic Rust structs with #[derive(Debug, Serialize, Deserialize)] macros from the serde framework. It handles nested objects as separate structs, arrays as Vec<T>, and null values as Option<T>.

JSON to Rust Type Mapping

  • JSON stringString
  • JSON integeri64
  • JSON floatf64
  • JSON booleanbool
  • JSON arrayVec<T> (type inferred from first element)
  • JSON object → nested struct
  • JSON nullOption<serde_json::Value>
  • Mixed arraysVec<serde_json::Value>

Field Name Conventions

JSON keys are converted to snake_case for Rust field names following the Rust naming convention. If the original JSON key differs from the snake_case conversion, a #[serde(rename = "original_key")] attribute is added automatically, ensuring correct deserialization. Struct names are generated in PascalCase, with the root struct named Root and nested structs named after their parent field.

Cargo.toml Dependencies

To use the generated code, add these dependencies to your Cargo.toml:

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

Mode Differences

  • Serde — generates the most idiomatic code. Fields are the exact types inferred from the JSON. Use this when your JSON structure is fixed and reliable.
  • Serde + Option — wraps all fields in Option<T>. Use this when consuming APIs that may omit fields or return null values. Prevents deserialization panics when fields are absent.
  • Public Fields — adds pub visibility to all struct fields. Use this when the struct is in a library module and fields need to be accessed from outside the module.

Working with the Generated Code

After generating your structs, use serde_json::from_str() to deserialize JSON into the struct and serde_json::to_string() to serialize back to JSON. For API clients built with reqwest, you can use .json::<Root>() directly on the response, which handles deserialization automatically. The generated structs are also compatible with actix-web, axum, and other Rust web frameworks that support serde types.

Handling Complex JSON Structures

When your JSON contains deeply nested objects, the converter generates a separate struct for each level of nesting. This produces clean, reusable code but may result in many struct definitions for complex data. For simple cases where you need flexibility, consider using serde_json::Value directly, which can represent any valid JSON value without a predefined schema. The generated code is a starting point — you should review and adjust types (for example, changing i64 to u32 if values are always non-negative) based on your specific use case.

Frequently Asked Questions

Serde is a Rust framework for serializing and deserializing data. It provides Serialize and Deserialize derive macros that automatically implement the conversion logic for your structs. Serde supports JSON (via serde_json), YAML, TOML, MessagePack, and many other formats.
No. This JSON to Rust converter runs 100% in your browser using JavaScript. Your data never leaves your machine. There is no server-side processing, no logging, and no data collection.
The converter maps JSON types to Rust types as follows: JSON strings become String, JSON integers become i64, JSON floats become f64, JSON booleans become bool, JSON arrays become Vec<T>, JSON objects become nested structs, and null values become Option<T>. Field names are converted to snake_case and struct names to PascalCase.
Use Option<T> when a field may be absent or null in the JSON data. The "Serde + Option" mode wraps all fields in Option<T>, which is useful when consuming APIs that may omit fields or return null values. This prevents deserialization failures when fields are missing.
Add the following to your Cargo.toml: serde = { version = "1", features = ["derive"] } and serde_json = "1". These are the two core dependencies for JSON serialization in Rust. The derive feature enables the #[derive(Serialize, Deserialize)] macros used by the generated code.