Why Generate Rust Structs from JSON?
When working with REST APIs, configuration files, or any external data source that produces JSON, the first step in a Rust project is usually defining typed structs that mirror the JSON shape. Writing these structs by hand is tedious and error-prone, especially for deeply nested payloads with dozens of fields.
This tool automates the process: paste your JSON, click a button, and get production-ready struct definitions complete with serde attributes. You can immediately drop the output into your codebase and start deserializing data with serde_json::from_str().
How the Type Mapping Works
JSON is dynamically typed while Rust is statically typed, so the converter needs to make reasonable inferences from the actual values present in your sample data.
"hello" → String 42 → i64 3.14 → f64 true → bool [1, 2, 3] → Vec<i64> null → Option<...> {...} → NestedStruct Understanding Serde Attributes
Automatically implements the Serialize and Deserialize traits from the serde crate, allowing you to convert between JSON and your Rust structs with zero boilerplate. Debug is included for easy printing during development.
Tells serde to automatically convert between Rust's idiomatic snake_case field names and the JSON's camelCase keys. This is the most common convention since most JSON APIs use camelCase while Rust style requires snake_case.
When rename_all is disabled, individual fields that differ from the JSON key get a per-field rename attribute. This is useful when keys use inconsistent casing or special characters that don't follow a single naming convention.
Typical Workflow
- 1
Grab a sample response. Copy a representative JSON payload from your API documentation, a curl response, or browser DevTools. The more fields present in the sample, the more accurate the generated types will be.
- 2
Paste and generate. Drop the JSON into the input area, set your root struct name, and click Generate. The tool will recursively walk the object tree and output all necessary structs.
- 3
Review and refine. The generated code is a strong starting point, but you may want to change
i64tou32for IDs, wrap optional fields inOption<T>, or add#[serde(default)]for fields that may be absent. - 4
Copy into your project. Hit the Copy button and paste the structs into your Rust source file. Make sure you have
serdeandserde_jsonin your Cargo.toml dependencies.
Tips for Better Results
Include all possible fields in your JSON sample. Fields absent from the sample won't appear in the generated struct. If some fields are optional, consider wrapping their types in Option manually.
Instead of the default "Root", use a descriptive name like "ApiResponse", "UserProfile", or "OrderPayload". Nested struct names are derived from their JSON key, so use descriptive keys for cleaner output.
The tool defaults to i64 for all integers since JSON numbers don't carry size or signedness information. In practice, IDs are often u64, counts are u32, and status codes are u16. Adjust these after generation.
Frequently Asked Questions
What does this JSON to Rust converter do?
It takes a JSON object or array and generates Rust struct definitions with serde derive macros. It recursively handles nested objects, arrays, nullable values, and converts field names to idiomatic snake_case.
How does the tool handle nested JSON objects?
Each nested JSON object is extracted into its own separate Rust struct. The field name is converted to PascalCase and used as the struct name. For example, a key named "shippingAddress" containing an object will produce a ShippingAddress struct.
What Rust types are generated from JSON values?
Strings become String, integers become i64, floats become f64, booleans become bool, arrays become Vec<T>, null values become Option<serde_json::Value>, and nested objects become their own named structs.
What does the rename_all camelCase option do?
When enabled, it adds #[serde(rename_all = "camelCase")] to each struct. This tells serde to automatically map between Rust's snake_case field names and the JSON's camelCase keys during serialization and deserialization.
Is this tool safe for proprietary or sensitive JSON data?
Yes. The entire conversion runs client-side in your browser using JavaScript. No data is transmitted to any server. Your JSON input never leaves your device.