Skip to content
Orpheus

JSON to TypeScript Interface Generator

Turn a sample payload into interfaces, with optional keys detected.

Options
Declaration
Result

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

  1. Paste a representative payload. Use a real response with several array elements, not a single trimmed example — that is what reveals optional keys.
  2. Name the root. Nested objects are named from their key, and collections are singularised so posts yields a Post interface.
  3. 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?
Because they appeared in some elements of an array and not others. Generating from the first element alone would claim they are always present, which breaks the moment the code meets an element without them.
What happens to null values?
A null becomes the null type, and where a key is null in one element and a string in another it becomes a union such as string | null. A field that is only ever null cannot be inferred further.
Can generated types replace runtime validation?
No. TypeScript types are erased at compile time, so they describe what you expect rather than what arrives. Use them alongside a runtime check at the boundary, not instead of one.
What about an empty array?
It becomes unknown[], because there is nothing to infer from. Supply a sample containing at least one element if you want a real element type.
Should I use an interface or a type alias?
Either works for object shapes. Interfaces can be reopened and merged by later declarations, which is useful for extending third-party types and undesirable if you want the shape fixed.

Last updated