Overview / Developer & Code / JSON to TypeScript
Developer & Code 100% In-Browser & Offline

JSON to TypeScript

Paste a JSON sample and get TypeScript interfaces or types: nested objects get names, missing keys become optional, mixed values become unions.

Paste or type your input; results appear instantly and errors are shown below the input.

Your files and input never leave your browser – nothing is uploaded.

JSON

TypeScript

Types are guessed from the values you paste. A key missing from some array items becomes optional, mixed values become a union, and an empty array becomes unknown[]. Check the result against your real data before relying on it.

Turn a sample response into types

Most API work starts the same way: you have a JSON response and need a type for it. Paste the response on the left and the right side shows the matching TypeScript, ready for a .ts file. Nothing has to be typed by hand, and a payload with ten levels of nesting takes as long as one with two.

From payload to declaration file

  1. Paste your JSON into the left box, or press Sample to see how it works.
  2. Set the name of the main type, for example User or ApiResponse.
  3. Choose between interface and type, and tick export or read-only if you want them.
  4. Check the result on the right. A syntax error is reported in place of the types.
  5. Use Copy or Download .ts to take the code.

How the types are worked out

  • Strings, numbers, booleans and null map to the matching TypeScript type.
  • A nested object becomes its own interface, named after its key in PascalCase. The key shipping_address gives ShippingAddress.
  • For arrays, all elements are merged. A key that appears in only some of them is marked optional with a question mark, and values of different kinds become a union such as string | number.
  • Two objects with exactly the same shape share one interface instead of producing two copies.
  • An empty array gives unknown[], an empty object gives Record<string, unknown>.

Limits of guessing from one sample

A sample can only show what it contains. A field that was always filled in your example may be null in production, and a number that looks like an id may in fact be a string elsewhere. Dates arrive as strings, so they are typed as string. Treat the output as a first draft and tighten it by hand, or validate real responses at runtime, if the data comes from outside your control.

Your payload stays private

The conversion runs in the page. Production responses, tokens and personal data in a pasted sample are not sent to a server, which makes it fine to use with real data.

Frequently asked questions (FAQ)

Paste the JSON into the left box. The interface appears on the right at once. Name the main type, copy the code and put it into a .ts file.