Skip to content
DevTools

JSON to TypeScript

JSON → types

Turn a JSON sample into named TypeScript interfaces.

root type name
json
typescript
Interfaces appear here

Every element of an array is inspected, not just the first: a field that some records lack comes out optional, and mixed types come out as a union. Identical shapes share one interface rather than being duplicated.

About JSON to TypeScript

Paste a JSON response and get TypeScript interfaces for it. Nested objects become their own named interfaces, arrays of objects are named in the singular — a "categories" array yields a Category interface — and keys that are not valid identifiers are quoted so the output compiles as-is.

The part that makes this worth using rather than eyeballing it is how arrays are handled. Every element is inspected, not just the first. If ninety records have a nickname and ten do not, the field comes out optional rather than wrongly required. If a field is sometimes a number and sometimes a string, you get a union instead of a guess. And two objects with identical shapes share one interface instead of producing near-duplicate definitions.

Treat the result as a strong first draft rather than a finished type. A sample cannot tell you that a field is a union when your sample only ever showed one branch, or that a string is really an enum — only the API's documentation can.

Common questions

Why did a field come out optional when the API always sends it?
Because at least one object in your sample was missing it. That is the honest reading of the data you provided. Paste a larger or more representative sample, or make the field required by hand once you have checked the contract.
How are null values typed?
As null. If a field is null in one record and a string in another, you get `string | null`, which is what you want. If it is null in every sample, there is nothing to infer beyond null — you will need to widen it yourself.
Can it produce types for other languages?
Not yet — this generates TypeScript only. The same inference would work for Go structs or Python dataclasses, and may follow.
Should I use interfaces or types?
For describing the shape of API data, either is fine and interfaces are emitted here because they read more cleanly when nested and can be extended later. The practical difference only shows up in declaration merging, which you are unlikely to want for a response type.

This page does the work itself, in this tab. Nothing you paste is sent to a server. Close the tab and no copy remains.