JSON to Zod Schema Converter
FV-19Paste a JSON sample and press Convert to get a Zod schema and the TypeScript type inferred from it. Nulls and missing keys are marked. It runs in your browser.
Runs in your browser. Nothing is uploaded.
JSON
Zod
How to use it
- Paste a JSON sample into the input box, or open a .json file.
- Choose whether missing keys become optional, and whether to add format checks.
- Press Convert and copy the schema into a TypeScript file next to import { z } from "zod".
- Validate data with Root.parse(data) or Root.safeParse(data).
How the schema is built
Objects become z.object({...}) with nested objects written inline, and arrays become z.array(...) with the item schema merged from every item. Whole numbers become z.number().int() and other numbers z.number(). A value that is null in some items adds .nullable(), a key missing from some objects adds .optional() when that option is on, and mixed types become z.union([...]). The export type line gives you z.infer<typeof Root>.
Format checks
Turn on format checks and the tool inspects the strings it sees. If every value of a key matches, it adds .email(), .uuid(), .url() or .datetime({ offset: true }). A string that is only a date, such as 2024-05-01, stays z.string(), because there is no check for it here. These checks come from your sample, so a key whose sample holds only one email still gets .email().
Numbers and what it cannot know
A whole number too large for a JavaScript number is marked with a comment, because JSON.parse rounds it before Zod sees it. A key that is always null becomes z.null() and an empty array becomes z.array(z.unknown()). The schema reflects the sample only, so review it before using it to validate real input, and loosen it where the API allows other values.
Questions people ask
How do I generate a Zod schema from JSON?
Paste a representative JSON response and press Convert. Copy the output into a TypeScript file. Call Root.parse(data) to validate and get typed data, or Root.safeParse(data) to get a result object without throwing.
What is the exported type for?
The z.infer line creates a TypeScript type from the schema, so you keep one source of truth. Turn the option off if you only want the schema. The type has the same name as the schema constant.
When does a key become optional?
A key becomes .optional() when it is missing from at least one object in the sample, for example a field that appears in only some array items. Turn the option off to treat every key as required.
What is the difference between nullable and optional?
Nullable means the key is present and its value can be null. Optional means the key may be absent. The tool adds .nullable() when it sees null in the sample and .optional() when it sees the key missing.
Why is one field z.null()?
A key that is null in every item gives the sample no other type to learn from, so the schema says it must be null. Replace it with the real type, such as z.string().nullable(), once you know it.
Does it work with Zod 4?
The output uses Zod 3 method names, such as z.string().email(). Check the Zod documentation for your version, since some helpers are renamed in newer releases. Plain z.object, z.array, z.number and z.string are the same idea in both.
About this tool
Infers one z.object schema from the sample you paste, so it only knows what that sample shows. Nested objects are written inline. Whole numbers become z.number().int() and everything else z.number(); a whole number too large for a JavaScript number is marked with a comment, because JSON.parse rounds it before Zod sees it. A null in some items adds .nullable(), a key missing from some objects adds .optional() when that option is on, and mixed types become z.union([...]). A key that is null in every item becomes z.null() and an empty array becomes z.array(z.unknown()). The format checks look at the sample strings and add .email(), .uuid(), .url() or .datetime({ offset: true }) when every value of a key matches; plain dates stay z.string(). The method names are Zod 3 style.