JSON to TypeScript Generator
Generate TypeScript interfaces from any JSON. Handles nested objects, arrays, union types, and optional properties. Free, runs in your browser.
- Runs in your browser
- Tool input is not uploaded to ZeroTool servers
- Free · No Sign-Up
Scan with WeChat to share this tool
Examples, details and FAQ Worked examples, how it compares with other tools, and answers to common questions.
Type Inference Rules
nullon an object property → optionalnull(field?: null); in an array of objects, a key that isnullin every item stays required (field: null)- string →
string - number →
number - boolean →
boolean - empty array →
unknown[] - array of objects → merged interface +
ItemName[] - array of mixed primitives → union, e.g.
(string | number)[] - nested object → separate named interface
Example
Given this JSON:
{"user": {"name": "Alice", "age": 30}, "tags": ["admin", "user"]}
The tool generates:
interface RootObject {
user: User;
tags: string[];
}
interface User {
name: string;
age: number;
}
Example: An API Response
{
"user": {
"id": 42,
"name": "Alice",
"email": "alice@example.com",
"roles": ["admin", "editor"],
"preferences": { "theme": "dark", "notifications": true },
"lastLogin": null
},
"pagination": { "page": 1, "perPage": 20, "total": 143 }
}
generates
interface RootObject {
user: User;
pagination: Pagination;
}
interface User {
id: number;
name: string;
email: string;
roles: string[];
preferences: Preferences;
lastLogin?: null;
}
interface Preferences {
theme: string;
notifications: boolean;
}
interface Pagination {
page: number;
perPage: number;
total: number;
}
The root interface comes first, then each nested interface in the order it is found. A null value is typed null and marked optional, because one sample cannot show what the field holds when it is set. After checking the API docs, change it to something like lastLogin: string | null.
Example: Arrays With Different Shapes
{
"items": [
{ "id": 1, "name": "a", "tags": [] },
{ "id": 2, "price": 9.5, "tags": ["x", 1] }
],
"messages": [
{ "type": "text", "content": "hi" },
{ "type": "image", "url": "/a.png" }
]
}
generates
interface RootObject {
items: ItemsItem[];
messages: MessagesItem[];
}
interface ItemsItem {
id: number;
name?: string;
tags: (unknown[] | (string | number)[]);
price?: number;
}
interface MessagesItem {
type: string;
content?: string;
url?: string;
}
All objects in an array are merged: a key that is missing from some elements gets ?. The messages array is really two shapes keyed by type. The merged interface accepts invalid mixes such as a text message with a url, so rewrite it as a discriminated union (TypeScript Handbook: Narrowing):
type Message =
| { type: "text"; content: string }
| { type: "image"; url: string };
Example: Two Objects Under the Same Key
{
"order": { "id": "A-1001", "meta": { "createdBy": "alice", "source": "web" } },
"payment": { "status": "paid", "meta": { "provider": "stripe", "fee": 0.3 } }
}
generates
interface RootObject {
order: Order;
payment: Payment;
}
interface Order {
id: string;
meta: Meta;
}
interface Meta {
createdBy: string;
source: string;
}
interface Payment {
status: string;
meta: PaymentMeta;
}
interface PaymentMeta {
provider: string;
fee: number;
}
order.meta takes the name Meta. payment.meta has different fields, so it gets its own interface, named with its parent in front: PaymentMeta. If it had the same fields as order.meta, both would share Meta. Merging the two into one Meta with every field optional would accept objects that neither API returns, so each shape keeps its own type. If the name is still taken, a number is added (Meta2). The root keeps the name you chose even when a nested key has the same name.
What to Refine by Hand
nullfields: replace?: nullwith the real type plus| null.- Empty arrays:
[]givesunknown[]; supply the element type. - Dates: ISO 8601 strings and Unix timestamps stay
stringandnumber. - Keys that are not valid identifiers (
user-name,2fa) are written in quotes, which is valid TypeScript. - Generated names such as
PaymentMetaorMeta2come from the JSON keys; rename them to match your domain. - A key that gives no valid type name (
2fa, an empty key) gets aTprefix for its interface (T2fa); the property itself keeps the original key in quotes.
Limits
- The input must be valid JSON (no comments or trailing commas). When it is not, the output is cleared and the status line gives the line, the column and the likely cause, counted in the input as you typed it:
{"a":1,}givesInvalid JSON: line 1, column 7, trailing comma before the closing bracket.A Python dict pasted as is,{'id': 7}, givesInvalid JSON: line 1, column 2, JSON strings and property names need double quotes ("). - A root array that mixes objects with other values, such as
[{"id": 1}, "x", null], gives the objects one interface named after the root and adds an alias for the whole array, written like a nested array:type RootObjectArray = (string | null | RootObject)[];. UseRootObjectArrayfor the response. - The root name keeps Unicode letters, digits,
$and_(the ECMAScript identifier rules). Other characters are removed and the next letter is capitalized (user profilegivesuserProfile), a name that cannot start an identifier gets aTprefix (2fagivesT2fa), and a reserved word or a primitive type name gets a_suffix (classgivesclass_). A name with nothing left uses RootObject. - A name that TypeScript's standard library (
lib.dom.d.tsand thelib.es*.d.tsfiles) already uses for a one-word global type, such asPromise,Date,Map,Record,Body,Response,Event,JSONorURL, is not used as is: in a file withoutimportorexportsuch a declaration merges with the global type, and in a module it hides the global type. A nested object gets its parent's name in front ({"date": {"year": 2026}}givesRootObjectDate, and a"URL"key givesRootObjectURL; a"url"key givesUrl, which is not a global type), and a root name gets_(MapgivesMap_). The root nameglobalThisgets_as well (globalThis_): TypeScript rejects a declaration with that name in a file withoutimportorexport. Names that exist only as global values, such asImage,IntlorProxy, are kept, because an interface or a type alias of the same name does not conflict with them. - Global types whose names have more than one word, such as
FormDatafrom aformDatakey, keep the generated name; rename the interface if you paste it into a file withoutimportorexport, or into a module that also uses that global type. - Very deep nesting can exceed the browser's call stack. The status line then shows the error and the previous output is cleared.
- Integers larger than 9007199254740991 (
Number.MAX_SAFE_INTEGER) are read byJSON.parseand lose digits, and the type isnumber. If an API sends such IDs, ask for them as strings and type them asstring. - Types come only from the sample you paste. A field that is a string in your sample but sometimes a number in production stays
string. - With Use type instead of interface, each declaration is written as
type Name = { ... }; the status line still says "interfaces".
FAQ
What does this tool generate?
It generates TypeScript interface (or type alias) declarations from a JSON object. Each nested object becomes its own named interface. Array items with mixed types produce union types.
How are nested objects handled?
Each nested object field is extracted into a separate interface named after the field key in PascalCase. For example, a field named 'address' becomes an 'Address' interface, and the parent interface references it by name. Only letters after a hyphen, space or other separator are capitalized, so 'user-info' gives 'UserInfo' but 'same_name' gives 'Same_name'. A name that is also a one-word global type in TypeScript, such as Date or Response, gets the parent's name in front (RootObjectDate).
What happens with arrays of objects?
If an array contains objects, all objects are merged to form a single item interface. Fields present in some but not all objects are marked as optional. A field that has different types in different items becomes a union, for example (null | string).
What is the 'Make properties optional' option?
When checked, every property in every generated interface gets a '?' suffix, making it optional. Useful when you expect partial data or want a loose type definition. Click Generate TypeScript after changing it.
What is the difference between interface and type?
Both declare object shapes in TypeScript. 'interface' supports declaration merging and is idiomatic for object types. 'type' is more flexible and can also represent primitives, unions, and tuples. Toggle 'Use type instead of interface' to switch.
Is my data sent to a server?
No. The page infers the types from your JSON in your browser and does not send the JSON anywhere or save it in browser storage. The page's analytics record only a usage event (the tool name and the action generate) after you edit the input and move the focus out of it, or click Generate TypeScript or Example, without any of the JSON or the generated code.