JSON to TypeScript Interface Generator
Turn a sample payload into interfaces, with optional keys detected.
Everything is processed in this tab. Nothing you paste is sent anywhere.
Every tool runs entirely in your browser. Your files are never uploaded to a server.
A generator infers TypeScript types from a sample payload: objects become interfaces, arrays become element types, and a key present in only some array elements is marked optional rather than assumed to always exist.
How to use JSON to TypeScript
- Paste a representative payload. Use a real response with several array elements, not a single trimmed example — that is what reveals optional keys.
- Name the root. Nested objects are named from their key, and collections are singularised so posts yields a Post interface.
- Check the optional markers. Any property marked with a question mark appeared in some array elements and not others.
About generating TypeScript types
Generating types from a sample is quick and carries one important limitation: the generator can only describe the payload it was given. A field that happens to be a string in your sample and is sometimes a number elsewhere will be typed as a string, and the compiler will confidently accept code that then fails at runtime. The most common version of this problem is a key that appears in only some elements of a collection. Generating from the first element alone produces a type claiming the key is always present, which is precisely the assumption that breaks in production — so this tool reads every element of an array of objects, merges their keys, and marks anything not universal as optional. Nulls deserve the same care: a field that is null in the sample and a string in reality yields a type of null, which is almost never what you want, and is worth fixing by hand. The wider point is that a generated type is a description of one observation, not a contract. TypeScript erases types at compile time, so nothing checks that an incoming response actually matches. The reliable pattern is to generate types for developer convenience and to validate at the boundary with a runtime schema, so a payload that has changed shape fails where it arrives rather than several layers deeper.
Frequently asked questions
Why are some properties marked optional?
What happens to null values?
Can generated types replace runtime validation?
What about an empty array?
Should I use an interface or a type alias?
Last updated