ZapKit

JSON to TypeScript Interface Generator

Paste an API response and get TypeScript interfaces for it, with nested objects split into their own types and optional fields worked out from arrays.

What is a JSON to TypeScript generator?

A JSON to TypeScript generator writes type definitions that describe the shape of some JSON, so you get autocompletion and type checking when you use that data in code. Typing a large API response by hand is slow and easy to get wrong; generating it from a real sample takes seconds.

This generator creates one named interface or type alias per object, merges the shapes of objects in arrays, marks fields missing from some items as optional, quotes keys that aren't valid identifiers, and can make every property readonly.

How to use the JSON to TypeScript Interface Generator

  1. 1.Paste a JSON sample

    A real API response works best, with arrays containing a few items.

  2. 2.Name the root type

    Choose interface or type, and readonly if your code treats the data as immutable.

  3. 3.Copy into your code

    Copy or download the generated .ts and adjust names to taste.

Example: a user with repos

The sample user object, with root name User:

User
id: number; login: string; isAdmin: boolean; tags: string[]; profile: Profile; repos: Repo[]
Profile
city: string; website: null
Repo
name: string; stars: number; archived?: boolean

archived is optional because only the second repo had it.

How the types are inferred

Each object becomes a named type, named after its key in PascalCase: a profile object becomes Profile, and items in a repos array become Repo. When an array holds several objects, their keys are merged, and a key missing from some items is marked optional with ?.

null in the sample becomes the null type, because one value can't say what else it might be; widen it to string | null by hand if needed. Keys that aren't valid identifiers, like site-url, are quoted.

Frequently asked questions

Interface or type: which should I choose?

Either works for describing JSON. Interfaces can be extended and merged; type aliases can also express unions. Many teams just pick one style.

Why is a field marked optional?

It was missing from at least one object in an array, so the generator can't assume it's always there.

Why is a field typed as null?

The sample only had null for it. Use a sample where it has a real value, or edit the type to something like string | null.

Does it validate data at runtime?

No. TypeScript types disappear when compiled. For runtime checks, use a schema library such as Zod.

More Developer tools