How the JSON to TypeScript Interface works

The generator parses your JSON and records the kinds of values found at every position. All objects at the same position, such as every item of an array, are merged into one interface. Nested objects get their own interface named after the property in PascalCase, and array items use the singular form, so orders produces an Order interface.

Strings map to string, numbers to number, booleans to boolean and arrays to T[]. A property that is missing from some of the merged objects is marked optional with ?, and a property that is null in any sample gets | null. Arrays with mixed item types become a union such as (string | number)[], and an empty array becomes unknown[]. Keys that are not valid identifiers, such as first-name, are written in quotes.

You can choose between interface declarations and type aliases, set the root name, and leave out the export keyword. If the top-level JSON is an array of objects, the root interface describes one item; for other arrays and plain values a type alias such as type Root = number[] is generated.

How to use the JSON to TypeScript Interface

  1. Paste sample JSON into the input box, open a .json file, or select Load example.
  2. Set the root type name, choose interface or type alias, and tick or untick Add export keyword.
  3. Select Generate TypeScript, or press Ctrl + Enter (Cmd + Enter on a Mac).
  4. Copy the declarations into your project or download them as a .ts file.

Example

This input:

{"id": 90071992547409931, "user_name": "ann", "createdAt": "2024-05-01T10:30:00Z",
 "items": [{"sku": "KB-1", "qty": 1}, {"sku": "MS-2", "qty": null, "gift": true}]}

produces, with the default options:

export interface Root {
  id: number;
  user_name: string;
  createdAt: string;
  items: Item[];
}

export interface Item {
  sku: string;
  qty: number | null;
  gift?: boolean;
}

Common use cases

  • Typing the response of a REST API in an Angular, React or Vue application.
  • Creating types for JSON configuration files or static data imported into a TypeScript project.
  • Documenting the shape of webhook or message payloads for a Node.js service.
  • Replacing any in existing code with accurate types based on real data.

Frequently asked questions

When is a property marked optional?

When the tool merges several objects at the same position, for example the items of an array, a property that is missing from at least one of them gets a ? after its name.

How are null values handled?

A property that is null in any sample gets | null added to its type, for example number | null. A property that is only ever null is typed as null, because the sample does not show its real type.

How are arrays typed?

Arrays become T[]. When items have different types, the item type is a union such as (string | number)[]; objects in the array are merged into one interface named after the singular form of the property. An empty array becomes unknown[].

Should I use interface or type?

Both describe the same object shapes. Interfaces can be extended and merged, which many style guides prefer for object types, while type aliases are required for unions. Pick whichever your project uses; the members generated are identical.

Are large numbers handled differently?

No. Every JSON number is typed as number, which is what JSON.parse returns. Be aware that JavaScript numbers lose precision for integers larger than 9,007,199,254,740,991.

Is my JSON uploaded to your server?

No. The types are generated in your browser, and the JSON you paste or open is not sent to our server.