JSON to TypeScript Converter
Paste JSON and get TypeScript interfaces or types. Nested objects, arrays, null values and optional fields are handled for you.
How to use the JSON to TypeScript converter
- Paste a JSON object or array, for example a real API response, into the input box.
- Name the root type, choose
interfaceortype, and set the export and readonly options. - Copy the result or download it as a
.tsfile and drop it into your project. - If the JSON is rejected, check it with the JSON Validator. Starting from a spreadsheet? Convert it first with CSV to JSON.
Worked example: optional fields and nested types
The sample order above has two line items. The first has sku, qty and price; the second also has giftNote. Because giftNote is missing from one element, it becomes optional. The other rules at work:
- Nested objects become their own named types, named after the key:
customerbecomesCustomer. - Arrays of objects become a singular type name:
itemsbecomesItem[]. phone: nullbecomesphone: null. If another sample held a string there, you would getstring | null.- Numbers (integer or decimal) are
number;tagsbecomesstring[].
export interface Item {
sku: string;
qty: number;
price: number;
giftNote?: string;
}Types are inferred from the sample only. A field that never appears, or an enum-like string, cannot be detected, so review the output. TypeScript types are erased at compile time and do not validate data at runtime.
Frequently Asked Questions (FAQ)
No. The conversion runs entirely in your browser with JavaScript. Your JSON is not sent to a server, logged or stored, so it is safe to paste real API responses (though you should still remove secrets such as tokens before sharing the result).
When the JSON contains an array of objects, the converter merges every element. A key that is missing from some elements becomes optional (key?: type), and a key whose type differs between elements becomes a union such as string | number. A single object has no variation to compare, so all its keys are required.
A null value becomes the type null. If the same key is a string in one array element and null in another, the result is string | null. A key that only ever holds null is typed null, because a sample alone cannot tell what else it could be; edit it to the real type.
For plain object shapes they are interchangeable. interface can be extended and merged and gives clearer error messages; type aliases can also name unions and tuples. Pick the one your codebase already uses; the tool can output either.
It matches the sample you pasted, nothing more. Fields that are absent from the sample, numbers that are sometimes strings and enums can’t be inferred from one example. Paste several representative responses in an array to get better optional and union detection, and treat the output as a starting point to review.
No. TypeScript types exist only at compile time, so they do not check data coming over the network. For runtime checks use a schema library such as Zod, and validate your JSON structure first with the JSON Validator.
An empty array has no elements to learn from, so it becomes unknown[]. Keys such as first-name or 2nd are quoted automatically ("first-name": string).
Related Tools
- YAML to JSON Converter (and JSON to YAML)Convert YAML to JSON or JSON to YAML with line-numbered errors, indent options, copy and download. Runs in your browser.
- Docker Run to Docker Compose ConverterPaste a docker run command and get a docker-compose.yml service: image, ports, volumes, env vars, restart, network and more. Unsupported flags are listed. Runs in your browser.
- cURL to fetch, Axios and Python requests ConverterPaste a cURL command and get JavaScript fetch, Axios or Python requests code. Parsed in your browser; nothing is uploaded.
- Cron Expression Generator and ExplainerBuild cron expressions from dropdowns, explain them in plain English and preview the next 5 run times in your timezone.
- JSON ValidatorValidate, format and visualize your JSON data with ease.
- CSV to JSON ConverterConvert CSV data to JSON format with customizable options.