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.
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 value | TypeScript | Zod |
|---|---|---|
| "text" | string | z.string() |
| 42 or 1.5 | number | z.number() |
| true | boolean | z.boolean() |
| null | null | z.null() |
| { "b": 1 } | A named type, such as RootA | z.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 elements | n?: string | z.string().optional() |
| String in one element, null in another | string | null | z.string().nullable() |
04 · Use cases
When to use the JSON to TypeScript Types
- 01
Generate types for a third-party API you only have a sample response from
- 02
Bootstrap a Zod schema from a JSON fixture
- 03
Convert an inherited JSON blob into proper TypeScript before refactoring
- 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 01export interface Root { id: number; name: string; active: boolean; }
One interface inferred from a single object.
{ "items": [{ "id": "a" }, { "id": "b", "label": "B" }] }
ex 02export 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 03import { 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 04export 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?
How are nested types named, and can I rename them?
What's the difference between an interface and a type alias?
When should I use Zod or Valibot instead of TypeScript types?
Does it narrow values to literal types or enums?
How are null values handled?
What happens with invalid JSON or unusual keys?
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.
- RFC 8259: The JSON Data Interchange Format
IETF. The definition of JSON that the tool's error checking follows.
- TypeScript everyday types
Microsoft. Reference for the interface and type-alias forms this tool emits.
- Zod
Zod. Documentation for the Zod schema output, including z.infer.
- Valibot
Valibot. Documentation for the Valibot schema output, including InferOutput.
08 · More
Tools that pair with JSON → Types
Regex GeneratorAI-powered
Describe what you want to match in plain English and get a JavaScript regex, with a live tester and a token-by-token breakdown.
UUID Generator
Generate UUID v4, v7 or v1, one at a time or up to 1,000. Output as standard, uppercase, braces (GUID), no hyphens, URN or Base64, and decode any UUID you paste.
Lorem Ipsum Generator
Generate lorem ipsum placeholder text by paragraphs, sentences or words. Classic Latin or corporate, hipster and bacon variants. Copy or download as .txt.