Developer JSON to TypeScript Interface Generator

JSON to TypeScript Interface Generator

Paste JSON to generate matching TypeScript types, including nested objects and arrays.

Output Settings

Declaration kind
Field requirement
Nested objects

How to use this tool

Paste a real response or config file, not one you typed from memory, since the types can only be as good as the sample. Set the root type's name, then choose interface or type, whether every field should be optional, and whether nested objects become separate named types or are written inline.

Worked example: an array of orders

{
  "orders": [
    { "id": 1, "total": 499, "coupon": "DIWALI10" },
    { "id": 2, "total": 1299 },
    { "id": 3, "total": 899, "note": null }
  ],
  "addresses": [{ "city": "Pune" }],
  "meta": {}
}
interface Root {
  orders: Order[];
  addresses: Address[];
  meta: Meta;
}

interface Meta {
  [key: string]: unknown;
}

interface Address {
  city: string;
}

interface Order {
  id: number;
  total: number;
  coupon?: string;
  note?: null;
}

The three orders are merged into one Order type. coupon and note appear in only some of them, so they are marked optional. Array fields are named after one element: orders gives Order, addresses gives Address. The empty meta object says nothing about its keys, so it accepts any key with an unknown value.

What to fix by hand afterwards

  • Fields that were null in the sample get the type null, like note above. Replace it with the real type, such as string | null.
  • Fixed sets of values come out as string. If status can only be "paid", "pending" or "refunded", write that union yourself.
  • Dates in JSON are strings, so they stay string until your code turns them into Date objects.
  • Empty arrays become unknown[], because there is no element to learn from.

Frequently Asked Questions

Should I use "interface" or "type"?

For most object shapes, they're functionally interchangeable. interface is generally preferred by convention for object shapes that might be extended or implemented (matching common TypeScript style guides), while type is required for unions, intersections, and other non-object type constructs. This tool lets you generate either, since teams differ on which they standardize on.

What happens to an array with mixed values, like [1, "n/a", 2]?

Every element is checked, and the type lists each kind that appears: (number | string)[]. Objects in the same array are merged into a single type rather than one type per element, and arrays of arrays become types such as number[][].

Why are some keys in quotes?

A key that isn't a valid JavaScript identifier, such as "order-id" or "2fa_enabled", has to be quoted in a TypeScript type. You then read it with brackets, order["order-id"], rather than a dot.

Related Tools

Related Blog Posts