JSON to Rust

Generate Rust types from JSON automatically.

Local Your input is processed locally in your browser. Found a problem? Report an issue

What JSON to Rust does

JSON to Rust turns a sample JSON document into Rust code: one struct with Serialize/Deserialize derives per object, with a type for every field inferred from the value in the sample.

JSON to Rust examples

The built-in sample

Input

{"name":"Ada Lovelace","active":true,"tags":["math","code"],"address":{"city":"London"}}

Output

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
pub struct Address {
    pub city: String,
}

#[derive(Serialize, Deserialize)]
pub struct Root {
    pub name: String,
    pub active: bool,
    pub tags: Vec<String>,
    pub address: Address,
}

This is the input that the Load sample button fills in.

Awkward keys, a null, a decimal and arrays

Input

{"order-id":7,"total":19.5,"paid":false,"coupon":null,"items":[{"sku":"A1","qty":2},{"sku":"B2","qty":1,"gift":true}],"tags":[]}

Output

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
pub struct Items {
    pub sku: String,
    pub qty: i64,
    pub gift: bool,
}

#[derive(Serialize, Deserialize)]
pub struct Root {
    pub order_id: i64,
    pub total: f64,
    pub paid: bool,
    pub coupon: serde_json::Value,
    pub items: Vec<Items>,
    pub tags: Vec<serde_json::Value>,
}

The sample has a key with a hyphen (order-id), a null (coupon), a decimal (total), an array of objects whose second item has an extra field, and an empty array.

Each output was produced by running this tool on the input above it.

How it works

The sample is walked recursively and each value is mapped to a Rust type:

Sample valueRust type
StringString
Whole numberi64
Decimal numberf64
true / falsebool
null, or an empty array’s itemsserde_json::Value
Array of TVec<T>
Objecta new named type

Using the generated code

Add serde with the derive feature and serde_json to Cargo.toml, then:

let root: Root = serde_json::from_str(&text)?;

Details and limits

  • A key that is not a valid identifier is renamed with underscores. Add #[serde(rename = "order-id")] above the field so that it still matches the data.
  • Whole numbers are i64 and decimals f64.
  • A field that can be missing or null must be wrapped in Option<T> by hand; the generator makes every field required.
  • A key that is a Rust keyword, such as type, is emitted unchanged. Write it in the raw form r#type.
  • A type is named after the key that holds the object: items gives a type called Items, with no attempt to make it singular. The top-level type is always Root.
  • If two different objects sit under the same key name in different places, two types with the same name are generated. Rename one of them.
  • Objects in an array are merged into one type. A field that only some of them have is still generated as a required field.
  • Input that is not valid JSON stops the tool, and the status line shows where the problem is. The JSON Validator page lists the usual causes.

Questions

Is my data uploaded when I use JSON to Rust?

No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.

What does JSON to Rust do?

JSON to Rust turns a sample JSON document into Rust code: one struct with Serialize/Deserialize derives per object, with a type for every field inferred from the value in the sample.

Are there any limits I should know about?

A key that is not a valid identifier is renamed with underscores. Add #[serde(rename = "order-id")] above the field so that it still matches the data. Whole numbers are i64 and decimals f64.

Is there a size limit?

There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.

Does it work on phones, and is it free?

Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.