How to Generate TypeScript Types from JSON (and Refine Them)

Writing TypeScript types for an API response by hand is slow and error-prone. You copy a sample, type out every field, guess which ones are optional, and miss a nested property three levels down. Generating types from real JSON does the tedious part in seconds. Then you spend a few minutes turning “what this one sample looked like” into “what the API actually promises”.

This guide shows both halves: generating the types, and refining them so you can trust them.

The quick answer

  1. Copy a real response from your API: browser DevTools → Network → the request → Response.
  2. Paste it into Doetra’s JSON to TypeScript converter.
  3. Copy the generated interfaces into your project, e.g. types/api.ts.
  4. Refine them: optional fields, nulls, string unions, better names.
  5. If the data comes from outside your code, validate it at runtime too, with Zod or JSON Schema.

Step by step

1. Get a good sample

The generated types can only be as good as the sample. Use a real response rather than one you typed, and if you can, pick one with as many fields filled in as possible. For list endpoints, include several items, since one item may be missing fields others have.

2. Generate

Open doetra.com/json-to-csv/json-to-typescript, paste the JSON or drop the file, and choose TypeScript as the output. If the JSON has a syntax error, Doetra points to the line. Everything runs in your browser, so responses with internal data or tokens aren’t uploaded.

3. Paste into your project

Keep generated API types in one place, such as src/types/api.ts, so they’re easy to find and regenerate when the API changes.

Example

A typical response for a blog post endpoint:

{
  "id": 903,
  "title": "How to Convert JSON to CSV",
  "status": "publish",
  "author": { "id": 1, "name": "Doetra Team" },
  "tags": ["json", "csv"],
  "featured_image": null,
  "published_at": "2026-11-15T13:24:00Z"
}

Generated types will look roughly like this:

export interface Post {
  id: number;
  title: string;
  status: string;
  author: Author;
  tags: string[];
  featured_image: null;
  published_at: string;
}

export interface Author {
  id: number;
  name: string;
}

That’s a solid start: nested objects got their own interface, and the list became string[]. But a single sample can’t tell the generator everything, which is where refining comes in.

What to refine (and why)

Nulls that are sometimes values

featured_image: null only tells the generator that this sample had no image. Posts with an image will have a number or object there. Change it to the real type plus null:

featured_image: number | null;

Optional fields

If a field is missing from some responses, mark it optional with ?. Check the API docs, or compare a few samples:

excerpt?: string;

String unions instead of plain strings

When a field has a fixed set of values, a union catches typos and makes your editor’s autocomplete useful:

status: "publish" | "draft" | "pending" | "private";

Dates

JSON has no date type, so dates arrive as strings. Keep the type as string and convert with new Date(post.published_at) where you need it. Typing it as Date would be a lie: the value is still a string until you convert it.

Better names

Generated names come from field names, so you may get Author, Author2 or Item. Rename them to match your domain (PostAuthor, OrderLine) and reuse one type where two are identical.

Arrays of mixed objects

If list items have different shapes, for example a feed with posts and videos, model them as a union with a shared field to tell them apart:

type FeedItem =
  | { type: "post"; id: number; title: string }
  | { type: "video"; id: number; duration: number };

Types vs runtime validation

TypeScript types vanish when your code is compiled. They describe what you expect the API to return; they don’t check what it actually returns. If the API changes, or a third-party service sends something odd, your types will still say everything is fine until something breaks in production.

For data from outside your code (APIs, webhooks, form posts, files), add a runtime check:

  • Zod. A popular TypeScript library: write a schema once and get both the runtime check and the type. Doetra can generate a Zod schema from the same JSON. Choose Zod as the output instead of TypeScript.
  • JSON Schema. A language-neutral standard, useful when backend and frontend are in different languages or the API already publishes one. See our beginner’s guide to JSON Schema; Doetra can generate one from sample data too.
import { z } from "zod";

const Post = z.object({
  id: z.number(),
  title: z.string(),
  status: z.enum(["publish", "draft", "pending", "private"]),
  featured_image: z.number().nullable(),
});

type Post = z.infer<typeof Post>;

const post = Post.parse(await res.json()); // throws if the shape is wrong

Keeping types up to date

  • When an API version changes, grab a fresh sample and regenerate, then re-apply your refinements. Keeping them in one file makes the diff easy to review.
  • If the API publishes an OpenAPI spec, a spec-based generator can produce types for every endpoint at once. Sample-based generation is best for APIs without a spec, quick prototypes, and webhooks.

Frequently asked questions

How do I convert JSON to TypeScript for free?

Paste the JSON into Doetra and choose TypeScript as the output. It’s free, with no sign-up, and the data isn’t uploaded.

Interface or type?

Both work for object shapes. Interfaces can be extended and merged; type aliases can express unions. Many teams use interfaces for objects and types for unions. Pick one convention and stay consistent.

Why is a field typed as null or any?

The sample had null or an empty list there, so the generator couldn’t see the real type. Use a sample where the field is filled in, or set the type by hand.

Can it handle large responses?

Yes, files up to 25 MB. For very large lists, a few representative items usually produce cleaner types than thousands.

Try it: generate TypeScript types from your JSON →

Scroll to Top