JSON Nerd
A cartoon badger in round glasses, thinking

JSON to Rust

Turn a JSON sample into Rust structs with serde.

Runs in your browser. Nothing you type is uploaded.

How the converter works

Paste a JSON sample and Rust structs appear as you type. Each object becomes apub struct that derives Serialize and Deserialize, soserde_json::from_str can load your data straight into it.

How types are chosen

  • Whole numbers become i64, decimals become f64, text is String and booleans are bool.
  • Arrays become Vec<T>. Arrays of objects are merged into one struct.
  • A value that can be null, or a field missing from some array items, becomes Option<T>. Missing fields also get #[serde(default)].
  • Places that hold different types (Rust has no untyped unions), null and empty arrays use serde_json::Value, which can hold any JSON value.
  • Field names are converted to snake_case with a #[serde(rename = "...")] when the key differs. Keywords such as type are written as raw identifiers (r#type).

FAQ

What do I need in Cargo.toml?

Add serde with the derive feature, and serde_json to parse the text: cargo add serde --features derive and cargo add serde_json.

Why i64 and not u32?

A sample cannot tell whether a number is ever negative or how big it can get, so the converter picks a wide signed type. Narrow it by hand where you know better.

Is my JSON sent anywhere?

No. The conversion runs in your browser, so your data stays on your device.

More JSON tools

  • JSON formatter: Format, minify and validate JSON in one place.
  • JSON validator: Check JSON syntax and find the exact line and column of an error.
  • JSON beautifier: Pretty print JSON with 2 spaces, 4 spaces or tabs.
  • JSON minifier: Strip whitespace to make JSON as small as possible.
  • JSON to Go struct: Generate Go struct definitions with json tags from a JSON sample.
  • JSON to TypeScript: Generate TypeScript interfaces or types from a JSON sample.
  • JSON to Python: Generate Pydantic models, dataclasses or TypedDicts from a JSON sample.