About the JSON to TypeScript
Paste a JSON sample and get TypeScript interfaces back. The JSON to TypeScript converter reads an API response, a config file or a mock data object and writes an export interface for it, plus a separate interface for every nested object, so you can type your data without writing out each property by hand.
Set the name of the top-level interface in Root Interface Name, paste your JSON on the left, and the TS types appear on the right as you type, ready to copy into a .ts file. If the JSON has a syntax error, the output tells you the line and column where it went wrong instead.
How JSON values become TypeScript types
The JSON is parsed with the browser's standard JSON parser, then every property of the top-level object is given a type from its value. Strings, numbers and booleans become string, number and boolean. A nested object becomes its own interface, named after its key in PascalCase, so an "author" object becomes interface Author and a "shipping-address" object becomes ShippingAddress.
Every element of an array is read, not just the first. ["web", "tools"] becomes string[], a mix such as [1, "two"] becomes (string | number)[], and an array of objects becomes an array of one interface that holds every key found in any of the objects. A key that is missing from some of them is marked optional with a ?, and a key whose value is a string in one item and null in another is typed string | null. Arrays inside arrays nest the same way, giving number[][] for a grid of numbers.
A value that is only ever null, or an empty array, cannot show what type it would normally hold, so it is typed any or any[]. Keys that aren't valid TypeScript names, such as user-id or first name, are written in quotes, so the output compiles as it is.
The interfaces are listed with the root interface first and the nested ones after it, in the order they appear in the JSON, each one exported so you can import any of them from the file you paste them into.
Assumptions and limits
- A property is only marked optional when the sample shows it missing, which means inside an array of objects. A single object can't show which fields are sometimes left out, so compare a few responses and add a ? by hand where needed.
- Types come from the values in your sample. A field that holds a number in the sample but a string in other responses will be typed number, so paste a sample that covers the cases you care about.
- A top-level array becomes a type alias, such as export type User = UserItem[], with one interface for its elements named after the root name plus Item. A top-level string or number becomes a plain type alias.
- Nested interfaces are named from their key, so a list called items gives interface Items, not Item. When two different objects share a key name, the second gets a number, such as Address2; two objects with exactly the same fields share one interface.
- The root name you type is used as it is when it is a valid TypeScript name; otherwise it is turned into one, so my type becomes MyType.
Worked examples
The sample blog post object
The JSON already in the box when the page opens has an author object and a tags array. It becomes two interfaces: the root and Author.
export interface RootObject {
id: number;
title: string;
published: boolean;
author: Author;
tags: string[];
}
export interface Author {
name: string;
email: string;
}An e-commerce order with nested objects and an array of objects
With Root Interface Name set to Order, this response gives four interfaces. Only the second item has a gift field, so it becomes optional. coupon is null in the sample, so it is typed any; replace it with string | null if you know it is a code when present.
Input:
{"orderId":"A-1042","total":59.9,"coupon":null,
"items":[{"sku":"TS-01","qty":2},{"sku":"MG-02","qty":1,"gift":true}],
"shipping":{"address":{"city":"Lahore","zip":"54000"}}}
Output:
export interface Order {
orderId: string;
total: number;
coupon: any;
items: Items[];
shipping: Shipping;
}
export interface Items {
sku: string;
qty: number;
gift?: boolean;
}
export interface Shipping {
address: Address;
}
export interface Address {
city: string;
zip: string;
}A top-level array of users
With Root Interface Name set to User, an array where one name is null gives a type alias for the array and one interface for its items.
Input:
[{"id":1,"name":"Ayesha"},{"id":2,"name":null}]
Output:
export type User = UserItem[];
export interface UserItem {
id: number;
name: string | null;
}JSON values and the TypeScript types they produce
| JSON value | Example | TypeScript type |
|---|---|---|
| Text | "Lahore" | string |
| Whole or decimal number | 101, 59.9 | number |
| true or false | true | boolean |
| null on its own | null | any |
| null in some items, a value in others | "Alex" and null | string | null |
| Object | {"name": "Alex"} | a new interface named after the key |
| Array of text | ["web", "tools"] | string[] |
| Mixed array | [1, "two"] | (string | number)[] |
| Array of objects | [{"sku": "TS-01"}] | KeyName[] with a new interface |
| Array of arrays | [[1, 2], [3, 4]] | number[][] |
| Empty array | [] | any[] |
How to use the JSON to TypeScript
- Name the root interface. Type a name such as User or ApiResponse in Root Interface Name; RootObject is used if you leave it as is.
- Paste your JSON. Replace the sample in the left box with a real response copied from your browser's network tab, Postman or a .json file.
- Copy the interfaces. Check the TypeScript Output, press Copy Code, and paste it into a types.ts file, adding ? where a field is sometimes missing from a single object.
Frequently asked questions
How do I generate a TypeScript interface from a JSON response?
Copy the response body from your browser's developer tools or API client, paste it into the JSON box, and give the root interface a name. The interfaces for the whole structure, including nested objects, are written on the right for you to copy.
Should I use interface or type for JSON data in TypeScript?
Either works. Both describe the shape of an object equally well. This tool writes interfaces, which can be extended and merged later. If your project prefers type aliases, change export interface Author { to export type Author = { and the rest stays the same.
Why is a field typed as any?
Because its value in the sample was null or an empty array, so there was nothing to tell what it normally holds. Replace any with the real type, for example string | null for an optional text field or Tag[] for a list that was empty in the sample.
Does it make optional properties?
Yes, inside arrays: when some objects in an array have a key and others don't, that key gets a question mark, such as gift?: boolean. A single object can't show which fields are sometimes missing, so its properties are all required; add a ? by hand to any that don't always appear.
Why do I see an Invalid JSON message?
Usually because the input isn't strict JSON, which needs keys and strings in double quotes, no trailing commas and no comments. A JavaScript object literal copied from code often breaks these rules. Fix the spot the message points to, or run the text through a JSON formatter first.
Is my JSON sent anywhere?
No. The JSON is parsed and the interfaces are generated in your browser, so API responses containing customer data or internal fields stay on your device.
This tool runs entirely in your browser. What you enter or upload stays on your device and is never sent to our servers.