thoughtasylumTOOLBOX Preview

Tools › Data

JSON to Types

Turn a sample of JSON, YAML or TOML, or SQL CREATE TABLE statements, into types for TypeScript, Python, Go, Rust, Swift, Java or PHP, or a JSON Schema.

About this tool What it's for, how to use it and an example

What it's for

Write the type declarations for data you already have. Paste a sample of what an API returns, a config file or a database table’s CREATE TABLE statement, and get the matching TypeScript interfaces, Python TypedDicts, Go structs, Rust structs for serde, Swift Codable structs, Java records or PHP classes, with a named type for each nested object, or a JSON Schema to validate data like it.

For example, when you start calling a new API, paste one of its responses and copy the types into your code instead of writing them out field by field.

How to use it

Paste the sample and choose its format under From and the language under To; the types are written as you type. Root names the outer type; nested objects are named after their keys (an array called orders holds Order). Paste an array of objects and they are combined: a field missing from some of them is marked optional, a field that is sometimes null is nullable, and whole and decimal numbers together make a decimal. With SQL, each table becomes a type named after it, and columns without NOT NULL or PRIMARY KEY are nullable. To JSON Schema writes a draft 2020-12 schema with nested objects in place: fields missing from some samples are left out of required, and nullable ones allow null; with SQL, each table is a definition under $defs. The formats and the root name are remembered in the browser.

Example

Paste this with From JSON and To TypeScript:

[{"id":1,"name":"Alice","tags":["admin"]},{"id":2,"name":"Bob","tags":[],"email":null}]

You get:

export interface Root {
  id: number;
  name: string;
  tags: string[];
  email?: null;
}

Choose To Go instead, and email becomes Email any with the tag json:"email,omitempty".

Good to know

The types come from one sample, so they only know what’s in it: an empty array or a field that is always null can’t tell what it would hold, and gets the language’s “anything” type (unknown, Any, any, serde_json::Value, Object, mixed; Swift has none, so it uses an optional String and adds a comment). Strings are never guessed to be dates or UUIDs. Keys that aren’t valid names in the language are renamed (first-name becomes firstName or first_name), with the original name kept for decoding in Go, Rust, Swift and Java; Python keeps the keys as they are, and PHP properties are renamed without a mapping. Java records and PHP classes each need their own file. The Rust and Java output expects serde and Jackson.

Private: this tool runs in your browser. Nothing you type, paste or choose leaves this page.

Saved you a few minutes? Say thanks with a coffee.

Something wrong with this tool, or missing from it? Report a bug or suggest a feature.

↑ ↓ move↵ openesc close