JSON to Types

A JSON array to a TypeScript type

Every element is merged into one type. Typing from the first element alone is the most common way to get this wrong.

Input

[
  { "id": 1, "name": "Ada" },
  { "id": 2, "name": "Grace", "admin": true }
]

An array of one object shape.

1 interface · 3 fields · 2 levels deep

Where this sample is ambiguous

  • Check this

    Optionality inferred from one sample

    `[].admin` was missing from at least one object in the sample and is therefore marked optional. A single sample cannot distinguish "sometimes omitted" from "omitted in this particular payload".

    Fix: Check these against the real schema. Absent means optional; a null value would instead mean present-and-nullable, which is a different type.

  • 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 RootItem {
  id: number;
  name: string;
  admin?: boolean;
}

export type Root = RootItem[];

Zod

import { z } from 'zod';

export const RootItemSchema = z.object({
  id: z.number(),
  name: z.string(),
  admin: z.boolean().optional(),
});

export const RootSchema = z.array(RootItemSchema);

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