jsondecode.com logo

JSON to Rust Struct Generator — Free Online Tool

Convert JSON to Rust struct definitions with serde Deserialize/Serialize derives and proper types using AI. Free, no sign-up.

JSON to BigQuery Schema Converter — Free Online ToolJSON to React Flow Diagram — Convert JSON Online FreeJSON to Go Struct with BSON Tags — Free Online ToolJSON to TypeScript Interface Generator — Free OnlineJSON to YAML Converter — Convert JSON to YAML OnlineJSON to CSV Converter — Export JSON Array to CSV FreeJSON to Python Dataclass Generator — Free Online ToolJSON to SQL INSERT Statement Generator — Free OnlineJSON to Markdown Table Converter — Free Online ToolJSON to XML Converter — Convert JSON to XML Online FreeJSON to HTML Table Converter — Free Online ToolYAML to JSON Converter — Convert YAML to JSON Online FreeXML to JSON Converter — Convert XML to JSON Online FreeJSON to JSON Schema Generator — Free Online ToolJSON to GraphQL Schema Generator — Free Online ToolRuby to JSON Converter — Convert Ruby Hashes to JSON OnlineJSON to C# Class Generator — Free Online ToolJSON to Java Class (POJO) Generator — Free Online ToolJSON to Kotlin Data Class Generator — Free Online ToolJSON to PHP Array Converter — Free Online ToolCSV to JSON Converter — Free Online ToolJSON to Dart Class Generator — Free Online ToolJSON to Swift Struct (Codable) Generator — Free OnlineJSON to Terraform HCL Variables Converter — Free OnlineJSON to Mongoose Schema Generator — Free Online ToolJSON to Prisma Schema Model Generator — Free OnlineJSON to Protocol Buffer (proto3) Generator — Free OnlineJSON to TOML Config Format Converter — Free Online ToolTOML to JSON Converter — Convert TOML to JSON Online FreeJSON to Apache Avro Schema Generator — Free Online ToolJSON to OpenAPI 3.0 Schema Component — Free Online ToolJSON to R Data Frame Code Generator — Free Online ToolJSON to Lua Table Syntax Converter — Free Online ToolJSON to Zod Schema (TypeScript) Generator — Free OnlineJSON to Scala Case Class with Circe Codec — Free OnlineJSON to PowerShell Hashtable Converter — Free Online Tool

JSON to Rust Struct Generator — Free Online Tool

About JSON to Rust Struct Generator — Free Online Tool

JSON to Rust Struct converts JSON objects into Rust struct definitions annotated with serde's Deserialize and Serialize derives, mapping each JSON field to its correct Rust type. Developers use it to eliminate hand-writing boilerplate when integrating with APIs, config files, or any JSON data source in Rust projects.

JSON to Rust Type Mapping

JSON TypeExample ValueRust TypeNotes
string"hello"StringOwned heap string; use &str only with lifetime annotations
number (integer)42i64Defaults to i64; use u64 for unsigned, i32/u32 to save space
number (float)3.14f64Rust has no JSON float ambiguity; f64 matches JSON number spec
booleantrueboolDirect 1-to-1 mapping
nullnullOption<T>Nullable fields become Option<T> wrapping the inner type
array[1,2,3]Vec<T>Homogeneous arrays become Vec<T>; mixed arrays need serde_json::Value
object{"k":"v"}struct / HashMap<String,T>Named objects become structs; dynamic keys become HashMap<String,T>
nested object{"a":{"b":1}}Nested structEach nested object generates a separate named struct definition

serde Attribute Reference for Generated Structs

AttributeUsageEffect
#[derive(Serialize, Deserialize)]On structEnables serde JSON encode/decode for the type
#[serde(rename = "fieldName")]On fieldMaps a camelCase or hyphenated JSON key to a Rust snake_case field
#[serde(rename_all = "camelCase")]On structAutomatically renames all fields on serialize/deserialize
#[serde(default)]On fieldUses Default::default() when the JSON key is missing
#[serde(skip_serializing_if = "Option::is_none")]On fieldOmits null/None fields from serialized JSON output
#[serde(flatten)]On fieldMerges a nested struct's fields into the parent JSON object
#[serde(untagged)]On enumDeserializes union types without a discriminator key
#[serde(alias = "alt_name")]On fieldAccepts multiple JSON key names for the same Rust field

Frequently Asked Questions

How do I deserialize JSON into a Rust struct with serde?

Add serde = { version = "1", features = ["derive"] } and serde_json = "1" to your Cargo.toml dependencies. Annotate your struct with #[derive(Serialize, Deserialize)], then call serde_json::from_str(&json_string) to parse JSON into the struct. The tool generates the struct definition and derive annotations automatically so you can paste and compile immediately.

How does serde handle optional or nullable JSON fields in Rust?

Fields that can be null or absent in JSON should be typed as Option<T> in Rust. Use #[serde(default)] if the key may be missing entirely, and #[serde(skip_serializing_if = "Option::is_none")] to omit None values when serializing back to JSON. The generator wraps any field whose sampled value is null in Option<T> automatically.

How do I map camelCase JSON keys to snake_case Rust field names?

Add #[serde(rename_all = "camelCase")] at the struct level to automatically convert between camelCase JSON keys and snake_case Rust fields during both serialization and deserialization. For individual fields with irregular names, use #[serde(rename = "originalKey")]. The generated structs include the appropriate attribute based on the input JSON key casing.

What Rust type should I use for JSON numbers?

serde_json maps JSON integers to i64 and JSON floats to f64 by default, which matches the JSON specification's number range. You can manually narrow types to i32, u32, or f32 after generation if memory or FFI constraints require it. Avoid u64 for values that may be negative; the deserializer will return an error at runtime if the JSON value overflows the chosen type.

Can I deserialize JSON arrays with mixed types into Rust?

Rust's type system requires homogeneous Vec<T> for arrays, so mixed-type JSON arrays (e.g., [1, "text", true]) cannot be directly typed as Vec<T>. Use Vec<serde_json::Value> to accept any JSON value, then match on the variant at runtime. If the array is always one type at runtime but the sample was mixed, override the generated type to Vec<YourType> after inspection.

If jsondecode.com saved you time, share it with your team

Free forever. No ads. No sign-up. Help other developers find it.