About JSON to Rust Online
Paste a JSON object and instantly get Rust struct definitions with `#[derive(Debug, Serialize, Deserialize)]`. Handles nested objects (as separate structs), arrays (as Vec<T>), snake_case field renaming with `#[serde(rename)]`, and all JSON types mapped to Rust equivalents.
Frequently Asked Questions
What is JSON to Rust struct generation?
JSON to Rust struct generation analyses a JSON sample and produces Rust struct definitions with serde Deserialize and Serialize derive macros. Each key becomes a field, and #[serde(rename)] attributes are added when the Rust field name differs from the JSON key.
What crates do I need to add to Cargo.toml for serde JSON deserialization?
Add serde = { version = "1", features = ["derive"] } and serde_json = "1" to your [dependencies] section. The derive feature enables the #[derive(Deserialize, Serialize)] macros. Run cargo build and the generated struct with those derives will deserialise JSON via serde_json::from_str(&json_string).unwrap().
How does serde handle optional or missing JSON fields in Rust?
Fields typed as Option<T> are set to None when the key is absent or the value is null in the JSON. Fields typed as T (non-optional) cause a deserialization error if absent. Use #[serde(default)] to fall back to the type's Default implementation instead of erroring on missing keys.
How do I rename camelCase JSON fields to snake_case Rust fields?
Add #[serde(rename_all = "camelCase")] at the struct level to automatically map all camelCase JSON keys to snake_case Rust fields. For individual fields, use #[serde(rename = "originalKey")]. The generator adds per-field rename attributes by default; replace them with rename_all for cleaner code if all keys follow the same convention.
Can I derive both Serialize and Deserialize on the same Rust struct?
Yes. #[derive(Serialize, Deserialize)] adds both directions with a single line. This means the struct can be used to both decode incoming JSON (from_str) and encode outgoing JSON (to_string). Add Clone and Debug derives as well for ergonomic use in most applications.
Advertisement300 × 250
Advertisement300 × 250