TTKTheTextKit

JSON to TypeScript Converter

Paste a JSON object or array and get TypeScript interfaces or type aliases for it, ready to copy or download as a .ts file. Nested objects get their own named types, arrays of objects are merged into one type, and keys that are missing from some items become optional. You can choose interface or type, export, readonly and the name of the root type. Everything runs in your browser, so your data is not uploaded.

TypeScript
export interface Root {
  id: number;
  name: string;
  email: string;
  active: boolean;
  roles: string[];
  address: Address;
  orders: Order[];
}

export interface Address {
  city: string;
  zip: null;
}

export interface Order {
  id: string;
  total: number;
  paid?: boolean;
  coupon?: string;
}

Related Tools

[4 suggestions]

About the JSON to TypeScript Converter

The JSON to TypeScript converter reads your sample and works out a type for every value. Strings, numbers, booleans and null map to string, number, boolean and null. An object becomes an interface named after its key, so "address" becomes Address, and the items of an array named "orders" become Order. When an array holds several objects, they are merged into one type: a key that appears in some of them but not all is marked optional with a question mark, and a key whose value has different types becomes a union such as string | number | null. An empty array becomes unknown[], because a sample with no items says nothing about them, and an empty object becomes Record<string, never>. Keys that are not valid names, such as first-name or 1abc, are written in quotes. Type names that would hide a built-in type, such as Date or Record, get a Type suffix, and two different objects that would get the same name are numbered. A root that is an array or a plain value becomes a type alias, because an interface cannot be an array. The types describe your sample, not all possible data, so check optional keys and unions against the real API. The output was tested with the TypeScript compiler in strict mode: the sample from 2,000 random JSON documents was assigned to the generated type and all compiled, and 3,278 copies changed to be wrong, by adding a key or replacing a value with an object, were all rejected. Everything runs in your browser and nothing is uploaded.

How do I convert JSON to a TypeScript interface?▾

Paste the JSON into the tool and copy the result. Each object becomes an interface, with a property for every key and the type of its value. For {"id": 1, "name": "Ann"} you get an interface with id: number and name: string.

How does the converter decide that a property is optional?▾

When the sample has several objects in an array, a key that is missing from at least one of them is marked optional with a question mark. A key that is present everywhere stays required. With a single object, every key is required, so paste several items to see which keys are optional.

What should I use, interface or type?▾

For plain object shapes the two are almost the same. Interfaces can be extended and merged, and type aliases can also name unions and arrays. Choose the one your project already uses. When the root of the JSON is an array or a plain value, the tool always writes a type alias.

Why is an empty array typed as unknown[]?▾

An empty array has no items to learn from, so the tool cannot tell what it will hold. unknown[] is the safe answer: you have to check each item before you use it. Replace it with the real type when you know it, or paste a sample that has items.

Is the generated type a guarantee for the whole API?▾

No. It describes the sample you pasted. A field that was always a number in your sample may be null or a string in other responses. Use a sample with the variations you expect, and check the result against the documentation of the API.

Is my JSON uploaded?▾

No. The conversion runs in your browser, and what you paste is not sent anywhere or stored.