How to Create TypeScript Types from JSON?

You got some JSON from an API. Now you want your editor to know what is inside it. That is what TypeScript types are for. A type describes the shape of your data. It says which keys exist, and what kind of value each key holds.

This guide shows you how to write types for JSON by hand. It also shows how to deal with the tricky cases. And it shows how to make types in a few seconds with a tool.

Quick answer: Write an interface that lists each key in your JSON and its type. Or paste a JSON sample into a generator that writes the interfaces for you. Types only help while you write code. So check real data while the app runs, with a schema or a validation library.

Why add types to JSON?

In TypeScript, JSON.parse() returns the type any. So does response.json() when you use fetch. The type any means “this can be anything”. So your editor can’t warn you about a typo like user.nmae. It also can’t suggest keys as you type.

Once you give the data a proper type, you get:

  • Autocomplete for every key, even deep inside nested objects (objects inside other objects).
  • An error when you spell a key wrong or use a value the wrong way.
  • Safer changes. When the API changes, you update the type. Then the compiler (the tool that checks and builds your code) shows every place that breaks.

How do you write a TypeScript type for JSON?

Start with a real sample of the data:

{
  "id": 42,
  "name": "Ada Lovelace",
  "active": true,
  "address": { "city": "London", "country": "UK" },
  "tags": ["admin", "beta"]
}

Each JSON value matches a TypeScript type. Strings become string, numbers become number, true and false become boolean, and null stays null. An array of strings is string[]. A nested object gets its own interface.

interface Address {
  city: string;
  country: string;
}

interface User {
  id: number;
  name: string;
  active: boolean;
  address: Address;
  tags: string[];
}

Now you can use it:

const user: User = await response.json();
console.log(user.address.city); // autocomplete works here

Be careful: this line does not check anything. You are telling TypeScript “trust me, the data looks like this”. We will come back to that below.

Should you use interface or type?

Both can describe a JSON object. For plain data, they work almost the same. You can grow an interface with extends. You can also declare it again to add more fields.

A type alias is written like type User = { … }. It can also name things an interface can’t. One example is a union like string | number. For JSON, either is fine. Pick one style and use it in your whole project.

How do you handle optional fields?

Some keys are not always there. Put a ? after the key name to mark it as optional. This is different from a key that is always there but can be null:

interface User {
  id: number;
  nickname?: string;        // the key may be missing
  deletedAt: string | null; // the key is always there, the value may be null
}

Real APIs sometimes do both. In that case write nickname?: string | null.

What if a field can hold different types?

Use a union type. A union means “one of these”. You write it with a | between the options:

interface Product {
  id: number | string;
  status: "active" | "paused" | "deleted";
}

The second line is a union of fixed text values. It is very useful, but a generator can’t guess it. It only sees the values in your sample, so it writes string. You add the list of allowed values yourself.

How do you type an array of objects?

Many APIs return a list at the top level:

[
  { "id": 1, "title": "Buy milk", "done": false },
  { "id": 2, "title": "Call Sam", "done": true, "note": "after 5pm" }
]

Write one interface for a single item. Then add [] to mean “a list of these”:

interface Todo {
  id: number;
  title: string;
  done: boolean;
  note?: string; // only some items have it
}

type TodoList = Todo[];

If an array holds mixed values, use a union inside brackets: (string | number)[]. What if the array is empty in your sample? Then nobody can tell what goes in it. You will need to fill in the item type yourself.

How to generate TypeScript types from JSON automatically

Writing types by hand is fine for small objects. For a big API response, let a tool do the boring part. With the JSON to TypeScript generator:

  1. Paste a sample that is typical of your data, such as a real API response.
  2. Set the name of the root type. Choose interfaces or type aliases.
  3. Copy the code or download it as a file.

The tool works out nested types, arrays and optional fields for you. A key that only some array items have becomes optional. A field that holds different types in different items gets a union.

Some keys are not valid names in code, such as "user-name". The tool puts quotes around them, so they still work.

If the tool can’t read your sample, the JSON probably has a syntax error. Paste it into the JSON Validator to find the exact line. Or see the most common JSON errors.

Why generated types are only a starting point

A generator can only see the sample you give it. It can’t know:

  • About fields that were missing from your sample.
  • What type a field has when it happened to be null in every item.
  • The allowed values of a field like status.
  • That "2026-11-23" is a date. JSON has no date type, so it stays a string.
  • That a very large ID may lose digits in JavaScript, whatever the type says. See big numbers in JSON.

So read the output. Give the types clear names. Make them stricter wherever you know more than the sample shows. A sample with several different items gives a much better result than a single one.

Types do not check your data at runtime

This is the part many people miss. “Runtime” means the time when your app is running. TypeScript types are removed when your code is turned into JavaScript. So when the app runs, the types are gone.

If the API suddenly sends "id": "42" instead of 42, nothing stops it. Your code just breaks later, somewhere else.

To check data while the app runs, you need validation. Here are two common ways:

  • JSON Schema. A schema is a JSON document that describes the rules your data must follow. You can make one from the same sample with the JSON Schema Generator. Then add your own rules, and check data against it in your app. Read what JSON Schema is to learn more.
  • A validation library. Libraries such as Zod let you write the rules in TypeScript. You then get the type from those rules. So the rules and the type always match.

A good habit is to check data where it enters your app: API responses, uploaded files and user input. After that check, you can trust your types.

Common questions

Can TypeScript import a JSON file directly?

Yes. Turn on resolveJsonModule in tsconfig.json. Then import data from "./data.json" works, and the type is taken from the file. This only works for files you have when you build. It does not work for data you fetch later.

Does JSON.parse check my types?

No. It returns any. The type you write is only a promise you make to the compiler. Nothing is checked while the code runs.

Should I use unknown instead of any?

Yes, if you plan to validate the data. With unknown, TypeScript makes you check a value before you use it. That is exactly what you want for data from outside your app.

What is the difference between an optional field and null?

name?: string means the key may be missing. name: string | null means the key is always there, but its value may be null.