JSON to Types

JSON containing an empty array

An empty array carries no information about its elements. The output says so rather than inventing a shape.

Input

{
  "id": 1,
  "tags": [],
  "metadata": {}
}

A single object shape.

1 interface · 3 fields · 2 levels deep

Where this sample is ambiguous

  • Check this

    Empty array — element type unknown

    `tags` is empty in the sample, so there is nothing to infer an element type from. The output uses `unknown` rather than inventing a shape.

    Fix: Supply a sample where these arrays contain at least one element.

  • Check this

    Empty object — shape unknown

    `metadata` is empty in the sample. The output uses `Record<string, unknown>`, which accepts anything.

    Fix: Supply a populated example of these objects.

  • 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 {
  id: number;
  tags: unknown[];
  metadata: Record<string, unknown>;
}

Zod

import { z } from 'zod';

export const RootSchema = z.object({
  id: z.number(),
  tags: z.array(z.unknown()),
  metadata: z.record(z.string(), z.unknown()),
});

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