Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Free tool · Developer tools

JSON to TypeScript

Paste a JSON sample and copy the TypeScript interfaces for it. Nested objects get their own names, and keys that are missing from some items become optional.

  • Free
  • No sign-up
  • Runs in your browser

By Maaz Zindani · Updated

Write it as
Options
TypeScript

3 interfaces12 properties in 20 lines of TypeScript

export interface Root { id: number; customer: Customer; items: Item[]; paid: boolean; coupon: null; tags: string[]; } export interface Customer { name: string; email: string; } export interface Item { sku: string; qty: number; price: number; note?: string; }
  • Root is an object
  • 1 optional key (missing in some items)
  • Only null seen: coupon
Types
Root, Customer, Item
Properties
12
Optional
1
Lines
20

How to use the JSON to TypeScript

  1. Paste your JSON into Your JSON. The example is replaced as soon as you type.
  2. Type a name in Name of the top-level type, for example ApiResponse.
  3. Under Write it as choose interface or type, then set the Indent and tick or untick Add export.
  4. Press Copy or Download .ts. If the JSON has a mistake, the red message gives the line and column.

How do I turn JSON into TypeScript types?

Paste the JSON, and the types appear next to it. The tool reads every value in your sample and writes down what kind it is: text becomes string, numbers become number, true and false become boolean, and a list becomes the type of its items followed by []. A nested object gets an interface of its own, named after its key.

The example order has 3 types: Root, Customer, Item. The key customer holds an object, so it becomes customer: Customer;. The key items holds a list of objects, so it becomes items: Item[];. The name comes from the key with its first letter in capitals, and a plural key such as items is shortened to a single item. Names like shipping_address turn into ShippingAddress.

Copy the result into a .ts file and use it where you read the data. A sample is only a sample, so look the result over once. A key that never showed up in your JSON cannot appear in the types, and a value that was null in your sample may be something else in real data.

How does it handle arrays of objects and missing keys?

All objects in one list are folded into a single type. A key that appears in every object is required. A key that is missing from at least one object gets a question mark, which TypeScript reads as "may be left out". Take [{"id":1,"name":"Ann"},{"id":2,"name":"Bo","admin":true}]. Only the second object has admin, so the result has 1 optional key: admin?: boolean;

The same folding works one level down. If you have ten orders and each has a list of lines, every line from every order is merged into one Line type, so a key that only some lines have becomes optional there too. The more items you paste, the better the result. One item shows only the keys of that item.

If two places in the JSON hold objects with exactly the same keys and types, they share one interface. In {"home":{"city":"Lyon"},"work":{"city":"Pune"}} both home and work use home: Home; and the page shows a single Home interface. If two objects get the same name but different keys, the second gets a number: Root, A, Item, B, Item2.

What do null, empty arrays and mixed types become?

A value that is null in every place it appears becomes null, and the tool lists it in a chip so you remember to widen it. If a key is null in one item and text in another, the type is string | null. A list that holds more than one kind of value gets a union in brackets, such as price: (string | number | null)[];. An empty list has nothing to learn from, so it becomes unknown[].

The table shows what one key becomes for each kind of value. It was made by running this tool on each small piece of JSON.

What a value becomes in the types
The value isJSONTypeScript line
a number{"n":1}n: number;
text{"n":"a"}n: string;
true or false{"n":true}n: boolean;
null{"n":null}n: null;
a list of numbers{"n":[1,2]}n: number[];
a list with two kinds{"n":[1,"a"]}n: (string | number)[];
an empty list{"n":[]}n: unknown[];
a nested object{"n":{"k":1}}n: N;

Numbers are always number. JSON does not tell whole numbers from decimals, and TypeScript has no separate integer type. Dates arrive as text, so a date is a string until you convert it yourself.

Should I use an interface or a type alias?

Either works for an object shape. The TypeScript handbook says an interface can be extended and reopened, a type alias cannot be reopened, and a type alias can also name unions and primitive types. It suggests starting with interface and switching to type when you need what only a type gives you. Pick interface or type under Write it as. For {"user":{"id":7}} the type style gives type Root = { user: User; }; type User = { id: number; };

When the top level of your JSON is a list or a plain value, the result is always a type line, because an interface can only describe an object. A list of objects gives type Root = RootItem[]; followed by the item interface.

Will these types check my data when the program runs?

No. Types are a help while you write code. The TypeScript handbook explains that type annotations and assertions are removed by the compiler and change nothing at run time, and that a wrong assertion does not throw an error. If the data from a server differs from your sample, the code still runs, and the mistake shows up later as an odd value.

For data you do not control, check it at run time with a validation step before you trust the type. The types here are a starting point built from one sample, and you should edit them where you know more than the sample showed. Your JSON is read in the browser and is not uploaded. To tidy or check the JSON first, use the JSON formatter.

Use it with

Questions

How do I convert JSON to TypeScript?

Paste the JSON, set a name for the top-level type and copy the result. The example order becomes 3 types: Root, Customer, Item.

Why is a key marked optional?

It was missing from at least one object in a list. TypeScript marks such a key with a question mark, which means the key may be left out.

What type does null become?

A key that is only ever null becomes null, and the tool warns you in a chip. A key that is null in some items and text in others becomes string | null.

Why is my empty array unknown[]?

An empty list shows nothing about its items, so the tool cannot guess. Replace unknown with the real item type.

Does it use interface or type?

Your choice. Interface is the default. A top-level list or plain value always gets a type line, because an interface can only describe an object.

Is my JSON uploaded anywhere?

No. The JSON is read by code in your browser and is not sent or stored.

The tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations are checked against the tool's own output.