zod to json schema examples

Zod to JSON Schema Examples

Practical examples for converting Zod object, enum, union, optional, nullable, array, and nested schemas.

Zod to JSON Schema Converter

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 patternJSON Schema outputProduction 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.
NullableA 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 Converter

Zod to JSON Schema resources

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