JSON to TypeScript
Paste a JSON object or API response and get TypeScript interfaces for it, with nested types, optional fields and unions worked out from the data.
Generated in your browser; your JSON is never uploaded.
Related Developer Tools
This JSON to TypeScript converter writes the interfaces for a piece of JSON so you do not have to type them out field by field. Paste an API response, a config file or a fixture, and it produces a typed declaration for every object, works out which fields are optional by comparing array items, and turns mixed values into union types. Copy the result into your project and adjust names as needed.
Example
This response:
{
"id": 42,
"name": "Ayesha Khan",
"address": { "city": "Lahore", "zip": "54000" },
"orders": [
{ "id": 1001, "total": 49.99, "paidAt": "2025-04-02T10:15:00Z" },
{ "id": 1002, "total": 12.5, "paidAt": null, "coupon": "SPRING" }
]
}gives:
export interface Root {
id: number;
name: string;
address: Address;
orders: Order[];
}
export interface Address {
city: string;
zip: string;
}
export interface Order {
id: number;
total: number;
paidAt: string | null;
coupon?: string;
}coupon is optional because only one order has it, and paidAt is string | null because it is a string in one order and null in the other. The more varied the sample, the more accurate the types, so paste a response that includes the edge cases you know about.
How JSON Values Map to TypeScript
| JSON | TypeScript |
|---|---|
"text" | string |
42, 4.5 | number (TypeScript has one number type) |
true | boolean |
null | null, or part of a union such as string | null |
{ โฆ } | A named interface |
["a", "b"] | string[] |
[1, "a"] | (string | number)[] |
[] | unknown[], since an empty array shows no item type |
{} | Record<string, unknown> |
Options
- Root type name: the name of the top-level type. If the JSON is an array, this names the array type and its items get the singular form (Users and User).
- Declare as:
interfaceortypealiases. - export: adds
exportso the types can be imported from another file. - All fields optional: marks every field with
?, useful for partial updates or loosely documented APIs. - readonly: makes every field
readonly, for data you do not mean to change.
Limits of Generating Types From a Sample
Generated types describe the JSON you pasted, not the API's contract. A field that is always present in your sample may be missing in other responses, a string that looks like an enum ("active", "paused") is typed as plain string, and numeric IDs that are sometimes strings will only show up as a union if the sample has both. Types also do not check data at runtime: if an API changes, TypeScript will not notice. Where the API publishes an OpenAPI or JSON Schema description, types generated from that are more reliable; this tool is ideal for undocumented APIs, quick scripts and fixtures.
Related Tools
If the JSON will not parse, the JSON formatter shows where the error is and can fix common mistakes. To look inside an auth token from the same API, use the JWT decoder, and to turn spreadsheet data into JSON first, the CSV to JSON converter.
Frequently Asked Questions
How do I generate a TypeScript interface from JSON?
Paste the JSON, for example a response copied from your browser's network tab, and the interfaces appear on the right. Set the name of the top-level type, then copy the code or download it as a .ts file.
How are optional fields detected?
When the JSON contains an array of objects, every item is compared. A field that appears in some items but not others is marked optional with ?. A field from a single object cannot be judged that way, so turn on All fields optional if you know some may be missing.
What happens with null values?
A field that is null in some items and a string in others becomes string | null. A field that is null in every sample becomes just null, because the data gives no clue to its real type; replace it with the right type by hand.
Should I use interface or type?
For object shapes either works. Interfaces can be extended with extends and merged when declared twice, and editors often show their names in errors; type aliases can also describe unions and other non-object types. Many codebases pick one style for consistency, so choose whichever yours uses.
How are nested types named?
Each nested object gets an interface named after its key in PascalCase, so address becomes Address. Items of an array are named after the singular of the key: orders gives Order and categories gives Category. Two objects with identical fields share one interface.
Are dates detected?
No. JSON has no date type, so dates arrive as strings and are typed as string. Convert them to Date objects in your code if you need to.
Is my JSON uploaded?
No. The types are generated in your browser, so API responses with real data never leave your device.