DevKit

JSON to TypeScript

Turn a real API response into TypeScript interfaces, with nested types and optional fields inferred.

Free No sign-up Runs in your browser
JSON
TypeScript

From a real payload to a real type

Hand-writing interfaces from an API response is slow and error-prone: field names get mistyped, nested objects get flattened, and nullable fields are forgotten until production. Generating the skeleton from an actual response gives you a type that is guaranteed to match the data you are looking at — then you tighten it.

What the generator infers

  • Primitives map to string, number and boolean. JSON has one numeric type, so integers and floats both become number.
  • Nested objects become their own exported interface, named after the key that contains them (profile → Profile).
  • Arrays of objects are merged: if one element has an extra key, the resulting interface includes it. Fields missing from some elements are the place to add ? by hand.
  • null becomes the literal type null, which is a signal to widen the field to a union such as string | null.
  • Empty arrays become unknown[] — the generator refuses to guess.

What it cannot know

A sample is a single observation. Optional fields, unions of string literals, date strings, and enums all look like plain values. The honest workflow is: generate, then review each field and tighten it. In particular:

  • A field that is always present in the sample may be absent in production → make it optional.
  • "2024-01-31T10:00:00Z" is a string, but you probably want a branded date type or a parsed Date.
  • Status codes like "active" | "paused" infer as string; replace with the real union.

Run the type check against the API, not against your memory

The most reliable habit is to keep one captured response as a fixture and assert that it satisfies the generated type in a test (expectTypeOf<typeof fixture>().toMatchTypeOf<User>()). When the API changes, the test fails before your users find out. Tools like Zod or Valibot take this further: define the schema once, derive the TypeScript type and validate at runtime.

Frequently asked questions

Should I use interfaces or type aliases?
For plain object shapes they are nearly interchangeable. Interfaces can be merged and extended across declarations; type aliases handle unions and mapped types. Pick one convention per project — the toggle above generates either.
Why are some fields marked with null?
The sample contained a literal null. Widen it manually to string | null (or add ? if the key can be missing entirely) — TypeScript treats those as different things.
Can it generate Zod or JSON Schema instead?
Not yet — this page emits TypeScript only. For runtime validation, define the schema in Zod and infer the type from it; that keeps validation and types in sync automatically.
Is the JSON I paste stored?
No. Generation happens in your browser and the page makes no network requests.

Related tools