About the JSON to TypeScript
Typing API responses by hand is slow and easy to get wrong. A JSON to TypeScript converter reads a sample response and writes the matching interfaces for you, so your editor can autocomplete fields and the compiler can catch typos and missing checks.
This tool creates a named type for every nested object and looks at every item in an array to build one shape, marking fields optional when only some items have them.
How to use the JSON to TypeScript
- Paste a JSON sample into the Input editor, or click Load sample.
- Set the Root type name, for example
UserorApiResponse. - Choose
interfaceortype aliasin the Style menu. - Click Generate types or press Ctrl + Enter (Cmd + Enter on Mac), then copy the result into a
.tsfile.
Examples
Nested object to interfaces
Input
{
"orderId": "A-1001",
"paid": true,
"total": 59.9,
"items": [{ "sku": "MUG-01", "qty": 2 }],
"customer": { "name": "Ada", "tags": ["vip"] }
}Output
export interface Order {
orderId: string;
paid: boolean;
total: number;
items: Item[];
customer: Customer;
}
export interface Item {
sku: string;
qty: number;
}
export interface Customer {
name: string;
tags: string[];
}Root type name set to Order. Each nested object gets its own named interface.
Array items with different keys become optional fields
Input
[
{ "id": 1, "name": "Ada", "email": "ada@example.com", "nickname": "countess" },
{ "id": 2, "name": "Linus", "nickname": null }
]Output
export type User = UserItem[];
export interface UserItem {
id: number;
name: string;
email?: string;
nickname: string | null;
}email is missing from one item, so it gets ?. nickname is a string in one item and null in the other, so it becomes string | null.
Common errors and how to fix them
Expected property name or '}' in JSON at position 1 (line 1 column 2)
The input is a JavaScript object literal, not JSON. Wrap keys in double quotes, or run JSON.stringify(obj) in your console and paste the result.
A field is typed as `null` instead of its real type
Every sample had null for that field, so there was nothing else to infer from. Add an example where the field has a value, or edit the type to string | null by hand.
An empty array becomes `unknown[]`
An empty array has no items to infer a type from. Put one representative item in the array before generating the types.
Good to know
- Nested objects get their own type, named after their key:
ownerbecomesOwner, and items ofusersbecomeUser. - In arrays of objects, a field missing from some items becomes optional (
?), and a field with different value types becomes a union such asstring | null. - Objects with the same shape share one type. Different shapes with the same name are numbered, such as
Data2. - Empty arrays become
unknown[]and empty objects becomeRecord<string, unknown>, because there's nothing to infer from. Replace them with the real types if you know them. - Types describe only the sample you paste. Dates stay
string, all numbers arenumber, and a field that is alwaysnullin the sample is typed asnull.
Frequently asked questions
Should I use interface or type?
Both work for describing JSON. interface is common for object shapes and can be extended later; type aliases can also express unions. Pick whichever your codebase already uses.
Why is a field marked optional?
It appeared in some items of an array but not all of them. If the field is actually always present, paste a sample where every item has it, or remove the ? by hand.
Can I use the result with Zod or other validators?
The output is plain TypeScript types, which exist only at compile time. To validate data at runtime, write a schema with a library like Zod and infer the type from it instead.