JSON to Rust Structs – Generate Rust Types from JSON Free

Use the free JSON to Rust Structs below: your data never leaves your browser — all work happens on your device, and nothing is uploaded to any server.

JSON to Rust Structs With Serde in Mind

Rust developers working with APIs face a specific chore: turning a JSON response into structs that serde can serialize and deserialize. The structs need the right field names, the right types, and the derive attributes that make serde work. Writing them by hand is slow, and a single wrong type produces a compile error or, worse, a runtime deserialization failure. This tool generates the structs from your JSON, with serde derives included, so you can paste the result into your project and keep moving.

Paste the JSON, set the root struct name, and press Convert. The tool reads the whole document and produces one struct per object. Text becomes String, whole numbers become i64, decimals become f64, true and false become bool, and arrays become Vec of the element type. Every struct carries the Debug, Serialize, and Deserialize derives, which is the standard starting point for API models in Rust.

Field Names and Serde Rename

Rust convention uses snake_case for field names, and the tool converts JSON keys to snake_case. A key like zipCode becomes zip_code. A key like isActive becomes is_active. When the converted name differs from the original key, the tool adds a serde rename attribute with the original key, so deserialization still matches the JSON exactly. You do not need to remember which fields were renamed: the attribute is right there in the output.

This matters because serde matches field names exactly by default. Without the rename attribute, a field called is_active would look for a JSON key called is_active, and your API sends isActive, so the field would fail to fill or the whole parse would fail. The rename attribute closes that gap automatically. If you prefer to configure serde differently, for example with rename_all on the struct, you can remove the individual attributes and add one line per struct. The generated code is a starting point, not a straitjacket.

Types, Options, and Unknown Values

Numbers deserve care in Rust because the type system is strict. The tool uses i64 for whole numbers, which covers the full range of JSON integers most APIs send, and f64 for decimals. If your API sends small integers that you would rather model as i32 or u32, change them after generation. The tool chooses the safe, wide type rather than guessing a narrow one.

Null values become Option of serde_json Value. That is the honest translation: the sample says nothing about what the field holds when it is present, so the tool uses the most general JSON value type wrapped in Option. Replace it with the real type once you know it, for example Option of String or Option of i64. The same honesty applies to empty arrays, which become Vec of serde_json Value. Fill in the real element type when you know what the array carries.

One practical note: using serde_json Value means your project needs serde_json as a dependency, alongside serde with the derive feature. If you replace every Value with a concrete type, you can drop that dependency for those structs. Either way, the generated code tells you what it needs, and the compiler will tell you quickly if something is missing.

Nested Structs and Naming

Nested objects become their own structs. An address object becomes an Address struct. An array of orders becomes a Vec of Order, with the name singularized from the field name. If two fields would produce the same struct name, the tool numbers the second one, so the output always compiles. Rename numbered structs to meaningful names once you see what they hold.

Keep the struct names aligned with your domain. A root struct called Root is fine for a first test, but ApiResponse or UserProfile reads better in real code. Set the root name before you convert, and rename nested structs right after. Good names are the cheapest documentation you can add to a model.

A Workflow That Catches Real Problems

Convert the fullest response you have, not a trimmed example. A trimmed example hides the nested shapes and the nullable fields that cause real deserialization errors. Then run the generated structs against a second response from the same API. If a field is missing in one response and present in another, make it an Option. If a number arrives as a string in one response, change the type to String and parse it yourself.

Pay attention to enums in disguise. A field like status that only ever holds a few strings is a good candidate for a Rust enum with serde, but the tool generates a String because it cannot know the full set of values from one sample. That is the right default: a String always parses, while an enum fails on an unexpected value. Upgrade to an enum deliberately, once you have the full list of possible values from the API documentation.

Privacy

All conversion happens in your browser. Your JSON never leaves your device, is never stored, and is never sent to a server. That makes the tool suitable for responses containing real user data, though you should still keep secrets like API keys out of pasted text as a general habit. No account is needed, and the tool is free.

Related tools

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top