JSON to TypeScript

Generate TypeScript interfaces or type aliases from a JSON sample. Array items are merged into one shape, and keys missing from some items become optional.

Use the tool

Why this tool exists

When you start using a new API, the quickest way to understand it is to look at a real response, but typing that response out as interfaces is dull and error-prone. Doing it by hand also means you have to notice which fields appear only sometimes.

Paste a sample and the tool infers a type for each value, names nested objects from their keys, and merges all items in an array into one type. If one order has a gift field and another does not, gift is marked optional. You can choose interface or type, and set the root name. The result is a starting point from a single sample, so review it against your API documentation. Nothing is uploaded; inference runs locally.

How to use it

  1. Paste a representative JSON sample into the input box.
  2. Enter a root name such as User or ApiResponse.
  3. Choose interface or type, and keep Mark missing keys optional ticked if you want that.
  4. Copy the generated declarations into your project.

Frequently asked questions

How does it decide a field is optional?

When you merge array items and a key is present in some but not all, it is marked optional with a question mark. A single object has no such information, so all its keys are required.

What happens with null values?

A null in the sample becomes the type null. If the same field is a string elsewhere in an array, it becomes string | null. Check these against what your API really allows.

Is one sample enough?

It is enough for a first draft, but types come from only what the sample shows. Rare fields, other number formats and extra variants will be missed, so use several items or the API schema when possible.

How are awkward key names handled?

Keys that are not valid identifiers, such as my-key, are quoted in the output. Nested object type names are built from the key in PascalCase and made unique.