JSON to TypeScript
Generate TypeScript interfaces or type aliases from a JSON sample in your browser. Nested objects, optional fields and unions are handled.
How to use the JSON to TypeScript generator
- Paste a JSON sample into the JSON sample box. It can be an object, an array or a single value.
- Choose the Declaration: interface or type.
- Type a Root name (the default is
Root). It is converted to PascalCase, soapi responsebecomesApiResponse. - Tick Export declarations to put
exportin front of every declaration. - Click Generate types, or keep typing: the output updates as you edit. Then click Copy, or Download the result as
types.ts.
If the sample is not valid JSON, the error message gives the line and column of the problem, and nothing is generated until the text is fixed.
How the types are inferred
Each JSON value becomes a TypeScript type. Strings become string, booleans become boolean and null becomes null. JSON has a single number type, so every number becomes number, whether it is an integer or not. An object becomes a named declaration, and the name comes from its key: a field called user_info produces an interface called UserInfo. The root object takes the name typed in Root name.
Take this sample:
{"user_name": "Ada", "address": {"city": "Paris"}, "tags": ["admin"], "nickname": null}
The generator writes:
interface Root {
user_name: string;
address: Address;
tags: string[];
nickname: null;
}
interface Address {
city: string;
}
Arrays are read item by item, and all the objects inside one array are merged into a single shape. A field present in every item is required. A field missing from at least one item becomes optional, written label?: string. Arrays with different kinds of values produce a union: [1, "a", {"x": 1}] under the key values gives (string | number | Value)[]. An object that is sometimes null gives a union such as Address | null. An empty array has no known element type, so it becomes unknown[].
Keys that are not valid TypeScript names are written in quotes, for example "first-name": string;.
Interface or type alias
Both options describe the same shape. interface writes interface Root { … }, which suits object shapes and can be extended later in a project. type writes type Root = { … };, which is handy when you also need unions or aliases. A root that is an array or a single value cannot be an interface, so it is always written as a type alias, such as type Root = RootItem[];.
Limits of a generated type
The generator only knows the sample it receives. A field that never appears in the sample cannot be guessed, and a single object is treated as complete, so every field is required. To get optional fields, paste an array with several representative items. Types are chosen from JSON syntax only: a date written as text is a string, and a value that can only be one of a few words still shows as string. Adjust those by hand, for example by replacing string with a union of literal types. If two different shapes get the same name, a number is added (Address2).
Frequently asked questions
Do I need an array with many items?
No, but it helps. One object gives required fields only. Several items let the generator see which fields vary, and mark the others as optional.
Why is a number typed as number and not as an integer type?
TypeScript has no integer type, and JSON does not separate integers from decimals. number is the correct type for both.
Can I use the output for an API response?
Yes, paste a real response as the sample. Check the result against the API documentation, because a single response may not show every optional field or every possible value.
Why does a field appear as optional?
It is missing from at least one object in the sample. The generator marks such fields with ? so that code reading them is forced to handle the missing case.
Is my JSON uploaded anywhere?
No. The conversion runs in your browser. The JSON you paste never leaves your device.
Related tools
- JSON Formatter and Validator Free JSON formatter and validator that beautifies, minifies and checks JSON in your browser, with syntax errors reported by line and column.
- JSON to YAML Converter Convert JSON to YAML and YAML to JSON in your browser, with anchors, merge keys and multi-document files supported. Nothing is uploaded.
- JSON to CSV Converter Convert JSON to CSV and CSV back to JSON in your browser. Nested keys are flattened into columns, delimiters are detected, and nothing is uploaded.