JSON Schema
New — Zod 4 introduces a new feature: native JSON Schema conversion. JSON Schema is a standard for describing the structure of JSON (with JSON). It's widely used in OpenAPI definitions and defining structured outputs for AI.
To convert a Zod schema to JSON Schema, use the z.toJSONSchema() function.
All schema & checks are converted to their closest JSON Schema equivalent. Some types have no analog and cannot be reasonably represented. See the `unrepresentable' section below for more information on handling these cases.
String formats
Zod converts the following schema types to the equivalent JSON Schema format:
These schemas are supported via contentEncoding:
All other string formats are supported via pattern:
Numeric types
Zod converts the following numeric types to JSON Schema:
Nullability
Zod converts both undefined/null to { type: "null" } in JSON Schema.
Similarly, optional and nullable are made nullable via oneOf:
Configuration
A second argument can be used to customize the conversion logic.
Below is a quick reference for each supported parameter. Each one is explained in more detail below.
target
To set the target JSON Schema version, use the target parameter. By default, Zod will target Draft 2020-12.
metadata
If you haven't already, read through the Metadata and registries page for context on storing metadata in Zod.
In Zod metadata is stored in registries. Zod exports a global registry z.globalRegistry that can be used to store common metadata fields like id, title, description, and examples.
unrepresentable
The following APIs are not representable in JSON Schema. By default Zod will throw an error if they are encountered. It is unsound to attempt a conversion to JSON Schema; you should modify your schemas as they have no equivalent in JSON. An error will be thrown if any of these are encountered.
By default, Zod will throw an error if any of these are encountered.
You can change this behavior by setting the unrepresentable option to "any". This will convert any unrepresentable types to {} (the equivalent of unknown in JSON Schema).
cycles
How to handle cycles. If a cycle is encountered as z.toJSONSchema() traverses the schema, it will be represented using $ref.
If instead you want to throw an error, set the cycles option to "throw".
reused
How to handle schemas that occur multiple times in the same schema. By default, Zod will inline these schemas.
Instead you can set the reused option to "ref" to extract these schemas into $defs.
override
To define some custom override logic, use override. The provided callback has access to the original Zod schema and the default JSON Schema. This function should dircectly modify ctx.jsonSchema.
pipes
Pipes contain an input and and output schema. By default the result of z.toJSONSchema represents the output schema; use "pipe": "input" to extract the input type instead.
Registries
Passing a schema into z.toJSONSchema() will return a self-contained JSON Schema.
In other cases, you may have a set of Zod schemas you'd like to represent using multiple interlinked JSON Schemas, perhaps to write to .json files and serve from a web server. To achieve this, you can pass a registry into z.toJSONSchema().
The schemas above both have an id property registered in z.globalRegistry. To convert these to JSON Schema, pass the entire registry into z.toJSONSchema().
Any schema with an id property in the registry will be extracted into schemas.
By default the $ref URIs are relative paths like "User". To make these absolute URIs, use the uri option. This expects a function that converts an id to a fully-qualified URI.
