JSON to TypeScript Interface
Generate TypeScript interfaces from a JSON sample — with nested types.
Was this tool helpful?
Thanks for the feedback!
Paste a sample JSON object, or an array of objects, and this generator writes matching TypeScript interfaces, creating a separate interface for every nested object. It is meant for front-end and Node.js developers who want typed API responses or config objects without writing each property by hand.
Example
With the Root interface name left as Root, this input:
{"id": 1, "user": {"name": "Asef", "tags": ["a", "b"]}}
produces:
export interface Root {
id: number;
user: User;
}
export interface User {
name: string;
tags: string[];
}
The root interface comes first and nested interfaces follow. Every interface is exported, so you can paste the block into a types.ts file and import from it.
How types are inferred
| JSON value | TypeScript type |
|---|---|
| "text" | string |
| 42 or 3.14 | number |
| true / false | boolean |
| null | any |
| [] (empty array) | any[] |
| ["a", "b"] | string[] (from the first element) |
| [[1, 2], [3]] | number[][] |
| { ... } | a new interface named after the key |
Interface names come from the property name with its first letter capitalised and symbols removed, so user becomes User and items becomes Items. Property names that are not valid identifiers are quoted, for example "order-id": string;. The Root interface name field is cleaned the same way, so "Order Response" becomes OrderResponse. If you paste a top-level array, the root interface describes one element, and you would type the whole response as Root[].
What to review after generating
The generator sees one sample, not your API's documentation, so a quick review makes the types trustworthy:
- Replace any. A null in the sample becomes
any, which switches off type checking. If the field is a nullable string, change it tostring | null. - Mark optional fields. Every property is required. If a field is sometimes missing, add
?, as incoupon?: string;. - Check arrays of objects. Only the first element is inspected, so fields that appear only in later elements are missed.
- Rename plural interfaces. An
itemsarray produces an interface calledItems;OrderItemis usually clearer. - Watch repeated key names. If two different nested objects share a key, such as
metaunder both billing and shipping, only the first shape is used for both. - Narrow strings. A status field arrives as
string; you may want a union like"paid" | "pending".
Using the interfaces
Annotate the parsed response, for example const data: Root = await res.json();. Keep in mind that TypeScript types disappear at runtime: they describe what you expect, but do not check what the server actually sends. For untrusted input, add runtime validation as well.
If the JSON will not parse, the error message names the problem; tidy it first in the JSON Formatter. Spreadsheet data can be turned into JSON with the Excel to JSON Converter and then typed here. Your sample stays in the browser, which matters when it contains real customer records.