OnlineTextLab
Esc

JSON to TypeScript Interface Generator

Generates interfaces from a sample response, reading every item in a list so optional fields and unions come out right.

TypeScript output

Your TypeScript appears here as you type.

Nothing to paste yet? Use Try an example to see it work.

Options
Declare with

Runs on your device. Your files and text are not uploaded.

About JSON to TypeScript

Typing an API response by hand is slow, and a tool that only looks at the first array element gets it wrong as soon as the second element has an extra field. This generator reads the whole sample and merges what it finds, so the type describes all of it.

Paste a response from your browser's network tab, and you get an interface for each object in the structure. It is the quickest way to start typing an endpoint with no OpenAPI spec.

The result is a starting point you can paste into your project and refine. It can only describe what the sample contains, so the more complete the sample, the better the types.

How it works

The whole document is walked and merged into one description of its shape. In a list of objects, a key present in every item is required and a key present in only some gets ?. A value seen as both a string and null becomes string | null, and mixed lists become (string | number)[].

Each object gets its own named interface, built from its parent's name and its key: address under User becomes UserAddress. If two names collide, the later one gets a number, so one type is never silently reused for different data. Keys that are not valid identifiers, such as last-login, are quoted.

An empty list has nothing to learn from, so it becomes unknown[], and an empty object becomes Record<string, unknown>. Both are honest placeholders for you to fill in.

How to use JSON to TypeScript

  1. Paste a JSON sample, the fuller the better, or open a .json file.
  2. Set the Root type name to what this data represents.
  3. Pick interface or type under Declare with, and decide whether to Add export.
  4. Copy the types into your project, or use Download .ts.

A list where the second item has an extra field

Input
[{"id":1,"name":"Ada","address":{"city":"London","zip":null},"roles":["admin"]},{"id":2,"name":"Grace","address":{"city":"Arlington","zip":"22201"},"roles":[],"last-login":"2024-05-01"}]
Output
export type User = UserItem[];

export interface UserItem {
  id: number;
  name: string;
  address: UserItemAddress;
  roles: string[];
  "last-login"?: string;
}

export interface UserItemAddress {
  city: string;
  zip: string | null;
}

last-login appears only in the second item, so it is optional, and it is quoted because of the hyphen. zip is null in one item and a string in the other, which gives string | null.

When it helps

  • Typing the response of a third-party API that has no published types.
  • Creating a first version of types for a fixture or test data file.
  • Describing a configuration file so that an editor can check it.
  • Finding out which fields of a response are really optional, by pasting several records as a list.

Tips for a better result

  • Paste several real records in a list, including ones with edge cases. Only values in the sample can shape the type.
  • Rename generated types such as UserItem to User after pasting. A list at the root produces the Item suffix.
  • Dates in JSON are strings, and they are typed as string here. Convert them yourself if you parse them into Date objects.
  • Add a runtime check at the boundary if the data matters. These types only help at compile time, and a server can still send something else.

Good to know

  • It cannot know about values missing from the sample, so a field that is only sometimes null will look non-nullable if your sample never shows it.
  • No enums or string literal types are inferred. A status field is just string.
  • Objects with the same shape under different keys get separate interfaces, which you may wish to merge by hand.
  • A map keyed by IDs is typed as an object with those exact keys, since JSON cannot show the difference. Change it to Record<string, T> yourself.

Questions people ask

Should I use interface or type?

For object shapes either works. Interfaces can be extended or merged with another declaration of the same name, while type aliases cannot be reopened. Follow whatever your codebase already does.

Why is a field optional when it is always present in my real data?

It is missing from at least one object in the sample you pasted. Remove the question mark if you know better, or paste a sample without the odd record.

Can it read a list at the top level?

Yes. A root list gives export type Root = RootItem[]; along with an interface for the items, merged across all of them.

What about numbers too long for JavaScript?

Integers beyond the exact range of a JavaScript number are read as text so that no digits change, and they are typed as string. A notice tells you when this has happened, which is usually accurate for what your code will have to do with them.

What to use next

Learn more

Last reviewed . Read our editorial policy.