Skip to content

JSON to TypeScript Types

Paste JSON and get TypeScript interfaces, type aliases, a Zod schema or a Valibot schema. Updates as you type, in your browser.

Output
Output · 20 lines
export interface Root {
  id: number;
  user: RootUser;
  orders: RootOrder[];
  active: boolean;
  createdAt: string;
}

export interface RootUser {
  name: string;
  email: string;
  tags: string[];
}

export interface RootOrder {
  sku: string;
  qty: number;
  note?: null;
  gift?: boolean;
}

01 · Definition

How do you convert JSON to TypeScript?

To convert JSON to TypeScript, give every key a property typed after its value: a string becomes string, a number number, true or false boolean, an object its own named interface, and an array its element type followed by []. This tool infers the types from a pasted sample and can also write a Zod or Valibot schema.

02 · Overview

How the JSON to TypeScript Types works

Convert a JSON sample into TypeScript types or a runtime schema. Choose interface, type alias, Zod or Valibot output and set the root type name. Nested objects become their own named types. For arrays of objects, a key missing from some elements is marked optional and differing value types become a union. Two options make properties readonly and type null-only fields as unknown. Invalid JSON is reported with a line and column. The conversion runs in the page; your JSON is not sent to our server.

03 · Guide

JSON to TypeScript, Zod and Valibot type mapping

The table shows what this tool writes for each kind of JSON value. It was produced by running the tool's own inference code on one sample per row. The Valibot output is the same as the Zod column with v in place of z, except that optional and nullable wrap the type: v.optional(v.string()) and v.nullable(v.string()).

JSON has one number type, so integers and decimals are both number. Dates arrive as strings and stay string. Values are never narrowed to literals or enums.

JSON valueTypeScriptZod
"text"stringz.string()
42 or 1.5numberz.number()
truebooleanz.boolean()
nullnullz.null()
{ "b": 1 }A named type, such as RootAz.object({ b: z.number() })
["x", "y"]string[]z.array(z.string())
[]unknown[]z.array(z.unknown())
["x", 1](string | number)[]z.array(z.union([z.string(), z.number()]))
Key missing from some array elementsn?: stringz.string().optional()
String in one element, null in anotherstring | nullz.string().nullable()
What each JSON value becomes

04 · Use cases

When to use the JSON to TypeScript Types

  1. 01

    Generate types for a third-party API you only have a sample response from

  2. 02

    Bootstrap a Zod schema from a JSON fixture

  3. 03

    Convert an inherited JSON blob into proper TypeScript before refactoring

  4. 04

    Add runtime validation to a route that previously trusted unknown input

05 · Examples

JSON to TypeScript Types examples

{ "id": 1, "name": "Alex", "active": true }

ex 01

export interface Root { id: number; name: string; active: boolean; }

One interface inferred from a single object.

{ "items": [{ "id": "a" }, { "id": "b", "label": "B" }] }

ex 02

export interface Root { items: RootItem[]; } export interface RootItem { id: string; label?: string; }

Array elements are merged into one type. label is missing from the first element, so it is optional.

{ "id": 1, "name": "Alex" } as Zod

ex 03

import { z } from "zod"; export const RootSchema = z.object({ id: z.number(), name: z.string(), }); export type Root = z.infer<typeof RootSchema>;

A Zod schema plus the static type inferred from it.

{ "tags": ["a", 1, null] }

ex 04

export interface Root { tags: (string | number | null)[]; }

Mixed array values become a union.

06 · FAQ

JSON → Types: frequently asked questions

How does the tool decide whether a field is optional?

It compares the objects inside an array. A key that is missing from at least one element is marked optional with ?. Keys in a single object are always required, because one sample cannot show that a key is sometimes absent. To get optional fields, paste an array with several representative objects.

How are nested types named, and can I rename them?

Each nested object becomes its own type, named after its parent and its key: the user object inside Root is RootUser. Objects inside an array use the singular of the key, so orders gives RootOrder. Set the root name in the Root type name field and every nested name follows it. Individual nested types cannot be renamed in the tool; rename them in your editor after copying.

What's the difference between an interface and a type alias?

For object shapes they are nearly interchangeable. Interfaces can be extended by declaration merging; type aliases can also name unions and primitives. If the JSON root is an array or a primitive, the tool emits a type alias in both modes, because an interface cannot describe those.

When should I use Zod or Valibot instead of TypeScript types?

Types are erased at build time, so they cannot check data at runtime. Use a Zod or Valibot schema where data enters your program from outside: HTTP requests, third-party API responses, environment variables. Both outputs also export the static type inferred from the schema, so you do not write it twice.

Does it narrow values to literal types or enums?

No. A value of "success" is typed as string and 200 as number. One sample cannot show which other values are possible, so the tool always uses the primitive type. Narrow to a union of literals by hand where you know the full set.

How are null values handled?

A field that is null in the sample is typed as null, since the sample gives no other information. If the same key holds a string in one array element and null in another, it becomes string | null (z.string().nullable() in Zod). Tick "Type null-only fields as unknown" to emit unknown instead of null for fields that were only ever null.

What happens with invalid JSON or unusual keys?

Invalid JSON shows an error with the line and column of the first problem, such as a trailing comma or single quotes. Keys that are not valid identifiers, like first-name or 2fa, are emitted in quotes so the output still compiles. An empty array is typed as unknown[].

07 · Reference

Specs and further reading

These are the primary sources this tool follows. Where a specification defines the behaviour, we link the specification, not a summary of it.

08 · More

Tools that pair with JSON → Types

Last updated .