DevToolCase
Sign in Sign in

Convert JSON to TypeScript types

Turn a JSON sample, such as an API response, into TypeScript interfaces or types: nested objects named, optional keys and null unions inferred.

Share this result

Encrypted in your browser. The key stays in the link, after “#”: only the people you give it to can read it, not even DevToolCase.

The link works for

Sign in for longer links or links without expiry, and to find them in your account.

Types are inferred from this sample only: dates stay strings, and a key absent from every item cannot be guessed.

Runs entirely in your browser: nothing you type is sent or stored.

Why generate TypeScript types from JSON?

An API answers with JSON, and your code reads it as any, so a typo in a property name only shows up in production. Paste a real response here and you get the interfaces that describe it, ready to copy into your project: autocompletion, and compile errors as soon as you use a property that does not exist.

In Angular, pass the root type to HttpClient and the response is typed in the rest of the code.

getUser(id: number): Observable<Root> {
  return this.http.get<Root>(`/api/users/${id}`);
}

This type is a promise, not a check: TypeScript trusts it, nothing verifies the actual response at run time.

How the types are inferred

  • Each nested object gets its own interface, named after its key in PascalCase: home_address gives HomeAddress.
  • The items of an array are named in the singular (items gives Item) and merged: a key missing from some items becomes optional, coupon?: string.
  • A value that is sometimes null gives a union: string | null. An array of mixed values gives (string | number)[].
  • An empty array gives unknown[], an empty object Record<string, unknown>, as there is nothing to infer from.
  • Two objects with exactly the same keys and types share one interface; different objects under the same name are numbered (Item, Item2).
  • Keys that are not valid identifiers, such as first-name, are quoted.

interface or type?

For the shape of an object, both work the same way. interface is the usual choice for API data: it can be extended (extends) and its errors are easier to read. type is needed to name a union or an array, which is why a JSON document that is an array or a single value always gets a type at the top.

readonly prevents the code from changing a property by mistake, handy for data received from a server or stored in a signal.

Limitations of a type inferred from a sample

  • Dates stay string since JSON has no date type: convert them yourself where you need a Date.
  • The types only cover what the sample shows: a key absent from every item, or a value that can also be null but is not in this response, cannot be guessed. Paste a response with several varied items for a better result.
  • Numbers are all number, and a string that can only take a few values (a status, a currency) is not turned into a union of literals: narrow it by hand, 'paid' | 'pending'.

Is your JSON hard to read? Format it first; to pick a few values out of it, try the JSONPath tester.

Add this browser’s favorites to your account?

This browser has 0 favorites that your account does not have:

Either way, this browser keeps its favorites: you find them again after signing out.