JSON to Zod

Generate a Zod schema from sample JSON.

The JSON to Zod generator builds a Zod validation schema from a sample JSON document, together with the matching inferred TypeScript type. It handles nested objects, arrays, nullable and optional values, and lets you choose strict or passthrough handling of unknown keys. It is a quick way to start runtime validation for an API response.

What is a Zod schema?

Zod is a TypeScript library for declaring a schema once and using it for both runtime validation and static types. A schema such as z.object({ id: z.number(), name: z.string() }) checks that incoming data has the right shape, and z.infer extracts the matching TypeScript type from it. This matters because TypeScript types vanish at runtime, so data from an API, a form or a file is unchecked until you validate it. The generator inspects a sample, records each value's type, and writes the corresponding Zod calls: z.string(), z.number() (with .int() for whole numbers), z.boolean(), z.array(...) and z.object(...). Keys missing from some objects get .optional(), and null alongside another type gets .nullable(). Unknown-key handling is chosen with .strict() (reject extra keys) or .passthrough() (keep them).

How to use JSON to Zod

  1. Paste a sample Paste a JSON document into the Sample JSON box.
  2. Name the schema Enter a Schema name, for example User.
  3. Pick unknown-key handling Choose Strict or Loose (passthrough) in the Unknown keys option.
  4. Copy the code Copy the output, or download it as schema.ts, and place it in your project.

Frequently asked questions

What is the difference between Strict and Loose?

Strict adds .strict() so parsing fails when an object has keys that are not in the schema. Loose adds .passthrough() so unknown keys are accepted and kept in the parsed result.

Why is a field marked optional?

It was missing from at least one of the objects merged into that part of the schema. If you want it required, remove .optional() from the generated code.

How do I add stricter checks such as email or minimum length?

Edit the generated schema, for example change z.string() to z.string().email(). The generator infers only basic types from the sample.

Does it generate TypeScript types too?

Yes, it adds an exported type using z.infer<typeof YourSchema>, so the type always matches the schema.

Is my JSON sent to a server?

No. The schema is generated in the page and the sample is not sent anywhere.