Skip to Content

Code generator tool

JSON to TypeScript Interfaces

Paste a JSON object or array and get TypeScript interfaces for it: one per nested object, ? for keys that are sometimes missing, and | null for nulls. It runs in your browser, so your JSON never leaves your machine.

INPUT JSON100% local in browser
types.ts
// Paste JSON on the left (or click 'Load Sample') to generate TypeScript interfaces.

What the converter generates

Here's an order with a nested customer, a list of items and a coupon that's null:

order.json
{
  "orderId": 1042,
  "customer": { "name": "Ada", "country": "GB" },
  "items": [
    { "sku": "KB-01", "price": 49.5, "qty": 1 },
    { "sku": "MS-02", "price": 19, "qty": 2, "giftWrap": true }
  ],
  "couponCode": null
}

With the root name set to Order, the TypeScript tab produces:

types.ts
export interface Order {
  orderId: number;
  customer: Customer;
  items: Item[];
  couponCode: unknown | null;
}

export interface Item {
  sku: string;
  price: number;
  qty: number;
  giftWrap?: boolean;
}

export interface Customer {
  name: string;
  country: string;
}

How JSON types map to TypeScript

JSON valueTypeScript
stringstring
any numbernumber
true or falseboolean
nullT | null
a key some objects lackan optional property, key?: T
arrayT[]
objectits own interface
mixed types, or only nullunknown

If the top level of your JSON is an array, you also get a type alias. Name the root Orders and paste a list of orders to get export type Orders = Order[];.

? or | null: two different things

giftWrap?: boolean means the key may be missing, and reading it then gives undefined. couponCode: string | null means the key is always there but may hold null. JSON has null but no undefined, and APIs use both patterns, so the converter keeps them apart. Each needs its own check:

describe.ts
export function describe(order: Order): string {
  const wrapped = order.items.filter((item) => item.giftWrap).length;
  const coupon = order.couponCode !== null ? " with a coupon" : "";
  return `${order.items.length} item(s), ${wrapped} gift-wrapped${coupon}`;
}

Once you change couponCode to string | null, strict mode won't let you call string methods on it until you've ruled out null.

Typing a fetch response

Import the interfaces and tell TypeScript what res.json() returns:

api.ts
import type { Order } from "./types";

export async function loadOrder(id: number): Promise<Order> {
  const res = await fetch(`/api/orders/${id}`);
  if (!res.ok) throw new Error(`HTTP ${res.status}`);
  return (await res.json()) as Order;
}

as Order asks the compiler to trust you. It doesn't check anything at runtime, so if the API changes shape, you find out when something breaks. For data you don't control, check it first. A type guard checks the fields your code relies on:

guards.ts
export function isOrder(value: unknown): value is Order {
  if (typeof value !== "object" || value === null) return false;
  const v = value as Record<string, unknown>;
  return typeof v.orderId === "number" && Array.isArray(v.items);
}

To check every field, a schema library such as Zod validates the data and can infer the TypeScript type from the schema, so you only describe the shape once.

Interface or type alias?

The converter writes interface declarations for objects and uses type only when it has to, for a top-level array. For plain object shapes the two behave the same in everyday code. Interfaces can be extended with extends; type aliases can also describe unions such as "card" | "cash".

Learning TypeScript? Start with the TypeScript tutorial, then read TypeScript best practices for patterns that keep types like these accurate as your code grows.

Convert JSON to other languages

The converter above has a tab for each language. These pages explain the output for the other two: