JSON to Zod Schema Generator
Generate Zod validation schemas and TypeScript types from sample JSON. Handle nested objects, arrays, optional fields and null values. Everything runs in your browser.
Your text never leaves your browser.
Generation options
Used exactly as typed. The inferred type is named RootSchemaType.
Generated Zod schema
Paste a JSON object, array or API response, or load the sample. The Zod schema appears here as you type.
Summary and inference notes
- Status
- —
- Root
- —
- Optional
- —
- Properties
- —
Waiting for input: paste JSON or load the sample.
Sample-based schemas are a starting point, not an authoritative API contract. String formats, number constraints and enums are never guessed.
How to convert JSON to a Zod schema
- Paste a real JSON sample, such as an API response, into the Sample JSON editor.
- Optionally rename the schema, toggle the inferred TypeScript type or enable strict object validation.
- Review the inference notes, then copy the generated code into a file in your project.
- Tighten the schema by hand where you know more than the sample shows, such as formats or enums.
The generator parses your JSON strictly, walks every value and records the type it observes. Objects become z.object(), arrays become z.array(), and strings, numbers, booleans and null map to z.string(), z.number(), z.boolean() and z.null(). Property order follows your sample, and every key is written as a quoted string so names like "user-id" stay valid.
Worked example
Two user records are merged into one item schema. name appears in both records but is null in one, so it is nullable. active is missing from the second record, so it is optional.
{
"users": [
{ "id": 1, "name": "Ada", "active": true },
{ "id": 2, "name": null }
]
}import { z } from "zod";
export const RootSchema = z.object({
"users": z.array(z.object({
"id": z.number(),
"name": z.string().nullable(),
"active": z.boolean().optional(),
})),
});
export type RootSchemaType = z.infer<typeof RootSchema>;Arrays, optional and nullable properties
All objects in an array are merged recursively into a single item schema, including nested objects and arrays at the same path. A key missing from some records gets .optional(); a key present in every record stays required. A value seen as both null and another type gets .nullable(). These are different rules: optional means the key may be absent, nullable means the key is present but may hold null. A field that is only ever null stays z.null() and is never made optional.
Arrays with mixed primitive types become a union in first-observed order, so [1, "x"] becomes z.array(z.union([z.number(), z.string()])). An empty array becomes z.array(z.unknown()), but an empty array in one record never widens items seen in another record.
What cannot be inferred reliably
A sample shows one response, not the API contract. Fields that never appear cannot be added, and fields present in every record may still be optional in reality. Strings are never narrowed to emails, URLs, UUIDs, dates, regexes, literals or enums, and numbers are never narrowed to integers or ranges. Defaults and coercion are not added. Add those rules yourself, for example z.email(), z.int() or z.enum(["admin", "editor"]). Inputs are limited to 256 KiB, 50,000 values and 64 levels of nesting; larger samples are rejected rather than truncated. Numeric tokens are used only to infer z.number(), never copied as rounded sample values. JavaScript may round large integers or tiny decimals; exponents beyond its finite range produce an explicit warning because Zod 4 rejects infinity. The generated schema is not executed here and does not validate your sample. Keys named __proto__ are emitted with safe computed syntax, but Zod 4 object parsing may strip them and skip their validation. An inference note flags this limitation so you can rename the key if needed.
Using the schema with Zod 4
Install Zod with npm install zod, save the output as a module, then validate untrusted data at runtime with parse(), which throws, or safeParse(), which returns a result object. By default z.object() strips unknown keys; strict mode emits z.strictObject(), which rejects them and requires Zod 4.
import { RootSchema } from "./schema";
const response = await fetch("/api/users");
const result = RootSchema.safeParse(await response.json());
if (!result.success) {
console.error(result.error.issues);
} else {
result.data.users[0].id; // number
}Zod schema vs TypeScript type vs JSON Schema
A TypeScript type, like the output of the JSON to TypeScript Converter, exists only at compile time and checks nothing when data arrives. A Zod schema is runtime code that validates data, and z.infer gives you the matching static type from the same source. A JSON Schema is a language-neutral JSON document interpreted by a validator; test one with the JSON Schema Validator. This tool generates Zod code from sample JSON; it does not convert JSON Schema documents.
Privacy and local processing
Parsing, inference and code generation run entirely in your browser. Your JSON is never evaluated, the generated code is never executed and sample values are never copied into the schema. Nothing is uploaded, logged, stored, placed in the URL or sent to analytics.
Related JSON tools
Frequently asked questions
Which version of Zod does the generated code target?
The output targets Zod 4 and imports z from "zod". Basic builders such as z.object, z.array and z.union also exist in Zod 3, but strict mode uses z.strictObject, which is Zod 4 syntax. Install a current version with npm install zod.
Why is a field z.null() instead of z.string().nullable()?
If a field is null everywhere in your sample, null is the only type the generator can prove. Replace it with the real type, for example z.string().nullable(), or paste a sample where the field has a value.
What is the difference between .optional() and .nullable()?
.optional() means the key may be missing from the object entirely. .nullable() means the key is present but its value may be null. The generator adds .optional() only when a key is missing from some objects in a merged array, and adds .nullable() only when it observed a null alongside another type.
Why does an empty array become z.array(z.unknown())?
An empty array contains no items, so there is nothing to infer an item type from. z.unknown() accepts any item. Replace it with the real item schema, or include at least one item in your sample. If the same array has items in other records, those items are used instead.
Does it detect emails, UUIDs, dates or enums?
No. One example value cannot prove a format or a closed set of values, so every string becomes z.string() and every number becomes z.number(). Add refinements such as z.email(), z.uuid(), z.iso.datetime(), z.int() or z.enum() yourself once you know the real contract.
What happens to properties that are not in the schema?
By default z.object() accepts unknown keys and strips them from the parsed result. With strict object validation enabled, every object uses z.strictObject(), which rejects input containing keys the schema does not list.
How is a Zod schema different from a TypeScript type or JSON Schema?
A TypeScript type exists only at compile time and checks nothing at runtime. A Zod schema is JavaScript code that validates data at runtime, and z.infer derives a matching TypeScript type from it. JSON Schema is a language-neutral JSON document that a separate validator such as Ajv interprets.