Zod to JSON Schema Examples
Practical examples for converting Zod object, enum, union, optional, nullable, array, and nested schemas.
Zod object to JSON Schema example
Zod object fields are required unless the field uses `.optional()`. FrameworkKit preserves that distinction in the generated JSON Schema `required` array. Use the converter page when you want to paste this example and inspect live output.
const UserSchema = z.object({
id: z.string().uuid(),
email: z.string().email(),
nickname: z.string().optional(),
});Zod enum to JSON Schema example
Use `z.enum()` for a string choice list and `z.literal()` for exact values that should become JSON Schema enum constraints.
const StatusSchema = z.object({
role: z.enum(["admin", "member"]),
version: z.literal("v1"),
});Zod nullable field to JSON Schema example
Nullable fields and arrays are common API response patterns. Toggle OpenAPI mode if a downstream OpenAPI 3.0 document needs nullable output.
const ResponseSchema = z.object({
data: z.array(z.string()).nullable(),
});Nested object example
Nested Zod objects become nested JSON Schema objects. Keep reusable objects named in your source code when the schema will be shared across OpenAPI, AJV, or documentation workflows.
const OrderSchema = z.object({
id: z.string(),
customer: z.object({
email: z.string().email(),
}),
items: z.array(z.object({ sku: z.string(), quantity: z.number().int() })),
});Union and literal example
Simple unions of literals can become enum-like JSON Schema output. Wider unions should be reviewed because downstream validators may represent them as `anyOf` or `oneOf` depending on the target.
const EventSchema = z.object({
type: z.union([z.literal("created"), z.literal("updated")]),
actorId: z.string(),
});Review generated constraints
Generated JSON Schema is a strong starter, but it cannot express every TypeScript refinement. Review string formats, numeric ranges, transforms, defaults, and custom validation before publishing a contract.
- Use fixtures that cover valid, invalid, nullable, optional, and extra-property cases.
- Choose the JSON Schema draft or OpenAPI mode before sharing output.
- Keep the source of truth clear so Zod and JSON Schema do not drift.
Zod example conversion map
| Zod pattern | JSON Schema output | Production review |
|---|---|---|
| Object | `type: object` with `properties` and `required`. | Confirm strict, loose, or passthrough object behavior. |
| Enum | `enum` with literal string values. | Check that UI labels and API values do not drift. |
| Nullable | A nullable representation based on the selected target mode. | Review OpenAPI 3.0 versus JSON Schema draft expectations. |
| Array | `type: array` with an `items` schema. | Add min/max item rules when the API contract requires them. |
Use FrameworkKit to generate the starter code, then review the output before shipping it in production.
Zod to JSON Schema ConverterZod to JSON Schema resources
Zod to JSON Schema Converter
Use the canonical free online converter for Draft 2020-12, Draft 7, AJV, or OpenAPI-compatible output.
Zod 4 z.toJSONSchema() guide
Convert schemas natively in Zod 4, pick a draft target, add metadata, and migrate off the zod-to-json-schema package.
Zod to JSON Schema examples
Review object, enum, nullable, optional, array, and nested Zod conversion examples.
Zod to OpenAPI guide
Generate Swagger/OpenAPI docs from Zod schemas, compare package workflows, and choose OpenAPI 3.0 or 3.1.
Convert Zod to JSON Schema with AJV
Convert Zod to JSON Schema, choose an Ajv draft, compile validators, and avoid unsupported Zod features.
Fastify Zod validation
Use Zod schemas with Fastify request validation, response schemas, type providers, and OpenAPI output.
Zod to JSON Schema for OpenAPI
Publish OpenAPI-compatible schema output from TypeScript-owned Zod contracts.
Zod refine vs superRefine
Review why custom refinements cannot be represented as plain JSON Schema without a separate validation step.
Zod API schemas
Use Zod at API request and response boundaries before publishing or validating contracts.
Zod vs JSON Schema
Decide when Zod, JSON Schema, OpenAPI, and AJV should own runtime validation contracts.
FAQ
Do Zod examples convert directly to JSON Schema?
Common object, enum, literal, array, optional, and nullable patterns convert well. Custom refinements and transforms need manual review because JSON Schema cannot run TypeScript logic.
Should I use OpenAPI mode for nullable fields?
Use OpenAPI mode when the generated schema is going into an OpenAPI 3.0 document. Use the JSON Schema draft output when the target validator expects standard JSON Schema.
Can the generated examples be used with AJV?
Yes, after selecting a JSON Schema draft that matches your Ajv runtime and testing the compiled validator with representative fixtures.
Why keep examples separate from the live tool?
Examples show stable patterns that teams can compare before pasting their own Zod schema into the converter.
Related comparisons
Related tools
JSON to Zod Converter
Convert JSON to Zod schemas with strict objects, optional field inference, and inferred TypeScript types.
TypeScript to Zod Converter
Convert TypeScript interfaces and type aliases into Zod schemas with inferred types in your browser.
OpenAPI to Zod
Turn OpenAPI schemas into Zod validators and lightweight typed fetch clients.