JSON to Types

A JSON array with mixed types

Arrays holding more than one type become a union of everything observed, not just whatever came first.

Input

{
  "values": [1, "two", true, null]
}

A single object shape.

1 interface · 1 fields · 2 levels deep

Where this sample is ambiguous

  • Note

    Mixed types merged into a union

    `values[]` contained more than one type, so the output is a union of everything observed. Note that typing from the first element alone — the usual shortcut — would have produced a type that rejects the rest of this sample.

  • 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 {
  values: (number | string | boolean | null)[];
}

Zod

import { z } from 'zod';

export const RootSchema = z.object({
  values: z.array(z.union([z.number(), z.string(), z.boolean()]).nullable()),
});

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