JSON to TypeScript converter
Paste an example of a JSON response and get TypeScript types for it. Nested objects become separate named interfaces, array items are merged into one shape, and a key that is missing from some items is marked optional.
Runs in your browser: nothing you enter is sent anywhere.
How to use it
- Paste a JSON document, ideally a real response that contains an array with several items.
- Choose
interface or type, and whether to add export, optional marks and readonly.
- Copy the declarations into your project and adjust names or unions to match your API.
How types are inferred
| In the sample |
In TypeScript |
"text" |
string |
1, 2.5 |
number |
true |
boolean |
null |
null |
[1, 2] |
number[] |
[1, "a"] |
(string | number)[] |
{...} |
a named interface or type |
Keys that are not valid identifiers, such as "a-b", are quoted.
Example
{"orders":[{"sku":"A","qty":1},{"sku":"B","qty":"2","note":"x"}]}
gives Root with orders: Order[], and Order with sku: string, qty: string | number and note?: string, because only the second item has a note.
Tips
Include more than one item in each array so that optional keys and mixed types show up. If the top level is an array, the result is a type alias such as Root = RootItem[].
Frequently asked questions
How are the interface names chosen?
The top-level type is called Root. A nested object takes the PascalCase form of its key, and the items of an array use a naive singular of the key, so "orders" gives Order and "categories" gives Category. When two names clash, a number is added, for example Item2.
When does a property become optional?
Only when it is missing from some of the objects merged at the same place, typically the items of one array. A single object cannot show that a key is optional, so every key of it is required. You can turn the question mark off.
What happens when the same key has different types?
The types are joined into a union, for example string | number. null is added as | null. Whole numbers and decimals are both number, because TypeScript has one number type.
What does an empty array become?
unknown[], because nothing in the sample says what its items would be. Replace it by hand once you know.
Is the result a guarantee about the API?
No. It describes only the sample you pasted. Fields that are sometimes absent or null in other responses will not show up unless the sample includes them, so use several array items or a sample with the awkward cases.