b2KIT

JSON to Rust Struct

Generate Rust struct definitions with serde derives from JSON data with Option types for nullable fields.

Tested tool guide Tested browser tools Checked August 16, 2026

What JSON to Rust Struct does and how it behaves

JSON object members are recast as Rust fields, with nested objects producing additional struct definitions and arrays producing collection fields. The observed strings, numbers, booleans, arrays, objects, and nullability guide the selected Rust types. The declarations include Serde derive annotations, and nullable fields use Option types. The main surprise is that the result reflects only the pasted sample. A field that is always present and non-null in that sample may still be optional or nullable in other responses, so the generated model is a starting point rather than an API contract.

How the result is produced

1

Object and collection mapping

Each JSON object supplies a candidate Rust struct, and each member supplies a candidate field. Nested objects require additional struct definitions, while arrays become Vec fields using a type inferred from their elements. Strings and booleans have direct Rust counterparts; numbers require a concrete numeric choice. The result describes the values present in the pasted JSON, not every value the source may later send.

2

Nullable field handling

When the JSON evidence marks a field as nullable, the declaration uses Option<T> around its inferred Rust type. This lets derived Serde deserialization represent null as None. A null value by itself does not reveal the intended inner T, so inspect such fields carefully. The generated Option models the Rust data shape; it is not proof that the producer formally permits omission or null.

Good uses

  • Turn a captured REST API response into an initial set of Rust response structs before adding endpoint-specific validation, domain types, and error handling.
  • Convert a representative JSON configuration document into Serde-compatible Rust types, then compare the generated fields with the configuration's documented requirements.
  • Sketch the data model for a deeply nested JSON fixture so that object boundaries, Vec fields, and nullable values are visible as Rust declarations.

Limits and checks

  • A single JSON sample cannot reveal fields that were omitted, alternate object shapes, or values that become null only under other conditions. Compare several representative payloads before treating the structs as stable.
  • JSON numbers do not specify Rust widths, signedness, or application limits. Check every generated numeric field against the source contract, especially identifiers, large counters, decimals, and values that may exceed the sample.
  • JSON keys may contain punctuation, collide after Rust-style renaming, match Rust keywords, or require exact serialized names. Verify generated identifiers and any Serde rename attributes before compiling or deserializing production data.

Common questions

Will the generated structs compile as pasted?

Not necessarily in every crate. The project must provide Serde and make the requested derive macros available, commonly through Serde's derive feature. The surrounding module may also need imports, visibility choices, or adjusted type names. Generated identifiers and numeric types still require review because the JSON sample cannot describe your crate layout or the producer's complete contract.

Does Option distinguish a missing field from a JSON null?

No. With ordinary Serde-derived deserialization, both a missing Option<T> field and an explicit null normally become None. That loses the distinction between omission and null. During serialization, None is normally written as null unless you add behavior such as #[serde(skip_serializing_if = "Option::is_none")]. Use a different representation if that distinction matters.

References and verification

The behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools