{ }jsonkit
Format
Validate
Convert
Query
Generate
→rs

JSON to Rust Struct Generator

Build nested Rust structs with Option, Vec and Serde derives.
353 B · paste or drop a file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
✓ complete353 B → 627 B0.0 ms

About JSON to Rust

The JSON to Rust generator converts sample payloads into nested structs suited to API clients, command-line applications and service integrations. It maps arrays to Vec, missing or null members to Option, integers to i64, decimals to f64 and strings to owned String values.

Serde mode adds Serialize and Deserialize derives and rename attributes for JSON keys that cannot be used directly as Rust field identifiers. Generated field names use snake_case, reserved words use raw identifiers, and nested object arrays are merged so the model reflects more than the first element.

Everything runs locally in the browser, which is useful for private payloads and internal schemas. The generator infers only what the pasted values demonstrate; review integer ranges, borrowed versus owned data, tagged enums, timestamps and custom deserializers before adopting the model as production code.

How it works

Map JSON shapes to Rust types

Strings become String, booleans become bool, integral values become i64 and decimal values become f64. Arrays become Vec<T>, nested objects become separate structs and explicit nulls wrap the inferred member in Option<T>.

Detect optional array-object fields

The generator merges every object in an array. A key absent from any sampled element becomes Option<T>, while compatible numeric samples are widened from i64 to f64 when needed. Incompatible element types use a conservative fallback.

Generate Serde-ready models

With Serde enabled, structs derive Debug, Clone, Serialize and Deserialize. Rename attributes preserve original keys such as display-name after the Rust field is sanitized. Mixed or unknown JSON values use serde_json::Value and therefore require serde_json.

Control field visibility

Public fields are convenient for straightforward data-transfer structs. Disable public fields when you want private state and plan to add constructors, accessors or validation methods yourself.

Frequently asked questions

Which Cargo dependencies does Serde mode require?

Add serde with its derive feature. If the generated output contains serde_json::Value because a sample was empty or heterogeneous, add serde_json as well. Disable Serde mode if you only want plain structural definitions.

Why does a field use Option?

Option is emitted when the JSON value is null or the key is missing from at least one object in a sampled array. A single object cannot reveal keys that may be absent in unseen responses, so compare against the API contract.

Does the tool infer Rust enums or chrono dates?

No. JSON does not carry enum or timestamp type metadata. String values remain String; replace them with an enum, chrono type, UUID or validated newtype when your domain rules support it.

Does generation upload my API response?

No. Parsing and Rust source generation happen locally in the browser. The tool makes no payload-processing request to a server.