JSON Schema to Zod
Convert JSON Schema or raw JSON objects into runtime-safe Zod validation schemas.
Input Structure Mode
Choose whether your JSON input is a formal JSON Schema or raw example JSON.
What is Zod and JSON Schema?
JSON Schema is a vocabulary that allows you to annotate and validate JSON documents. It is widely used in APIs, configuration definitions, and OpenAPI specs.Zod is a TypeScript-first schema validation library. Unlike typescript types (which are erased at runtime), a Zod schema validates data at runtime.
This upgraded tool converts standard JSON Schema specifications directly into ready-to-use Zod schema chains. It supports constraints like minLength, maxLength,minimum, maximum, enum selections, email/uri/uuid formats, and nested array/object schema logic. It is fully client-side and performs all validation inside your browser.
Built and maintained by Meet Shah · Last updated
What this tool is used for
- Generating a runtime validation schema from a sample payload.
- Getting a schema whose inferred type can replace a hand-written interface entirely.
- Getting the nested object and array syntax written for you.
- Creating a schema whose inferred type replaces a hand-written interface.
- Adding runtime validation to a boundary that previously only had compile-time types.
Frequently Asked Questions
- Does it read sample JSON or a JSON Schema?
- Both. A plain document has its types inferred from the values; a formal JSON Schema is translated rule by rule, which also carries across constraints that a sample cannot express.
- Which schema constraints survive the conversion?
- `minLength`/`maxLength`, `minimum`/`maximum`, `pattern`, `minItems`/`maxItems`, `enum`, and the `email`, `uuid` and `uri` formats — emitted as the matching Zod chain such as `.email()` or `.min(3)`.
- How is optionality decided?
- From the schema's `required` array — anything absent from it gets `.optional()`. When inferring from a sample there is no such information, so every key present is treated as required and you tune it afterwards.
- Why is an integer emitted as z.number().int()?
- Zod has no separate integer primitive, so the check is a refinement on a number. Inference applies it whenever a sampled value has no fractional part — which means `1.0` in your sample produces an int rule you may not want.
- How are oneOf, anyOf and allOf handled?
- `oneOf` and `anyOf` become `z.union([...])`; `allOf` becomes a chain of `.and(...)` intersections. A union is a slightly loose reading of `oneOf`, which requires exactly one branch to match rather than at least one.
- What does an empty array infer to?
- `z.array(z.unknown())`, because there is no element to sample. Give the generator one representative element and it produces the element schema, which is nearly always what you actually want.
Common errors and gotchas
- Accepting inferred `optional`, which one sample cannot determine.
- Generating a schema stricter than the API, which then rejects legitimate responses.
- Missing `passthrough` where extra keys are expected, since Zod strips unknown keys by default.
- Letting a numeric string become `z.string()` when the API means a number.
- Shipping without error messages, which makes validation failures unhelpful.