Last updated:
JSON Schema to Zod Converter
Paste a JSON Schema and this JSON Schema to Zod converter writes the Zod code for it: one schema and one inferred type per definition, with the constraints chained on and references resolved, in code that works with Zod 3.23 and later and with Zod 4.
How This Works
All conversions use JSON Schema as an intermediate representation. Your JSON Schema input is first read into a JSON Schema, then converted to Zod Schema. What a format can't express is listed under the output. All processing happens in your browser — your schemas are never sent to a server.
How to use it
Paste a JSON Schema on the left. The Zod code appears on the right as you type, and Copy or Download (schema.ts) takes it away. Load Example fills in a small schema, and From switches the input to a JSON example, TypeScript or GraphQL.
The schema stays in your browser. Each object definition becomes an exported …Schema constant and a type from z.infer, in an order where every schema comes after the ones it uses. A reference it can't follow gets a note under the output; the keywords it leaves out without one are listed below.
Example: a payment with a discriminated union
A payment schema with shared definitions in $defs, a payment method that is either a card or a bank transfer, a nullable description and a map of metadata. No other properties are allowed:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Payment",
"type": "object",
"required": ["id", "amount", "method", "createdAt"],
"additionalProperties": false,
"properties": {
"id": { "type": "string", "pattern": "^pay_[A-Za-z0-9]{12}$" },
"amount": { "$ref": "#/$defs/Money" },
"method": {
"oneOf": [
{ "$ref": "#/$defs/Card" },
{ "$ref": "#/$defs/BankTransfer" }
]
},
"description": { "type": ["string", "null"], "maxLength": 140 },
"metadata": { "type": "object", "additionalProperties": { "type": "string" } },
"createdAt": { "type": "string", "format": "date-time" }
},
"$defs": {
"Money": {
"type": "object",
"required": ["value", "currency"],
"properties": {
"value": { "type": "integer", "minimum": 1, "description": "In the currency's smallest unit, such as cents." },
"currency": { "type": "string", "enum": ["EUR", "GBP", "USD"] }
}
},
"Card": {
"type": "object",
"required": ["type", "last4"],
"properties": {
"type": { "const": "card" },
"last4": { "type": "string", "pattern": "^[0-9]{4}$" }
}
},
"BankTransfer": {
"type": "object",
"required": ["type", "iban"],
"properties": {
"type": { "const": "bank_transfer" },
"iban": { "type": "string", "minLength": 15, "maxLength": 34 }
}
}
}
} Both options of method are objects with a required type set to a different constant, so the union becomes z.discriminatedUnion("type", …), which picks the option by that field. additionalProperties: false becomes .strict(), the enum z.enum, the description a .describe(), and the metadata map z.record. Patterns are built with new RegExp(…, "u"), the Unicode mode JSON Schema validators use, and the date-time accepts an offset such as +01:00, as JSON Schema does.
The converter's Zod Schema output:
import { z } from "zod";
export const moneySchema = z.object({
value: z.number().int().min(1).describe("In the currency's smallest unit, such as cents."),
currency: z.enum(["EUR", "GBP", "USD"]),
});
export type Money = z.infer<typeof moneySchema>;
export const cardSchema = z.object({
type: z.literal("card"),
last4: z.string().regex(new RegExp("^[0-9]{4}$", "u")),
});
export type Card = z.infer<typeof cardSchema>;
export const bankTransferSchema = z.object({
type: z.literal("bank_transfer"),
iban: z.string().min(15).max(34),
});
export type BankTransfer = z.infer<typeof bankTransferSchema>;
export const paymentSchema = z.object({
id: z.string().regex(new RegExp("^pay_[A-Za-z0-9]{12}$", "u")),
amount: moneySchema,
method: z.discriminatedUnion("type", [cardSchema, bankTransferSchema]),
description: z.string().max(140).nullable().optional(),
metadata: z.record(z.string(), z.string()).optional(),
createdAt: z.string().datetime({ offset: true }),
}).strict();
export type Payment = z.infer<typeof paymentSchema>; Example: a recursive schema
A comment whose replies are comments, through a $ref to the schema's own root:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Comment",
"type": "object",
"required": ["id", "body", "replies"],
"properties": {
"id": { "type": "integer" },
"body": { "type": "string", "minLength": 1 },
"tags": { "type": "array", "items": { "type": "string" }, "uniqueItems": true },
"replies": { "type": "array", "items": { "$ref": "#" } }
}
} A schema that refers to itself can't be a plain constant, so the converter wraps it in z.lazy() and writes the Comment interface out as well: TypeScript can't infer a recursive type, and the explicit z.ZodType<Comment> lets the file compile under --strict. Note what's missing: uniqueItems has no Zod method, so tags accepts duplicates.
The converter's Zod Schema output:
import { z } from "zod";
export interface Comment {
id: number;
body: string;
tags?: string[];
replies: Comment[];
}
export const commentSchema: z.ZodType<Comment> = z.lazy(() =>
z.object({
id: z.number().int(),
body: z.string().min(1),
tags: z.array(z.string()).optional(),
replies: z.array(z.lazy(() => commentSchema)),
})
); What carries over, and what doesn't
Carried over: type (with .int() for integers), enum and const, minLength, maxLength and pattern, minimum, maximum, the exclusive bounds and multipleOf, minItems and maxItems, nullable types (a type list with null, or OpenAPI 3.0's nullable: true), and the formats email, uri, uuid, date-time and date. additionalProperties becomes .strict() when false, .passthrough() when true and .catchall() when it's a schema. An allOf of objects is merged into one object.
Not carried over, without a note: default, uniqueItems, not, if/then/else, and other formats such as ipv4, which become a plain z.string(). Add those as .refine() or .default() calls by hand. A $ref to another file and patternProperties do get a note.
Does the output compile?
Both examples are among the converter's compile tests, which run in CI on every pull request: the output is type-checked against Zod 4 and Zod 3 with tsc --strict, then each sample above must parse and each invalid one (a type of "cash", an extra property, an empty body) must be rejected, in both versions.
5 common mistakes
- Converting a schema with external references.
"$ref": "common.json#/Money"can't be followed in the browser, so it becomesz.unknown(). Bundle the schema first, or paste the referenced definitions into$defsand point the references there. - Relying on
default. A schema's default doesn't become.default(), so a missing field staysundefinedafter parsing. Add.default(…)to the fields where your code expects a value. - Expecting
if/thenrules to be enforced. Conditional requirements such as "a bank transfer needs an IBAN when the country is in the EU" are dropped. Model the cases as aoneOfwith aconsttag where you can, which becomes a discriminated union, or add a.superRefine(). - Hand-editing the generated file. If the JSON Schema is the source of truth, regenerate the Zod file when it changes, and keep refinements in a separate file that extends the generated schemas.
- Converting the wrong way round. This page converts JSON Schema to Zod. To get a JSON Schema from a Zod schema, Zod 4 has
z.toJSONSchema(); to get one from example data, use the JSON to Schema Generator.
Frequently Asked Questions
Which versions of Zod does the output work with?
Zod 4, and Zod 3 from 3.23 on. The compile tests type-check every output against Zod 4 and Zod 3.25 and run the same samples through both.
Which JSON Schema drafts can I paste?
The keywords it reads are written the same way from Draft 4 to 2020-12, and it also reads OpenAPI 3.0 schema objects. References to definitions, $defs, components.schemas, the root, any JSON pointer or an $anchor resolve, and Draft 4's boolean exclusiveMinimum and OpenAPI's nullable are read. $dynamicRef and $recursiveRef aren't followed.
Is the schema I paste uploaded?
No. The conversion runs in your browser. The site's usage log records which formats you converted between and counts such as the number of properties, as the privacy policy describes.
Can I convert JSON data to Zod without writing a schema?
Yes: set From to JSON Example and paste the data. The converter infers a schema first, including formats it recognizes such as emails and date-times. The JSON to TypeScript page explains how that inference treats optional and null fields.
Other converters
- JSON to TypeScript Converter
- JSON to Go Struct Converter
- JSON to Pydantic Converter
- Schema Converter, with every pair: JSON Schema, TypeScript, GraphQL or a JSON example in; TypeScript, Zod, Pydantic, Go, GraphQL or JSON Schema out
- JSON to Schema Generator, to infer a JSON Schema from one or more examples and refine it field by field