JSON to Zod Schema
Generate a Zod schema, plus the TypeScript type inferred from it, from a JSON sample. Arrays are merged, and keys missing from some items are marked optional.
Use the tool
Why this tool exists
Zod lets you validate data at runtime and get a TypeScript type from the same definition, but you first have to write the schema. For an API response with many nested fields, that is tedious, and it is easy to forget a field or mix up number and string.
Paste a real response and the tool builds a z.object with the right nested objects, arrays and primitives. Whole numbers use z.number().int(), null values become nullable, and keys that are absent in some array items are marked optional. The output also exports the inferred type so the schema and type cannot drift apart. A single sample cannot show every variation, so tighten the schema afterwards. Inference runs locally and nothing is uploaded.
How to use it
- Paste a JSON sample into the input box.
- Set the root name, which becomes RootSchema and the Root type.
- Copy the generated import, schema and inferred type into your project.
- Refine the schema with extra checks such as email or min length.
Frequently asked questions
What does the output contain?
An import of z from zod, an exported schema named after your root plus Schema, and an exported TypeScript type created with z.infer. You can paste all three into one file.
How are null and mixed values handled?
A field that is sometimes null becomes nullable. If the sample mixes other types, a z.union of the options is used. Empty arrays become arrays of unknown, because the item type cannot be known.
Will it add validations like email or min?
No. It describes the shape and basic types only. Add checks such as .email(), .min() or .regex() yourself, because a sample cannot tell what the real constraints are.
Which Zod version is the output for?
It uses basic constructs such as z.object, z.array, z.union and z.string that work across common Zod releases. Check unusual cases against the version you use.