JSON to TypeScript Interface Generator

Paste JSON, such as an API response, and get TypeScript interfaces or type aliases for it as you type. Nested objects and arrays of objects get their own types, keys missing from some array items become optional, and property names that are not valid identifiers are quoted. The conversion runs in your browser.

How to use

  1. Paste a JSON object or array into JSON input, or click Load sample.
  2. Set the Root type name; it is converted to PascalCase.
  3. Optionally tick Use type aliases instead of interfaces or Mark every property as readonly.
  4. Review the generated TypeScript and the type and line count, then click Copy.

Example

Input

Root type name: Users
[
  { "id": 1, "name": "Ada", "email": "ada@example.com" },
  { "id": 2, "name": "Alan", "email": null, "is-admin": true }
]

Output

interface User {
  id: number;
  name: string;
  email: string | null;
  "is-admin"?: boolean;
}

type Users = User[];

Frequently asked questions

How are arrays of objects handled?

The keys of all objects in the array are merged into one type named after the singular form of the property (users → User). A key that is missing from some items becomes optional with ?, and different value types for the same key are combined into a union such as string | null.

What type do null values and empty arrays get?

A null value becomes the type null, combined with other types seen for the same key. An empty array becomes unknown[], because its element type cannot be inferred; add at least one example element to get a precise type.

What happens to keys like is-admin or class?

Keys that are not valid TypeScript identifiers, or that are reserved words, are written in quotes, for example "is-admin"?: boolean.

Are integers and decimals told apart?

No. All JSON numbers become number, and true or false becomes boolean. If the root of the JSON is an array or a single value, the tool outputs a type alias with the root type name.

Related tools