CruxDevCruxDev Tools Prompts
dev Utility • Zero-Server Privacy • 100% Client-Side

JSON → Rust Struct (Serde)

Generate Rust structs with #[derive(Serialize, Deserialize)] and proper serde attributes from JSON samples.

Overview

Convert a JSON sample into a Rust struct wired for serde serialization and deserialization. Field types are inferred from the values you paste, the serde derives are included in the output, and the whole conversion happens in the browser with nothing sent to a server.

How It Works

A small sample object demonstrates the output format on load. Replace it with your own JSON and the struct regenerates immediately. Scalars and invalid JSON show a red error with no code; a top-level array of objects is accepted and described by one struct. Click the output panel once to copy the entire snippet, imports included.

Step-by-Step Usage Guide

  1. Paste the JSON object into the left-hand editor.
  2. Inspect the generated struct in the output panel.
  3. Check the snake_case field names and the serde rename attributes the tool adds for you.
  4. Click the output to copy, then drop it into your crate next to your serde_json usage.

Technical Specifications & Standards

The mapper emits Rust-primitive-friendly types: strings become String, booleans bool, whole numbers i64, decimals f64, and a field that is always null Option<serde_json::Value>, which keeps the struct deserializable when a value is null on the wire; fields that can be null or are missing from some array elements are wrapped in Option<T>. Arrays become Vec<T> with the element types of all items merged, an empty array becomes Vec<serde_json::Value>, and each nested object becomes its own pub struct named after its key. Field names are always converted to snake_case, and whenever the name differs from the JSON key the tool adds #[serde(rename = "original-key")] so the wire name is preserved; Rust keywords are written as raw identifiers such as r#type. The generated derive includes Debug, Serialize, and Deserialize, so the struct is immediately usable with serde_json::from_str and to_string. Keep in mind that integers beyond 2^53 cannot be held exactly by the browser's Number type, so they are typed f64 rather than i64; verify oversized ids against the raw payload.

Targeted Use Cases

  • Modeling a JSON API response in a Rust service or CLI before writing the request handling code.
  • Creating typed configuration structs for an application that reads JSON files.
  • Drafting serde test fixtures quickly during development.
  • Sharing a canonical struct shape with teammates by generating it from the same sample.

Notes & Gotchas

  • Make sure arrays are non-empty in the sample so the Vec element type is inferred instead of Vec<serde_json::Value>.
  • Replace serde_json::Value fields with concrete types once you know the real shape.
  • Keep the generated #[serde(rename)] attributes for keys that are not snake_case instead of editing the JSON.
  • Use Option<T> for fields the API omits; the tool wraps fields missing from some array elements, and null samples give Option<serde_json::Value>.

Frequently Asked Questions

Why serde_json::Value for null fields?

Rust needs a concrete type, and a null sample gives the generator nothing to infer. serde_json::Value accepts anything, and the field is wrapped in Option, so the struct compiles and a real null still deserializes. Narrow it to Option<String> or a similar type once you know the intent.

Does it generate nested structs?

Yes. Each nested object becomes its own pub struct, named after its key, and the parent field uses that type. Rename the structs to suit your crate.

Which serde crates does the output target?

The snippet uses the stable derive macros from the serde crate via use serde::{Deserialize, Serialize}, and references serde_json::Value for unknown types. It has no dependency on optional serde features.

Is anything sent to a server?

No. Parsing and code generation run locally in the page. The tool keeps working with your network disconnected after the initial load.