Loading JSON to TypeScript Converter...
Please wait a moment

JSON to TS in Three Steps

You have a JSON response from an API and want types for it in a React, Next.js or Node project. Paste it, copy the interfaces, done. If you want the longer story of how fields are typed, the JSON to TypeScript page goes through it in detail.

Step 1

Paste a response

Copy a JSON response from your browser network tab, Postman or a curl call and paste it on the left. You can also upload a .json file or press Sample.

Step 2

Check the interfaces

The TS code appears on the right half a second after you stop typing. Root is the top-level type and every nested object gets its own exported interface.

Step 3

Drop them into your code

Copy the code or download types.ts, put it next to the code that calls the API, and cast the parsed response with the line shown at the top of the file.

Example: a User Response

Here is a typical user object from a REST API. Notice the long ID, the second repo that has a field the first one lacks, and a bio that is null:

{
  "id": 8901240544102066246,
  "login": "octo-dev",
  "avatar_url": "https://example.com/a.png",
  "followers": 42,
  "plan": { "name": "pro", "seats": 5 },
  "repos": [
    { "name": "api", "stars": 120, "private": false },
    { "name": "web", "stars": 14.5, "private": true, "topics": ["react", "next"] }
  ],
  "bio": null
}

And this is exactly what the converter gives you for it. topics is optional because only one repo has it, and bio is unknown because null is all the sample shows:

// Read it with: const data = JSON.parse(text) as Root;
// Some whole numbers are past 2^53, so JSON.parse rounds them (see the comments below).
// The type stays number; if every digit matters, send those values as strings instead.

export interface Root {
  /** 8901240544102066246 is past 2^53 (9007199254740991): JSON.parse reads it as 8901240544102066000. */
  id: number;
  login: string;
  avatar_url: string;
  followers: number;
  plan: Plan;
  repos: Repo[];
  bio: unknown;
}

export interface Plan {
  name: string;
  seats: number;
}

export interface Repo {
  name: string;
  stars: number;
  private: boolean;
  topics?: string[];
}

Frequently Asked Questions

How do I use the types with fetch?

Save the output as types.ts, import Root from it, and cast what you parse: const data = (await res.json()) as Root. TypeScript then knows every field, so a typo like data.usrname fails to compile. A cast does not check the response at runtime, so if the API might change, validate it too (the JSON to Zod page writes a schema that does).

Why is a field marked with ? or | null?

If a key is missing from some items of a list, it gets a ? because it may not be there. If its value is null somewhere, its type gets | null. A field can have both. A field that is null in every place your sample shows becomes unknown, because the JSON doesn't say what it holds when it isn't null.

What happens to keys like first-name or 2nd?

They stay exactly as they are in the JSON. Keys that aren't plain identifiers are written in quotes, like "first-name": string, so you read them with data["first-name"]. Nothing is renamed, which means the types always match what the API really sends.

My API returns a list. What do I get?

Root becomes a type alias such as export type Root = RootItem[], and RootItem describes one item. Every item in the list is looked at, so a field that only shows up in the third item is still in the interface.

Why does a long ID have a comment above it?

JSON.parse turns every number into a JavaScript number, which only holds whole numbers exactly up to 9007199254740991. An ID like 8901240544102066246 comes back as 8901240544102066000, so the type is still number but the comment warns you. If the digits matter, ask for the ID as a string.

Is my JSON uploaded?

No. The types are built in your browser as you type, nothing you paste or upload is sent to our server, and a reload starts with an empty editor.

Need a runtime check as well as types? Try JSON to Zod. Working from a GraphQL schema or XML instead? See GraphQL to TypeScript and XML to TypeScript.