Skip to content
Schema helper

JSON to Zod schema

Create a Zod schema from a JSON sample. The output has import { z } from 'zod', a z.object({...}) schema and one line that extracts the type with z.infer, so you can paste it in and validate an API response or similar with schema.parse(data). When array elements have different shapes they are merged into one, a key present in only some elements becomes .optional(), and a value such as string or null becomes .nullable(). An empty array becomes z.array(z.unknown()). Numbers, whole or not, are z.number() and date strings are z.string(), so add .int(), .datetime() and constraints such as length and range yourself. It infers from a single sample. Everything runs in your browser and your JSON is not sent anywhere.

Other validators: Valibot and JSON Schema

Build a Valibot schema or a JSON Schema (draft 2020-12) from the same JSON sample. Array elements are merged, keys missing from some objects become optional, and the “Use string formats” and root name options above apply.

Guide

How to use & features

  • Paste JSON into the input and click “Convert to Zod.”
  • Objects are formatted into `z.object({...})` blocks with nested schemas.
  • Arrays infer their element types; mixed values are wrapped with `z.union([...])`.
  • Use the copy or clear buttons to reuse the schema or start over.
Samples

Sample input & output

Convert JSON to Zod schema

Input
{"id":42,"enabled":false,"labels":["alpha","beta"]}
Output
const Schema = z.object({
    id: z.number(),
    enabled: z.boolean(),
    labels: z.array(z.string()),
});
FAQ

FAQ

  • What code does it output?

    Three things: `import { z } from 'zod'`, a `z.object({...})` schema, and one line that extracts the type with `z.infer`. Paste it into your project and validate with `schema.parse(data)`.
  • What happens when array elements have different shapes?

    They are merged into one type. A key that only some elements have becomes `.optional()`. `z.union` is used only when the kinds of value differ, such as a string and a number.
  • How is `null` handled?

    A case like "string or null" gets `.nullable()`. A key whose only value is `null` becomes `z.null()`.
  • Do integers and dates become `.int()` and `.datetime()`?

    No. Numbers, whole or not, are `z.number()`, and date strings are `z.string()`. Add any narrowing you need to the output by hand.
  • What happens with an empty array?

    The element type is unknown, so it becomes `z.array(z.unknown())`. Replace it with the real type.
  • Is my JSON sent anywhere?

    No. Conversion happens entirely in your browser.
Use cases

Common use cases

  • Quickly building validation for an API response

    Paste a real response and use the schema to validate received data, for example `schema.parse(await res.json())`.
  • Getting the type and runtime validation in one go

    `z.infer` extracts the TypeScript type from the same schema, so you do not write a type and a validator twice.
  • Creating a starting point for a schema

    Instead of writing from scratch, build the skeleton from sample JSON and add only the constraints, such as length and range, by hand.
  • Checking the shape of test data

    Validate in your tests that mock or fixture JSON matches the schema you expect.
Notes

Notes & limitations

  • It infers from a single sample

    It looks only at the shape of the values in your JSON. It cannot know ranges, enums, lengths or formats such as email. Adjust the result to your spec.
  • Whether a key is required depends on the sample

    Keys present in every array element are required and keys present in only some become `.optional()`. Check that this matches the real specification.
  • No integer or date narrowing is added

    You get plain `z.number()` and `z.string()`. Add `.int()` or `.datetime()` where you need them.
  • Input must be strict JSON

    Comments, trailing commas and other non-JSON syntax cause an error. Large integers that JavaScript cannot represent exactly are rounded.
About

About this tool

JSON Response to Zod Schema Converter

Accept any unverified JSON data and instantly output schema validation code for “Zod”—the industry-standard runtime validation library inside the TypeScript ecosystem.
When simultaneously attempting to enforce both validation logic and static typing upon dynamic data returned from an API, manually authoring Zod schemas can be extraordinarily labor-intensive. By directly converting expected JSON formats into Zod objects, you can build type-safe, resilient applications more effortlessly.

When it helps

  • Validate external APIs: Build a first-pass Zod schema from a real JSON response.
  • Protect runtime boundaries: Check data coming from webhooks, forms, storage, or third-party services.
  • Create test fixtures: Generate schemas while documenting expected payload shapes.

Inference note

The generated schema reflects the sample JSON, not every possible response. Review optional fields, nullable values, enums, arrays with mixed shapes, and nested objects before using the schema in production validation.

Articles for this tool

Recent Articles