Generate JSON Schema from Zod
How Zod to JSON Schema conversion works, what survives and what is lost, which drafts consumers expect, and when a hand-written schema or plain Zod is the better choice.
Converting a Zod schema into JSON Schema lets a validation contract written in TypeScript be read by tools that do not run TypeScript, such as API documentation generators, form builders, other-language services and model APIs that accept structured-output definitions. The conversion is useful, but it is not lossless. This review explains what the conversion actually produces, what it drops, and how to decide whether the generated schema is good enough for your purpose.
Clarifying what "Zod to JSON" means
Searches for "zod to json" can mean two different things. One is serializing data that has already passed Zod validation, which is ordinary JSON serialization and does not involve schemas at all. The other, and the subject of this article, is translating the schema itself into a JSON Schema document that describes the shape of valid data. A third, related task runs in the opposite direction: generating Zod code from an existing JSON Schema. That reverse direction uses separate tools and has its own fidelity problems, so it is not covered here.
Is the conversion suitable for your use?
The conversion is a good fit when Zod is your single source of truth and you need a portable description of structure: field names, primitive types, required versus optional keys, enums, unions, array and string constraints, and descriptive text. The main benefit is that documentation and external contracts stay in step with the code that actually validates input.
The decisive tradeoff is that JSON Schema can only express declarative rules. Anything in your Zod schema that depends on running JavaScript, such as custom refinements, transforms or preprocessing, either disappears from the output or causes the conversion to fail, depending on the tool and its settings. A consumer validating against the generated schema can therefore accept data that your Zod schema would reject.
Plain alternatives are preferable in a few situations. If the schema is consumed only inside a TypeScript codebase, keep using Zod directly; a JSON Schema copy adds a second artifact without adding validation. If an external contract must be precise, versioned and reviewed by people who do not read TypeScript, a hand-maintained JSON Schema may be easier to control than generated output. If your Zod schemas lean heavily on refinements and transforms, the generated document may describe so little of the real rules that it misleads more than it helps.
How the conversion is done
Zod 4 includes a built-in conversion function, z.toJSONSchema. Projects still on Zod 3 have typically used the community package zod-to-json-schema, which exposes a zodToJsonSchema function. The two differ in defaults and options, so output from one should not be assumed to match the other. The snippets below are illustrative and were not executed for this article; confirm option names and defaults against the documentation for the version you have installed.
// Zod 4: built-in conversion
import { z } from "zod";
const User = z.object({
name: z.string().min(1).describe("Display name"),
email: z.email(),
age: z.number().int().optional(),
});
const userSchema = z.toJSONSchema(User);
// Choosing a target dialect and handling unsupported types
const draft7Schema = z.toJSONSchema(User, {
target: "draft-7",
unrepresentable: "any",
});
// Zod 3: community package
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
const User = z.object({
name: z.string().min(1),
age: z.number().int().optional(),
});
const userSchema = zodToJsonSchema(User, "User");
For a schema like the one above, you should expect an object-type schema with a properties entry for each field, a required list containing the non-optional keys, a string length constraint for name, an integer type for age, a format annotation for email and a description carried over from describe. The exact keywords, the presence of a $schema identifier, how unknown keys are treated and whether definitions are nested or referenced vary by tool, version and target. Inspect the generated document rather than assuming a particular shape.
What the conversion changes and what it loses
The conversion does not alter your Zod schema or your data. It produces a new, separate JSON document that describes a subset of the rules. The question to ask is which rules made it across.
| Zod feature | Typical outcome in JSON Schema |
| Primitive types, objects, arrays | Translated directly |
| Optional and required keys | Translated through the required list |
| Enums and literals | Translated as enum or const values |
| Unions and discriminated unions | Translated as anyOf or oneOf; discriminator intent may not be explicit |
| String length, number ranges, array size | Translated as standard constraint keywords |
| String formats such as email or uuid | Translated as format annotations, which many validators do not enforce by default |
| Regex patterns | Translated as pattern, but JavaScript regex features may not match other engines |
| Descriptions and metadata | Carried over as description or similar annotation keywords |
| Recursive or reused schemas | Expressed with references and definitions, if the tool supports them |
| Custom refinements | Lost; the logic cannot be represented |
| Transforms and preprocessing | Lost or rejected; input and output shapes may differ |
| Defaults | Represented, but whether they describe input or output depends on settings |
| Dates, bigint, Map, Set, symbols | Not representable as JSON; converted loosely, dropped or rejected |
| Custom error messages | Lost |
| Branded types | The brand is erased; only the underlying type remains |
Two items in that table deserve emphasis. First, the format keyword: in recent JSON Schema drafts, format is treated as an annotation unless a validator is configured to assert it, so a field marked as an email may pass validation with any string. Second, schemas with transforms have different input and output shapes. Zod 4 lets you choose which side the generated schema describes; a documentation consumer usually wants the input shape, while a schema describing returned data may want the output shape. Choosing the wrong side produces a schema that is internally consistent but describes the wrong data.
Will the generated schema stay usable after sharing?
The output is plain JSON, so copying, committing or pasting it does not corrupt it in the way styled text can be corrupted. The practical risks are about compatibility and drift, not about characters.
- Dialect mismatch: JSON Schema has several drafts, and OpenAPI 3.0 uses its own schema variant that differs from later drafts, for example in how nullable values are expressed. OpenAPI 3.1 aligns much more closely with modern JSON Schema. Generate for the dialect your consumer actually documents.
- Partial support in consumers: some services that accept JSON Schema, including structured-output features in model APIs, support only a subset of keywords or impose extra requirements on object schemas. Check the destination's documentation and test the specific schema; one successful upload does not establish compatibility for other schemas or other services.
- Regex portability: patterns are copied as written, and an expression that works in JavaScript can behave differently or fail in another language's regex engine.
- Drift after hand edits: if someone edits the generated file directly, the next regeneration overwrites those edits or the two diverge silently. Treat generated output as a build artifact and change the Zod source instead.
- Readability: generated schemas can be verbose, and automatically named definitions may be hard for people to follow. Giving reused schemas explicit identifiers or titles, where your tool supports it, makes the output easier to review.
Accessibility in the usual sense of screen readers and reading order is not directly at stake, because the output is a data file rather than displayed text. Descriptions do matter indirectly: documentation viewers and form generators often show description and title values as field labels and help text, so the wording you put in describe or metadata calls becomes reader-facing text downstream.
A practical checking routine
- List every refine, superRefine, transform and preprocess call in the schema, and decide whether losing each one is acceptable for the consumer.
- Set the target dialect explicitly instead of relying on a default.
- Decide whether the schema should describe input or output when transforms or defaults are involved.
- Decide how unsupported types should be handled: failing the conversion is safer during development, while loose fallbacks can hide gaps.
- Validate a few known-good and known-bad payloads against the generated schema with the same validator your consumer uses, and compare the results with Zod's own verdicts.
- Regenerate the schema in your build or test pipeline so it cannot fall out of date.
Gaps in this review
No version-specific test results, benchmark figures or verified outputs were available for this article. Behavior described here reflects the documented design of the tools and general JSON Schema principles, not measured observations. Defaults and option names change between releases, so the documentation for your installed version and a test against your actual consumer remain the deciding evidence.
Bottom line
Generating JSON Schema from Zod is worthwhile when you need a shareable, structural description of data and want it to track your code automatically. It is weakest where your real validation lives in custom logic. Keep Zod as the authority for enforcement, treat the generated schema as a description of structure, and verify it against the specific tool that will read it.