JSON to TypeScript is a free converter that generates TypeScript interfaces or type aliases from example JSON, such as an API response. It detects nested objects, arrays, optional properties and nullable values. Everything runs in your browser, so you can safely paste real data.
How to convert JSON to TypeScript
- Paste a JSON example into the left box. A real API response works best.
- TypeScript declarations appear on the right as you type.
- Optionally rename the top-level type, switch between
interfaceandtype, or removeexport. - Copy the result into your project, or download it as
types.ts.
Example
{
"id": 7,
"owner": { "id": 12, "name": "Priya" },
"tasks": [
{ "id": 1, "text": "Write docs", "dueDate": "2026-10-01" },
{ "id": 2, "text": "Ship it", "dueDate": null, "assignee": { "id": 12, "name": "Priya" } }
]
}generates:
export interface Root {
id: number;
owner: Owner;
tasks: Task[];
}
export interface Owner {
id: number;
name: string;
}
export interface Task {
id: number;
text: string;
dueDate: string | null;
assignee?: Owner;
}Notice what the converter worked out from the two tasks:
dueDateis a string in one task andnullin the other, so it becomesstring | null.assigneeonly appears in the second task, so it is optional (assignee?).assigneehas the same shape asowner, so both reuse theOwnerinterface instead of creating a duplicate.- The array
tasksgets an interface namedTask, the singular of its key.
Tips for accurate types
- Include several array items. Types are only as good as the example. With one item, nothing can be detected as optional.
- Avoid samples where a field is always null. A field that is only ever
nullis typed asnull; include a sample where it has a value. - Empty arrays become
unknown[]. Add at least one item, or edit the type by hand. - Dates stay strings. JSON has no date type, so
"2026-10-01"is astringin TypeScript too.
Edge cases handled
- Keys that aren’t valid identifiers, such as
"first-name"or"2fa", are quoted. - Arrays of mixed values become union arrays, such as
(number | string)[]. - A top-level array produces
type Root = RootItem[]. - Two different nested objects with the same key name get distinct names such as
DataandData2.
Types are not runtime validation
TypeScript types are removed when your code is compiled, so they can’t stop an API from returning unexpected data at runtime. If you need to check data as it arrives, use a validation library such as Zod alongside these types.
If your JSON has an error, the converter points it out; the JSON Validator lists every problem at once.
Frequently asked questions
How are optional fields detected?
When your JSON contains an array of objects, every object is compared. A property missing from some of them is marked optional with ?. Paste a response with several items for the most accurate types.
How are null values handled?
A value that is null in one place and a string elsewhere becomes string | null. If a field is only ever null in your sample, it is typed as null, so add a sample with a real value to get a better type.
Should I use interface or type?
Either works for describing JSON. Interfaces can be extended and merged, and many style guides prefer them for object shapes. Type aliases are needed for unions and top-level arrays. Pick whichever your project uses.
Does it check my data at runtime?
No. TypeScript types disappear when your code runs, so they cannot catch an API that returns unexpected data. For runtime checks, use a validation library such as Zod.
Is my JSON uploaded?
No. Types are generated in your browser, so it is safe to paste real API responses that contain private data.
Last updated