JSON to TypeScript
Turn JSON into TypeScript interfaces or types, merging array items and marking missing fields optional.
Types are worked out from the values in your JSON, so they describe this sample, not every possible response. Your JSON stays in this browser.
Runs in your browser. Nothing is uploaded.
What is JSON to TypeScript?
A JSON to TypeScript converter reads a piece of JSON and writes the TypeScript interfaces or types that describe it. Instead of typing out every field of an API response by hand, you paste a sample, copy the result and get autocomplete and type checking for that data in your editor.
The converter looks at every value, not just the first. In a list of objects it combines all the items, so a field that appears in only some of them becomes optional (`coupon?: string`) and a field that holds different kinds of value becomes a union (`string | number`). Nested objects get their own named types, such as `Address` for an `address` property, and objects with the same shape share one type. Names that would clash get a number (`Address2`), and names that would hide a built-in type, like `Date`, get a `Type` suffix.
You choose between `interface` and `type`, set the name of the root type, and can add `export`, make every property optional or readonly, and decide whether a value that is only ever null is written as `null` or `unknown`. The types are worked out from your sample, so they describe that sample and not every response the source could ever send. Everything runs in your browser and your JSON is never uploaded.
How to use
- Paste your JSON into the input box, or press Load sample.
- Choose interface or type, and enter a name for the root type.
- Turn on export, optional or readonly if you want them, and pick how a null-only value is written.
- Read the TypeScript in the output box. It updates as you type.
- Press Copy result or Download .ts and paste the types into your project.
Example
Input
{"id": 1, "name": "Ada", "tags": ["admin"], "address": {"city": "London"}}Output
interface Root { id: number; name: string; tags: string[]; address: Address; } interface Address { city: string; }How JSON becomes TypeScript
What each kind of JSON value is written as.
| Syntax | Meaning |
|---|---|
"text" | string |
3.14 | number (JSON has no separate integer type) |
true | boolean |
null | null (or unknown, if you choose that) |
[1, 2] | number[] |
[1, "a"] | (string | number)[] |
[] | unknown[] (nothing to learn from) |
{ "a": 1 } | a named interface or type with a: number |
{} | Record<string, unknown> |
Frequently asked questions
- Why is a property marked optional?
- When the JSON contains a list of objects and some of them do not have a field that others have, that field is written with a question mark. If you only paste one object, every property it has is treated as always present, so check the real API documentation for fields that can be missing.
- Should I use interface or type?
- For plain object shapes they behave almost the same. Interfaces can be extended and merged, and many teams use them for objects; type aliases can also describe unions and other shapes. Choose whichever your project already uses. A root that is not an object, such as an array of numbers, is always written as a type alias.
- What happens with null, empty arrays and empty objects?
- A value that is only null is written as null, or as unknown if you choose that, because a sample cannot tell what type would be there. An empty array becomes unknown[] and an empty object becomes Record<string, unknown> for the same reason. If a field is null in some items and a string in others, it becomes string | null.
- Can it validate data at run time?
- No. TypeScript types are removed when your code is compiled, so they do not check data that arrives while the program runs. To check real data, use a validation library or a JSON Schema and keep the type in sync with it.
- Is my JSON sent anywhere?
- No. The conversion runs in your browser, and the JSON you paste is only remembered on your own device so it is there when you return. Use Clear to remove it.
Related tools
- JSON Formatter & ValidatorFormat, minify and validate JSON online with line-accurate error messages.
- YAML to JSON & JSON to YAMLConvert YAML to JSON and JSON to YAML with error positions, expanded anchors and multi-document support.
- Fake Data GeneratorMake test data from fields you choose or a JSON Schema, as JSON, CSV or SQL INSERT, repeatable with a seed.
- CSV to JSON & JSON to CSVConvert CSV to JSON and JSON to CSV with delimiter detection and nested JSON flattening.
- JSON DiffCompare two JSON documents and see every added, removed and changed value by path.