JSON to Rust Struct Converter Online – Serde Code Generator

Create reviewable Rust structures and Serde annotations from JSON samples entirely in your browser.

Runs locally
Ready to generate

Generated code requires serde with its derive feature and serde_json. The JSON sample stays in your browser. Inspect optional fields and type fallbacks before production use.

Paste valid JSON (up to 200,000 characters).

130 chars

Review inferred structs and Serde annotations.

0 struct(s)

JSON to Rust guide

How to generate Serde Rust structs from JSON

Paste a valid JSON sample and generate Rust struct declarations with Serialize and Deserialize derives. Nested objects become named types; fields retain their original JSON keys through Serde rename attributes. This tool runs locally, so your payload never needs to leave the browser. This tool is free to use. No account or payment is required.

Last updated

What you can do here

  • Rust Serde struct declarations
  • JSON key preservation with serde rename
  • Nested objects and arrays
  • Option<T> for observed optional fields
  • Conservative Value fallbacks
  • Code copying
  • Browser-local conversion

How to use

  1. Paste a valid JSON sample and optionally enter a root type name.
  2. Select Generate Rust to infer Serde-compatible field types and nested structures.
  3. Review warnings about missing data, empty collections, null values, and mixed arrays.
  4. Copy the Rust declarations into a project with serde and serde_json dependencies and refine the fields.

Rust structs with Serde rename annotations

JSON objects become Rust structs with Serialize and Deserialize derives. Property names such as full-name, type, or URLs are normalized to Rust identifiers while serde(rename) preserves the original JSON key during serialization and deserialization.

Nested objects receive unique type names and arrays generate Vec<T> where sample values identify a stable element type. Null mixed with a single observed type becomes Option<T>.

Conservative inference for unknown response shapes

Mixed scalar array elements and null-only values use serde_json::Value instead of guessing an incorrect Rust union. Empty arrays also use Value because no element type has been observed.

A single JSON sample cannot establish whether a field is optional in other responses, whether number ranges fit your domain, or whether unseen API variants exist. Numeric precision and very large/deep input are guarded rather than silently altered.

  • Generated code requires serde with the derive feature and serde_json.
  • Convert and copy entirely locally without submitting sample data.
  • Inspect and compile the generated code in your Rust project before shipping it.

When to use Rust structs instead of a JSON Schema

Use Rust structs for typed application code and Serde deserialization. Use JSON Schema when you need a language-neutral contract shared with clients that do not use Rust.

If different records in one JSON array have different sets of keys, the generator may mark fields as Option<T>; confirm those differences against actual API documentation.

Nested JSON to Rust example

Preserve a hyphenated field name with a Serde rename attribute.

JSON sample

{"id":1,"full-name":"Ada"}

Rust code

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Root {
    #[serde(rename = "id")]
    pub id: i64,
    #[serde(rename = "full-name")]
    pub full_name: String,
}

Common questions

Frequently asked questions

Does this generator execute Rust or upload the JSON?

No. It parses the JSON and emits text locally in the browser. The generated Rust code is never executed here.

What dependencies does the Rust output require?

Add serde with the derive feature and serde_json to your Cargo dependencies. Review the generated model before integrating it into production code.

What happens to null values and mixed-type arrays?

A null-only field has an unknown non-null type and uses serde_json::Value. Null alongside one known type becomes Option<T>. Other mixed types use Value with a warning.

Can the tool infer optional fields from one sample?

Not reliably. Fields present in a single object are treated as required unless observed object array variants omit them. Confirm real response variants manually.

Are original JSON field names preserved?

Yes. Serde rename attributes preserve exact JSON keys even when Rust identifiers must be normalized or disambiguated.

Debugging a specific JSON issue? Browse JSON troubleshooting.