JSON to TypeScript

Convert JSON to TypeScript interfaces: a real output and how fields are typed.

loading JSON to TypeScript
About this tool

JSON to TypeScript: from JSON to code

Paste a JSON sample, usually an API response, and get the TypeScript interfaces that describe it. Each nested object becomes its own interface, arrays become T[] and a field that is missing in some items of an array becomes optional (name?:). The types exist only at compile time, so they document and check your code but do not validate the data that arrives.

This page opens the converter on TypeScript. Paste your JSON, choose the root name and the flavour, and copy the result; everything is generated in your browser and the JSON is never sent anywhere.

A real conversion

The tool opens with this JSON, an order with a customer and two items:

{
  "id": 48213,
  "status": "paid",
  "paid_at": "2026-11-21T14:03:00Z",
  "total": 389.3,
  "coupon": null,
  "customer": { "name": "Ana Souza", "email": "ana@example.com", "vip": true },
  "items": [
    { "sku": "KB-01", "qty": 2, "price": 149.9, "tags": ["usb", "keyboard"] },
    { "sku": "MS-07", "qty": 1, "price": 89.5 }
  ]
}

and this is what it writes for TypeScript:

export interface Root {
  id: number;
  status: string;
  paid_at: string;
  total: number;
  coupon: null;
  customer: Customer;
  items: Item[];
}

export interface Customer {
  name: string;
  email: string;
  vip: boolean;
}

export interface Item {
  sku: string;
  qty: number;
  price: number;
  tags?: string[];
}

How each JSON value is typed

In the JSONIn TypeScript
stringstring
integernumber
true / falseboolean
nullnull
array of itemsItem[]
nested objectinterface
field missing in some itemstags?: string[]

The second item of the sample has no "tags" key, and the first has one: that is why the tool treats it as optional.

Awkward keys, null and empty lists

{"user_id": 7, "display name": "Ana", "tags": [], "address": null, "scores": [9.5, 8], "meta": {"2fa": true}, "mixed": [1, "a"]}
export interface Account {
  user_id: number;
  "display name": string;
  tags: unknown[];
  address: null;
  scores: number[];
  meta: Meta;
  mixed: unknown[];
}

export interface Meta {
  "2fa": boolean;
}

A key with a space is quoted ("display name"), a key that starts with a digit is quoted too, an empty array has no items to learn from so it becomes unknown[], and null stays null. Keys keep the JSON spelling, so the interface matches what the API sends.

The other flavour

The same small JSON, {"id": 1, "paid_at": "2026-11-21", "coupon": null}, in the default flavour and in the "type" one:

export interface Order {
  id: number;
  paid_at: string;
  coupon: null;
}
export type Order = {
  id: number;
  paid_at: string;
  coupon: null;
};

The tool can write type aliases instead of interfaces. Interfaces can be extended and merged; aliases are the choice when you combine types with unions.

Using the interface when you read the JSON

const res = await fetch('/api/orders/48213');
const order = (await res.json()) as Root;
console.log(order.customer.name);

The cast tells the compiler what to expect; it does not check it. For data you do not control, validate at runtime with a schema library such as Zod or Valibot, or generate a JSON Schema from the same sample.

Common mistakes

  • Trusting one sample: a field that is a number in the sample may be null or a string in other responses. Paste the largest and the most unusual responses you have, or mark fields optional by hand.
  • Dates: JSON has no date type, so "paid_at" is a string. Convert it to Date where you use it; the type does not do it for you.

How to use the converter

  1. Paste your JSON on the left (or keep the example); a mistake is reported with its line and column.
  2. Set the root name and the flavour; the code updates as you type.
  3. Copy the code or download it as a file.

Frequently asked questions

How do I convert JSON to a TypeScript interface?

Paste the JSON in the box and copy the interfaces on the right; choose the root name, whether every field is optional and interface or type. It all runs in your browser.

Why is a number sometimes typed as number and never as int?

TypeScript has a single number type for integers and decimals, because JavaScript has one too. The distinction exists in Go, Java and the other typed languages of the tool.

Can I get a JSON Schema or a Zod schema instead?

The same tool writes JSON Schema from the sample; pick it in the language list. A Zod schema you can write from the interfaces, or validate with the JSON Schema.

Report a problem