JSON to Types

An API response to TypeScript

The common paginated-envelope shape: a wrapper object with a data array and page metadata.

Input

{
  "data": [
    { "id": "u_1", "email": "a@example.com", "verified": true },
    { "id": "u_2", "email": "b@example.com", "verified": false }
  ],
  "page": { "cursor": "abc", "hasMore": true, "total": 128 }
}

An object with 2 nested shapes.

3 interfaces · 8 fields · 3 levels deep

Where this sample is ambiguous

  • Note

    Whole numbers are still `number`

    Every numeric value in the sample was a whole number, but JSON has a single number type and cannot express the difference. The output uses `number`.

    Fix: If a field is genuinely an integer, tighten it by hand: `z.number().int()`.

TypeScript

export interface Root {
  data: Data[];
  page: Page;
}

export interface Data {
  id: string;
  email: string;
  verified: boolean;
}

export interface Page {
  cursor: string;
  hasMore: boolean;
  total: number;
}

Zod

import { z } from 'zod';

export const PageSchema = z.object({
  cursor: z.string(),
  hasMore: z.boolean(),
  total: z.number(),
});

export const DataSchema = z.object({
  id: z.string(),
  email: z.string(),
  verified: z.boolean(),
});

export const RootSchema = z.object({
  data: z.array(DataSchema),
  page: PageSchema,
});

Convert your own JSON

Paste a sample on the home page, or call the API or MCP server. This page is also available as markdown.

Related examples