How it works
Paste a JSON sample and get Zod schemas plus matching TypeScript types — one source of truth for both compile-time types and runtime validation:
- Every object becomes an exported
const <Name>Schema = z.object({…}), named after its property:owner→OwnerSchema. - Arrays of objects are merged into one schema named in the singular:
tasks→z.array(TaskSchema). - Keys missing from some array items become
.optional(); values that are sometimesnullbecome.nullable(). - Whole numbers become
z.number().int(). If any sample value has a decimal part, the field is plainz.number(). - Mixed values become
z.union([...]), and empty objects becomez.record(z.string(), z.unknown()). - Schemas are emitted in dependency order, so every schema is declared before it is referenced.
Example
{
"id": 42,
"owner": { "id": 7, "name": "Linh", "email": null },
"tasks": [
{ "id": 1, "done": true, "estimate": 3.5 },
{ "id": 2, "done": false, "assignee": "Minh" }
]
}
With Root name set to Project, this becomes:
import { z } from 'zod';
export const OwnerSchema = z.object({
id: z.number().int(),
name: z.string(),
email: z.null(),
});
export const TaskSchema = z.object({
id: z.number().int(),
done: z.boolean(),
estimate: z.number().optional(),
assignee: z.string().optional(),
});
export const ProjectSchema = z.object({
id: z.number().int(),
owner: OwnerSchema,
tasks: z.array(TaskSchema),
});
export type Project = z.infer<typeof ProjectSchema>;
export type Owner = z.infer<typeof OwnerSchema>;
export type Task = z.infer<typeof TaskSchema>;
If the root is an array, the elements get their own schema (RootItemSchema) and the root becomes z.array(RootItemSchema).
Options
- Root name — name of the top-level schema and type (
Project→ProjectSchema). - Export z.infer types — add
export type X = z.infer<typeof XSchema>for every schema, so you don’t maintain interfaces by hand.
Common mistakes
- Building the schema from one sample. Every field becomes required, and
safeParsefails on the first real response that omits a key. Generate from an array of several responses. - Mixing up optional and nullable. An API that sends
"email": nullneeds.nullable(); one that leaves the key out needs.optional(). Use.nullish()when both happen. - Integers that aren’t always integers.
z.number().int()rejects25.5. If a field can carry decimals, make sure the sample contains one, or drop.int().
Using the schemas
const res = await fetch('/api/projects/42');
const result = ProjectSchema.safeParse(await res.json());
if (!result.success) {
console.error(result.error.issues);
} else {
const project: Project = result.data; // fully typed
}
Refining the output
- Tighten strings: use
z.email()orz.url()(Zod 4;z.string().email()in Zod 3), orz.enum(['open', 'closed'])when you know the allowed values. - Dates stay
z.string()because JSON has no date type. Usez.iso.datetime()(Zod 4) orz.string().datetime()(Zod 3) to validate ISO timestamps, orz.coerce.date()if you wantDateobjects. - Replace
z.null()with the real type plus.nullable(). - Feed a few real responses as an array so optional fields are detected correctly.
Need plain types without runtime validation? Use JSON to TypeScript. Need a language-neutral contract instead? Try the JSON Schema Generator. If your sample isn’t valid JSON yet, fix it with JSON Repair.
Frequently asked questions
Which Zod version does the output target?
The generated code only uses long-standing Zod APIs — z.object, z.array, z.union, z.record(z.string(), …), .optional(), .nullable() and .int() — so it works with Zod 3 and Zod 4. Install it with `npm install zod`.
Why is a field `z.null()` instead of a string?
Every sample value for that field was null, so there is no way to know the real type. Replace it with the expected type, for example `z.string().nullable()`, or paste a sample where the field has a value.
What is the difference between optional and nullable here?
A key that is missing from some objects in an array gets `.optional()` (the key may be absent). A key whose values include both null and a real type gets `.nullable()` (the key is present but may be null).
Should I use .parse or .safeParse?
Use `.parse()` when invalid data is a bug and throwing is fine. Use `.safeParse()` at trust boundaries — API responses, form input, LLM output — where you want to handle the error yourself without try/catch.
Is my JSON uploaded?
No. Schema generation runs entirely in your browser — nothing is uploaded.
Last updated