JSON to Zod Schema
Generate Zod validation schemas from JSON data instantly. Handles nested objects, arrays, nullable fields, and optional properties. Runs in your browser.
- Runs in your browser
- Tool input is not uploaded to ZeroTool servers
- Free · No Sign-Up
Scan with WeChat to share this tool
Examples, details and FAQ Worked examples, how it compares with other tools, and answers to common questions.
Output Format
The generator outputs a complete TypeScript snippet including:
import { z } from "zod"— Zod importconst FooSchema = z.object({...})— Schema definitionexport type Foo = z.infer<typeof FooSchema>— Inferred TypeScript type
Common Use Cases
- API validation: Generate schemas from API response examples to validate real responses at runtime.
- Form validation: Convert a form's expected data shape into Zod schemas for use with react-hook-form or similar.
- Config files: Define and validate configuration objects loaded from JSON files.
- Type safety: Pair with TypeScript to get both runtime validation and static type inference from a single schema.
Example
With the root name User, this sample:
{
"id": 42,
"email": "alice@example.com",
"price": 19.9,
"tags": ["admin", "beta"],
"metadata": null,
"orders": [
{ "sku": "A1", "qty": 2 },
{ "sku": "B7", "qty": 1, "note": "gift" }
]
}
produces:
import { z } from "zod";
const UserSchema = z.object({
id: z.number().int(),
email: z.string(),
price: z.number(),
tags: z.array(z.string()),
metadata: z.null(),
orders: z.array(z.object({
sku: z.string(),
qty: z.number().int(),
note: z.string().optional(),
})),
});
export type User = z.infer<typeof UserSchema>;
note is optional because only one of the two orders has it. The output uses only calls that work in both Zod 3 and Zod 4. The schema constant is not exported; add export if another module needs it.
Example: A Strict Config File
Strict mode is useful for configuration, where an unknown key is usually a typo. With the root name Config and Strict mode (no extra keys) checked:
{"port":8080,"host":"localhost","db":{"url":"postgres://localhost/app","pool":10},"features":["search"]}
produces:
import { z } from "zod";
const ConfigSchema = z.object({
port: z.number().int(),
host: z.string(),
db: z.object({
url: z.string(),
pool: z.number().int(),
}).strict(),
features: z.array(z.string()),
}).strict();
export type Config = z.infer<typeof ConfigSchema>;
The nested db object is strict as well, so a misspelled poool key fails instead of being dropped without a warning.
Refining the Generated Schema
A sample shows one value per field, so the generator cannot know your rules. Check these points before you use the schema:
| Generated | Problem | Change to (Zod 4) |
|---|---|---|
email: z.string() | Any string passes | z.email() (Zod 3: z.string().email()) |
qty: z.number().int() | Correct only if the value is always a whole number. price: 10 in the sample also gives .int(), and then 10.5 fails with "expected int" | z.number() for prices; add .min(0) or .positive() |
metadata: z.null() | Only null passes | z.string().nullable() |
status: z.string() | Any string passes | z.enum(["active", "inactive"]) |
| Field present in the sample | Required, even if the API sometimes leaves it out | .optional() |
Use .parse() where a failure should stop the program (configuration at startup): it throws a ZodError. Use .safeParse() for API responses and user input: it returns { success: false, error } and you handle the error yourself.
Limits
- Types come from one sample. An empty array becomes
z.array(z.unknown()), and an empty object becomesz.record(z.string(), z.unknown()). nullbecomesz.null(). The generator does not add.nullable(). In an array of objects, a key that isnullin the first item and a string in the second becomesz.union([z.null(), z.string()]).- Mixed arrays become a union. An array such as
[{"a": 1}, 2], at the root or inside an object, givesz.array(z.union([z.number().int(), z.object({ a: z.number().int() })])). All objects in one array merge into a singlez.object, so the union has one object member. - No string formats or limits. Emails, URLs, dates, and UUIDs are all
z.string(). - Whole numbers and decimals under one key give a union. Prices such as
128.5and299in two array items becomez.union([z.number(), z.number().int()]); simplify it toz.number(). - Large integers. The input is read with
JSON.parse, so an integer above 9007199254740991 loses digits before the schema is built. - Root name. The first letter is capitalized and the name follows the ECMAScript identifier rules: Unicode letters, digits,
$and_are kept, so用户gives用户Schema; other characters are removed and the next letter is capitalized (user profilegivesUserProfileSchema); a name that cannot start an identifier gets aTprefix, so2fagivesT2faSchema. A name with nothing left usesRoot. - Very deep nesting. It can exceed the browser's call stack. The status line then shows the error, the previous schema is cleared and Copy is disabled.
- Invalid JSON. The output is cleared, Copy is disabled, and the status line gives the line, the column and the likely cause, counted in the input as you typed it:
{"port": 8080,}givesInvalid JSON: line 1, column 14, trailing comma before the closing bracket.
FAQ
What is Zod?
Zod is a TypeScript-first schema declaration and validation library. It lets you define the shape of your data with a schema, then validate data against that schema at runtime. It's widely used for API response validation, form validation, and configuration parsing.
How does the generator infer types?
The generator inspects each JSON value: strings become z.string(), booleans become z.boolean(), integers become z.number().int(), floats become z.number(), null becomes z.null(), arrays become z.array(), and objects become z.object(). For arrays of objects, keys present in only some items are marked as optional, and a key with different types in different items becomes a z.union.
What is strict mode?
In strict mode, .strict() is appended to every z.object() call, nested ones included. This causes Zod to reject input objects that contain keys not defined in the schema. Without strict mode, extra keys are silently removed from the parsed result (the default Zod behavior).
Does the output work with Zod 3 and Zod 4?
Yes. The generator uses only z.object, z.array, z.union, z.record, z.string, z.number().int(), z.boolean, z.null, z.unknown, .optional() and .strict(), which exist in both versions. The examples on this page are compiled with TypeScript in strict mode against Zod 3 and against the Zod 4 API (zod/v4). One difference: in Zod 4, z.number().int() also rejects integers outside the safe range (above 9007199254740991).
Does this generate production-ready schemas?
The generated schemas are a strong starting point. You should review and refine them — for example, adding .min()/.max() constraints on strings and numbers, converting nullable fields to .nullable().optional(), or splitting nested schemas into separate named constants.
Is my JSON data sent anywhere?
No. The page builds the schema from your JSON in your browser and does not send the JSON anywhere or save it in browser storage. The page's analytics record only a usage event (the tool name and the action generate) after you edit the input and move the focus out of it, or click Generate Zod Schema or Example, without any of the JSON or the generated code.