JSON to TypeScript
Generate TypeScript interfaces or types from a JSON sample, with nested types, optional fields and union types inferred for you.
export interface User {
id: number;
email: string;
verified: boolean;
profile: Profile;
roles: string[];
orders: Order[];
}
export interface Profile {
displayName: string;
avatarUrl: null;
}
export interface Order {
id: string;
total: number;
items: Item[];
coupon?: string;
}
export interface Item {
sku: string;
qty: number;
}
Types are inferred from this one sample. Paste a response that includes optional fields and nulls, or an array of several items, to get the most accurate result.
About this tool
Typing an API response by hand is slow and error-prone, especially when it nests objects inside arrays inside objects. Paste a sample JSON response here and you get TypeScript interfaces (or type aliases) for the whole structure, with a named type for every nested object.
The generator does more than map values to types. When several items of an array differ, it merges them: keys that some items lack become optional, and values that are sometimes null or sometimes a different type become unions. Identical shapes are declared once and reused. Everything runs in your browser, so you can paste real responses safely.
How to use JSON to TypeScript
- Paste a JSON sample, or open a .json file. A sample is preloaded.
- Set the root type name, for example User or ApiResponse.
- Choose interface or type, and whether to add export and readonly.
- Copy the result or download it as types.ts.
Examples
Nested objects and arrays
{ "address": { "city": "London" }, "orders": [{ "id": 7, "items": [{ "sku": "X" }] }] } interface Root { address: Address; orders: Order[] } plus Address, Order and Item Array item types get singular names: orders → Order, items → Item, categories → Category.
Items that differ
[{ "id": 1, "email": null }, { "id": 2, "email": "[email protected]", "coupon": "SPRING" }] with root name Users type Users = User[]; interface User { id: number; email: null | string; coupon?: string } Keys that are not valid identifiers
{ "last-login": "2026-01-01" } "last-login": string;
What the generator infers
- Strings, numbers and booleans map to string, number and boolean; JSON has no separate integer type.
- null stays null and is combined with other types seen at the same place, giving unions such as null | string.
- Objects in arrays are merged; a key missing from some items is marked optional (key?:) unless you turn that off.
- Arrays with mixed contents become union arrays such as (number | string)[]; empty arrays become unknown[].
- Two objects with exactly the same shape share one declaration, even if they appear under different keys.
Limits of inferring from a sample
A single response cannot show everything an API might return. A field that happens to be null in your sample is typed as null only, and a date arrives as a plain string. Paste a larger sample with several array items, adjust the generated types by hand where you know more, and consider validating responses at runtime with a schema library if the data comes from outside your control.
Frequently asked questions
Everything you need to know about this tool and data security.
Should I use interface or type?
For object shapes they are nearly interchangeable. Interfaces can be extended and merged, and many style guides prefer them for objects; type aliases are needed for unions and mapped types. Pick whichever your codebase uses.
How are nested types named?
From the property name in PascalCase, singularised for array items. If two different shapes would get the same name, a number is added, such as Item2.
Is my JSON uploaded?
No. Parsing and generation happen in your browser.
Why is a field typed as null?
Because it is null in every place it appears in your sample. Add an example where it has a real value, or edit the type, for example to string | null.
Can it handle very large files?
Files of several megabytes work, but a few representative items are usually enough and give cleaner types.
Is my data safe when using this tool?
Yes, 100% safe. This tool operates entirely inside your web browser using client-side JavaScript and WebAssembly. Your data never touches any external server.
Related Tools
Prettify, validate, and minify JSON data instantly in your browser.
Generate Go struct definitions with json tags from a JSON sample, including nested types, slices, optional and nullable fields.
Convert a curl command into JavaScript fetch() code, with headers, JSON bodies, form data and basic auth handled.
Featured Projects tech-fun.net
Explore more AI & productivity tools
Nihongo GameBox
Master Japanese typing, Kana, and JLPT N5–N1 vocabulary through interactive retro arcade games.
ResumeGPT
Tailors CVs and cover letters to every job opportunity with a multi-agent LLM pipeline, job tracking, and scheduled discovery.
CollectMind
Turns your bookmarks into a searchable AI knowledge base — entirely on-device, powered by Chrome's built-in Gemini Nano.