Skip to content
XUtilHub

JSON to Rust Struct Generator (Serde)

Convert JSON to Rust structs with serde Serialize/Deserialize derives, snake_case fields, #[serde(rename)] attributes and Option<T> for nullable and optional values.

  • Free, no sign-up
  • Runs in your browser, nothing uploaded
Input
Code

About the JSON to Rust

Generate Rust structs that work with serde and serde_json. Fields are snake_case with rename attributes where needed, Rust keywords are avoided, and nullable or optional values become Option<T>.

Key features

  • Structs with #[derive(Serialize, Deserialize)] for serde
  • snake_case fields with #[serde(rename = "…")] where needed
  • Option<T> for nullable and optional values
  • Rust keywords avoided (type → type_)
  • serde_json::Value for values of unknown type
  • Optional PartialEq derive

Rust structs for serde

Rust's serde is the standard way to work with JSON, but writing structs and rename attributes for every field gets repetitive. Paste JSON and get serde-ready structs with idiomatic snake_case fields.

Example

{ "userId": 7, "type": "admin", "nickname": null, "tags": ["a"] }
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Root {
    #[serde(rename = "userId")]
    pub user_id: i64,
    #[serde(rename = "type")]
    pub type_: String,
    pub nickname: serde_json::Value,
    pub tags: Vec<String>,
}
let root: Root = serde_json::from_str(&json)?;

Type mapping

JSONRust
"text"String
42i64
3.14f64
truebool
[...]Vec<T>
{...}struct
null alongside values / missingOption<T>
unknownserde_json::Value

Tips

  • Borrowed strings: for zero-copy parsing, change String to &'a str and add lifetimes.
  • Unknown keys: add #[serde(deny_unknown_fields)] for strict parsing.
  • Enums: replace string fields with #[derive(Deserialize)] enum types for fixed sets of values.

Generating for Go instead? Try JSON to Go. Need a C++ header? See JSON to C++.

How to use the JSON to Rust

  1. Paste your data into the input box, upload a file, or click Sample to try it out.
  2. Adjust the options above the editor if you need to (indentation, delimiter, and so on).
  3. The Rust appears instantly in the output panel. Any errors are shown with their line and column.
  4. Click Copy to copy the result, or Download to save it as a file.

Frequently asked questions

Which crates do I need?

Add serde with the derive feature and serde_json: cargo add serde --features derive and cargo add serde_json.

How are missing fields handled?

Optional fields are Option<T>. serde deserializes a missing Option field as None, and skip_serializing_if leaves it out when serializing.

Why i64 and f64?

They're the safe defaults that can hold any JSON integer or number. Narrow them (u32, i32, f32) when you know the range.

Can I use rename_all instead of per-field renames?

Yes. If every key follows one convention (e.g. camelCase), you can replace the individual renames with #[serde(rename_all = "camelCase")] on the struct.

Is my JSON uploaded to a server?

No. All JSON tools run entirely in your browser, so your data never leaves your device.

Is there a size limit?

There is no fixed limit. Files of several megabytes work fine; very large files depend on your device's memory.

Do I need an account?

No. Every JSON tool is free and works without signing up.

Last updated:

Related JSON tools