JSON to TypeScript
Paste an API response or any JSON to get matching TypeScript types.
This free JSON to TypeScript converter works in your browser. Everything runs in your browser. What you type is not sent anywhere. All inputs and calculations run locally on your device without server uploads; details on browser processing and storage are set out in the privacy policy.
How types are inferred
- Each nested object becomes its own named interface, named after its key.
- Arrays of objects are merged into one interface. A key missing from some items becomes optional (
?). - Arrays of mixed values become a union, such as
(string | number)[]. nullvalues are typed asnull; you will usually want to change that to the real type, or turn on “Make null values optional”.- Keys that are not valid identifiers, such as
"first-name", are quoted.
Tips
Paste a response with as many fields filled in as you can find: types can only be inferred from values that are present. For runtime validation as well as types, consider a schema library such as Zod.
Worked example: Generating nested TypeScript interfaces from a REST API response
Suppose you capture this JSON response from a /v1/orders/881 endpoint, where items contains mixed discount values (number and null) and an empty tags array:
{
"orderId": "ord_881",
"totalCents": 12500,
"isPaid": true,
"customer": { "id": 42, "email": "dev@example.com" },
"items": [
{ "sku": "KB-01", "qty": 1, "discount": null },
{ "sku": "MS-09", "qty": 2, "discount": 500 }
],
"tags": []
}
The converter inspects every object in the items array, merges { discount: null } and { discount: number } into the union type number | null, extracts nested objects into named sub-interfaces, and outputs:
export interface Root {
orderId: string;
totalCents: number;
isPaid: boolean;
customer: Customer;
items: Item[];
tags: unknown[];
}
export interface Customer {
id: number;
email: string;
}
export interface Item {
sku: string;
qty: number;
discount: number | null;
}
Limits and what this tool cannot do
- Can only infer types present in the sample JSON: If an optional field is completely omitted from your sample JSON response, or if a field happens to be
nullin every single array element (inferred asnull), TypeScript cannot guess that the backend might return astringthere on another request. Always reviewnullandunknown[]fields against your API documentation. - Does not generate runtime validation schemas (Zod / Valibot / Yup): TypeScript
interfaceandtypedeclarations are erased at compile time and do not validate runtime network payloads; for runtime schema validation, see JSON to JSON Schema. - String literal unions (
"pending" | "paid"): Because a single JSON sample shows"status": "paid", the generator infersstatus: stringrather than a closed enum union.
Questions
Is my JSON uploaded?
No. Everything runs in your browser. What you type is not sent anywhere.
Interface or type?
For object shapes they are interchangeable. Interfaces can be extended and merged; types can express unions. Pick whichever your codebase uses.
Can it handle a top-level array?
Yes. The root becomes an array type of the merged item interface.
Why did an empty array [] generate unknown[] instead of any[]?
In modern strict TypeScript (strict: true), unknown[] is safer than any[] because it reminds you to replace unknown with the actual element type (such as string[]) rather than silently disabling type checking.
How does the tool handle JSON keys with hyphens or spaces like 'content-type'?
Any property key that is not a valid JavaScript identifier (such as "content-type" or "2fa_enabled") is automatically wrapped in quotes inside the TypeScript interface ("content-type": string;).
Should I use 'interface' or 'type' in TypeScript?
For object shapes representing API payloads, interface and type behave almost identically; interface is the default convention in most TypeScript codebases and produces clear compiler error messages.
Related converters and guides
- JSON to MarkdownConvert JSON to Markdown instantly in your browser.
- JSON to SQLConvert JSON to SQL instantly in your browser.
- JSON formatterFormat, validate and minify JSON, with the error position shown.
- Code formatterBeautify or minify CSS, HTML and XML.
- JWT decoderRead the header and claims of a JSON Web Token and check its signature.
- URL parserSplit a URL into parts and edit its query string.