Skip to content

JSON to Rust Struct

Convert JSON to Rust struct definitions with serde derive macros. Auto-generate typed Rust structs from JSON data. Free converter.

Rust structs will appear here...

About JSON to Rust Struct

Generate Rust structs from JSON with #[derive(Debug, Serialize, Deserialize)]. Handles nested objects (separate structs), arrays (Vec<T>), optional fields (Option<T>), and proper type inference. Configure serde rename_all, additional derives, and field visibility.

How to Use JSON to Rust Struct

1

Paste JSON

Drop your JSON sample into the input area. The parser reads it as soon as you finish typing or pasting.

2

View generated Rust

The output panel shows struct definitions with serde derives attached and the necessary use statements at the top, ready to copy.

3

Customize names if needed

Generated names default to generic terms like Root or Object when the JSON does not suggest a meaningful name. Rename them so the structs match your domain language.

4

Use in Rust project

Paste the structs into lib.rs or a dedicated module, declare serde and serde_json in Cargo.toml, and start using serde_json::from_str to parse incoming payloads.

When to Use JSON to Rust Struct

Rust development from JSON APIs

Consuming a JSON API from Rust means writing strongly typed structs that mirror the wire format, and that work is mechanical and error-prone when done by hand. The generator produces matching struct definitions from a sample payload, so you spend your time on logic rather than transcription. Backend engineers writing Rust services and systems programmers working with external APIs save hours per integration.

Schema-first Rust development

When your API contract lives in JSON Schema or a sample document, regenerating Rust types from that source keeps client and server in sync without drift. The workflow fits anyone building typed API clients, anyone who wants to guarantee struct definitions track the spec, or any team that prefers schema-first design over hand-coded types.

Serde integration

Every generated struct carries #[derive(Serialize, Deserialize)] so it works out of the box with serde, the de facto standard for JSON in Rust. The output drops directly into a project that already pulls in serde and serde_json, with no extra glue code or attribute massaging required.

Quick Rust prototyping

Pasting a JSON response and getting working Rust types back is a fast way to start prototyping. The output works for rapid spike projects, for learning how Rust types map onto familiar data shapes, and for exploring third-party API responses without committing to a full client library.

JSON to Rust Struct Examples

Simple JSON

Input
{"name": "Alice", "age": 30}
Output
#[derive(Serialize, Deserialize)]\nstruct User {\n    name: String,\n    age: i32,\n}

The basic mapping. Strings become String, integers become i32, and the generated struct comes with serde derives attached. Add it to your project, pull in serde_json, and start parsing payloads immediately.

Nested objects

Input
{"user": {"name": "Alice", "address": {"city": "NYC"}}}
Output
Multiple structs (Address, User, Root) are generated, each with the appropriate field types and the hierarchical structure preserved.

Nested JSON expands into a family of Rust structs that mirror the original hierarchy. The generator walks the document, names each level after its parent field (capitalized for Rust convention), and wires the relationships together with field types pointing at the inner structs.

Optional fields

Input
Sometimes-null fields
Output
Option<T> wraps any nullable fields, with default values handled through serde attributes.

Fields that appear as null in the sample data wind up wrapped in Option<T>, which is the idiomatic Rust pattern for optional values. The detection is best-effort based on what your sample shows; it is worth double-checking against your real schema.

Tips & Best Practices for JSON to Rust Struct

  • 1.Treat the generated code as a starting point and verify it against the real schema. The tool infers types from a single sample, which can miss variants that appear only in production data.
  • 2.Add /// doc comments to the generated structs and fields before committing them. The generator emits bare definitions, and a few lines of context about business meaning save your future self serious time.
  • 3.Be deliberate about Option<T>. The tool marks fields optional based on whether your sample contained nulls, but the actual API contract may be stricter or looser. Cross-reference the API spec rather than trusting the inference.
  • 4.For genuinely fallible operations, Result<T, E> often beats Option<T>. The generator defaults to Option because it works from sample data; switch to Result manually where the domain calls for explicit error handling.
  • 5.The default derives are Debug, Clone, Serialize, and Deserialize. Add PartialEq, Eq, Hash, or Default by hand when you need them for collections, comparisons, or test fixtures.
  • 6.Complex JSON sometimes deserves more than a plain struct. Consider a builder pattern for ergonomic construction or a custom Deserialize impl when the wire format differs from the in-memory shape you actually want.

Frequently Asked Questions

Rust insists on knowing the shape of your data at compile time, so consuming JSON means writing struct definitions that match the payload. Doing that by hand is tedious for anything beyond toy examples and prone to copy-paste typos. Generating the structs from a sample payload skips that whole class of work.