JSON to TypeScript
Turn a JSON sample into TypeScript interfaces, with optional fields detected across array elements and union types instead of any.
What was inferred
Import from File
Drag & drop a file here
or
Supports .txt, .csv, .log, .json and other text files
Generation happens entirely in your browser, so a real API response containing real data never leaves your machine.
What It Does
Paste a JSON response and get the TypeScript interfaces that describe it. The value is in what the generator works out rather than in the transcription itself. It reads every element of an array instead of typing from the first, so a key that appears in some records and not others comes back marked optional — which is the difference between types that describe your data and types that describe one lucky sample. Where a field holds different types in different records it emits a union rather than giving up and writing any. And when the same shape appears in several places it emits one interface and references it, so the output stays proportional to the number of distinct shapes rather than the number of times they occur. Everything happens in your browser, which matters when the sample you want to type is a real response containing real data.
When to Use It
- An API returns a response you need to consume in a TypeScript project and you want the types without transcribing forty fields by hand.
- You are checking whether an endpoint's response is consistent — inconsistent types and unexpectedly optional fields show up immediately in the generated output.
- A third-party service changed its response and you want to see the new shape against the types you already have.
- You have a large fixture file and want interfaces for your tests without pulling in a code-generation dependency.
- You are documenting an internal API and want an accurate description of the payload as it actually is rather than as it was specified.
Worked Examples
[{"id":1,"title":"First","pinned":true},{"id":2,"title":"Second"}]
The optional-field case. Because pinned appears in only one of the two records, it comes back as pinned?: boolean — something a generator reading only the first element would get wrong.
{"page":1,"results":[{"id":9,"author":{"id":1,"name":"Jane"},"editor":{"id":2,"name":"Sam"},"deletedAt":null}]}
Shows two things at once: author and editor have the same shape so they share a single interface, and deletedAt becomes string | null rather than just null, once a populated record is present.
{"id":1,"tags":[],"scores":[1,"high"]}
The honest-uncertainty case. The empty tags array becomes unknown[] with a warning, because nothing in the sample says what it holds, while scores becomes (number | string)[] with the parentheses that keep the meaning correct.
Features
How to Use
1. Paste your JSON — ideally a full array of records rather than a single object, so optional fields can be detected. 2. Set a name for the root interface. 3. Adjust the options if you want type aliases, readonly fields or nullable-as-optional. 4. The interfaces appear immediately, along with a summary of what was inferred. 5. Click Copy to take the generated code.
Common Mistakes
- Generating from a single object. Every field looks required because in that one object it is — paste the whole array from a real response and the optional fields reveal themselves.
- Trusting the types at runtime. TypeScript is erased when it compiles, so nothing stops a third-party API returning a different shape and your code believing the old one.
- Accepting unknown[] without checking. It means your sample had an empty array, so the real element type is knowable — you just need a response that includes one.
- Ignoring a union you did not expect. A field typed number | string usually means the API is genuinely inconsistent, which is worth fixing rather than typing around.
- Pasting production data into a generator you do not control. This one runs entirely in your browser, but that is not true of every tool offering the same feature.