How to convert JSON to TypeScript
- Paste a sample JSON object or array, such as an API response.
- Set the root interface name, for example
UserorOrdersResponse. - Interfaces are generated instantly: one for the root and one for each nested object, named after its key. Arrays of objects are merged into a single item type.
- Copy the code into a
.tsfile. Untick export if you prefer non-exported declarations.
Smart inference
Writing interfaces for large API payloads by hand is slow and error-prone. The generator reads every value in your sample: if some array items are missing a key, that property becomes optional (coupon?: string); if a value is sometimes null, it becomes string | null; if a key holds different types in different items, you get a union. Keys that are not valid identifiers, such as first-name, are quoted automatically.
Because inference only sees your sample, check the result: an empty array becomes unknown[], and an ID that happens to be numeric will be typed as number. Give the generator a representative sample, ideally with several array items, for the best result.
Need runtime validation as well as types? Switch the language to Zod schema. The conversion runs entirely in your browser, so production data stays private.
FAQ
Interface or type alias: which is generated?
Interfaces, because they give clearer error messages and can be extended. You can change interface X { } to type X = { } if your codebase prefers aliases.
How are dates handled?
JSON has no date type, so ISO date strings are typed as string. Parse them into Date objects in your code if needed.
Can I generate types from a JSON array?
Yes. All items are merged into one interface and the root becomes an array type.