JSON1

JSON1 / JSON to Rust

JSON to Rust

Generate Rust structs with Serde derives from a JSON sample. Optional fields become Option types, and any key that snake_case cannot reach gets an explicit serde rename.

Processed locally
InputJSON
Ready
OutputRust

Converted output appears here as you type.

Good to know

  • Every record in an array is inspected, not just the first, so a field that appears in only some of them is typed as optional rather than required.
  • Identical nested shapes are emitted once and reused, and reserved words are renamed to something legal in this language.

About this tool

Serde does the work of deserialising JSON in Rust, but it needs structs that match the payload. This generates them with the derives already in place.

Field names are converted to snake_case, and any key that snake_case cannot round-trip gets an explicit #[serde(rename)] so the mapping is exact rather than approximate.

Why is a field wrapped in Option?
Rust has no null. A field that is optional or sometimes null becomes Option<T>, which forces the absent case to be handled at the point of use instead of panicking later.
Why serde_json::Value for some fields?
A field that was null in every record carries no type information at all, so nothing narrower would be honest. Value accepts whatever arrives; replace it with the concrete type once you have a sample where the field is populated.
How are optional fields decided?
Every record in an array is inspected, not just the first. A field that appears in some records and not others is typed as optional, and a value that is sometimes null widens accordingly. Generators that read only the first element get this wrong on exactly the payloads where it matters.
What happens to a key that is a reserved word?
It is renamed to something legal in the target language, and where the language supports it, an annotation records the original JSON key so serialisation still round-trips.