JSON → Zod Schema
Generate Zod validation schemas from JSON — nested objects, unions, and optional fields. 100% local.
Generated Schema Structure
The Zod schema generator produces a runnable z.object(...) schema that validates the same shape as your JSON sample. Each nested object becomes a named z.object variable. The generated code can be dropped directly into a TypeScript file and used with parse() or safeParse().
{
"user": {
"id": 1,
"name": "Alice",
"email": "[email protected]",
"role": "admin",
"tags": ["typescript", "zod"],
"address": {
"city": "London",
"country": "UK"
},
"metadata": null
},
"total": 42,
"active": true
}import { z } from 'zod';
export const AddressSchema = z.object({
city: z.string(),
country: z.string(),
});
export const UserSchema = z.object({
id: z.number(),
name: z.string(),
email: z.string(),
role: z.string(),
tags: z.array(z.string()),
address: AddressSchema,
metadata: z.null(),
});
export const RootSchema = z.object({
user: UserSchema,
total: z.number(),
active: z.boolean(),
});Zod Type Mapping
| JSON value | Zod schema | Usage |
|---|---|---|
| String | z.string() | Any string |
| Number | z.number() | Integer or float |
| Boolean | z.boolean() | true / false |
| null | z.null() | Null only — usually change to z.nullable(z.string()) etc. |
| Array of strings | z.array(z.string()) | Typed array |
| Mixed array | z.array(z.union([...])) | Union of element types |
| Object | z.object({...}) | Nested schema |
| Empty array | z.array(z.unknown()) | Inferred as unknown |
Customizing Generated Schemas
The generated schema is a starting point. Common manual adjustments after generation:
Change z.null() to z.nullable(z.string()) for fields that can be string or null. Add .optional() for fields that might be absent: z.string().optional(). Add .min(1) for required non-empty strings: z.string().min(1). Add .email() for email fields: z.email(). Add .url() for URL fields: z.url(). Add .int() for integer-only numbers: z.number().int().
Try It
Frequently Asked Questions
Your files stay on your computer. Dad promised.