thoughtasylumTOOLBOX Preview Guides Use offline Automate Feedback

Tools › Data

JSON to Types

Turn JSON, YAML, TOML or SQL CREATE TABLE into TypeScript, Go, Rust, Python, Zod, Pydantic, Protobuf, GraphQL and more, 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, Kotlin data classes, C# classes, PHP classes, Zod schemas, Pydantic models, Protobuf messages or GraphQL types, 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. To Zod writes z.object schemas with the nested ones first and a z.infer type for each; Pydantic writes v2 BaseModel classes, using Field(alias=…) when a key isn’t a Python name; Protobuf writes proto3 messages with repeated for arrays, optional for nullable or missing scalars and google.protobuf.Value for anything unknown; GraphQL writes type definitions with ! on required fields and a JSON scalar for anything unknown. 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, JsonElement, object, mixed, z.unknown(), JSON; 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, FirstName or first_name), with the original name kept for decoding in Go, Rust, Swift, Java, Kotlin, C#, Pydantic and Protobuf; Python TypedDicts keep 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, Java, Kotlin and C# output expects serde, Jackson, kotlinx.serialization and System.Text.Json. GraphQL Int is 32-bit, so a large whole number may need Float or a custom scalar; Protobuf field numbers are just 1, 2, 3… in the order of the sample, so set your own before using them on the wire.

Guide: Why won't my JSON file load?

Built with The third-party code and data this tool uses, and their licences

This tool runs on the following open-source work, copied onto this site and served from it:

js-yaml 5.4.2
Copyright
Copyright (C) 2011-2015 by Vitaly Puzrin
Licence
MIT License
Source
github.com/nodeca/js-yaml
smol-toml 1.9.0
Copyright
Copyright (c) Squirrel Chat et al.
Licence
BSD 3-Clause License
Source
github.com/squirrelchat/smol-toml

Every page also uses Montserrat (SIL Open Font License 1.1).
Everything the site uses is on Credits and licences.

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