JSON to TypeScript
Paste a JSON response and get clean TypeScript interfaces, with nested types, arrays and optional fields worked out for you.
This tool runs in your browser. We never send what you type or paste to a server.
How types are inferred
- Each object becomes an
interface, named after its key in PascalCase ("billing_address"→BillingAddress). - Arrays of objects are merged into one interface. A key missing from some items becomes optional (
key?: string). - Mixed values become unions, like
string | number.nullbecomes| null. - Empty arrays become
unknown[], because their item type can't be known from the sample.
Tips
Paste a response with as many variations as possible (for example several array items) so optional fields and unions are detected. Choose type instead of interface if your code style prefers type aliases.
Example
{"id": 1, "name": "Ana", "tags": ["admin"], "manager": null}
gives
export interface Root {
id: number;
name: string;
tags: string[];
manager: null;
}
A null in the sample only tells the tool the field can be null, not what it is otherwise, so widen it by hand (for example manager: User | null).
A worked example with an array of items
Two items that differ slightly:
{"status": "active", "items": [
{"price": 1.5, "tags": null},
{"price": 2, "tags": ["a"]}
]}
give
export interface Root {
status: string;
items: Item[];
}
export interface Item {
price: number;
tags: string[] | null;
}
Both items are merged into one Item interface (the name is the singular of items), and tags becomes string[] | null because one item has an array and the other null. With only the first item in the sample you would get tags: null, which is why several varied items give better types.
Naming and reuse
- Nested types are named after their key in PascalCase and singular for arrays:
categoriesgivesCategory,addressesgivesAddress. - Two objects with exactly the same fields share one interface. In
{"address": {"city": "Leeds"}, "billing": {"city": "York"}}both keys get the typeAddress. - Keys that are not valid identifiers, such as
"next-page"or"2fa", are kept in quotes. - A top-level array gives a type alias:
type Root = RootItem[]. Set the root name to match your domain, for exampleOrder.
What a sample cannot tell you
- Dictionaries:
{"u_17": {"name": "Ana"}, "u_42": {"name": "Ben"}}is a map keyed by user ID, but the generator sees two fixed properties,u_17andu_42. Replace them by hand withRecord<string, User>. - Enums:
"status": "active"becomesstring. If the API documents a fixed set, narrow it to"active" | "suspended". - Dates arrive as strings, so the type says
string. Convert them withnew Date()where you use them. - Integers: TypeScript has one
numbertype, and IDs beyond 9,007,199,254,740,991 are rounded byJSON.parsebefore your types ever see them. - Optional or null:
email?: stringmeans the key may be missing;email: string | nullmeans it is present but empty. APIs often mix both, so check the documentation.
Checking data at runtime
A type annotation such as const data = await res.json() as Root is a promise to the compiler, not a check. If the API sends "2" instead of 2, nothing complains until something breaks later. For data you do not control, validate it where it enters your program. A hand-written guard is enough for small shapes:
// A hand-written runtime check for: interface Item { price: number; tags: string[] | null }
const isItem = (v) =>
typeof v === 'object' && v !== null &&
typeof v.price === 'number' &&
(v.tags === null || (Array.isArray(v.tags) && v.tags.every((t) => typeof t === 'string')));
console.log(isItem(JSON.parse('{"price": 2, "tags": ["a"]}'))); // → true
console.log(isItem(JSON.parse('{"price": "2", "tags": null}'))); // → false a string price would pass an "as Item" cast
const order = JSON.parse('{"created": "2024-01-15T10:00:00Z"}');
console.log(typeof order.created); // → string JSON has no date type
console.log(new Date(order.created).getUTCFullYear()); // → 2024
For larger APIs a schema library such as Zod, Valibot or io-ts lets you write the schema once and derive the TypeScript type from it, so the two cannot drift apart.
Questions
Interface or type alias?
Both work for object shapes. Interfaces can be extended and merged; type aliases can express unions. Pick whichever your codebase uses.
Does it handle deeply nested JSON?
Yes. Every nested object gets its own named interface.
Does it validate data at runtime?
No. TypeScript types disappear when the code is compiled. To check data arriving from an API at runtime, use a schema library such as Zod alongside the types.
Why is a field typed as null?
Every sample value for that key was null, so the generator cannot know what it holds when it is set. Change it to the real type plus null, for example manager: User | null, or paste a sample where the field has a value.
Should I use unknown or any for fields I cannot type?
Prefer unknown. It forces you to check the value before using it, while any switches type checking off for everything it touches. The generator uses unknown[] for empty arrays and Record<string, unknown> for empty objects for that reason.
Can I generate types from a JSON Schema or OpenAPI file instead?
This tool works from sample data. If the API publishes a JSON Schema or an OpenAPI document, generating types from that is more reliable, because it lists every optional field and enum value rather than the ones that happened to appear in one response.