JSON to Zod Schema

Convert JSON to a Zod schema and TypeScript type instantly. Merges array items, marks missing keys optional and detects integers and nullable fields.

Built & Maintained by the devtoolspack Team

Last updated: September 2026

Generate Zod Schemas from Real Data

Zod is the most popular TypeScript-first validation library. You describe the shape of your data once and get both runtime validation and a static type through z.infer. Writing that schema by hand for a large API response is slow and easy to get wrong. This tool reads a JSON sample and writes the schema for you.

What the Generator Infers

  • Primitives: strings, booleans, and numbers, including .int() when every sample is a whole number.
  • Nullable and optional: null values add .nullable(), and keys missing from some array items add .optional().
  • Arrays of objects: all items are merged into one element schema instead of trusting only the first item.
  • Mixed types: a field that is sometimes a string and sometimes a number becomes z.union([...]).
  • Awkward keys: keys such as "first-name" are quoted so the output always compiles.

Refine After Generating

A sample cannot tell the generator what a value means. After pasting the schema into your code, tighten strings that have a known format with .email(), .url(), .uuid() or .datetime(), and replace fixed sets of strings with z.enum([...]).

Related Tools

Prefer plain types? Use JSON to TypeScript. If your sample will not parse, fix it first with the JSON Fixer or check it in the JSON Validator. Everything runs locally in your browser.

How to Generate a Zod Schema from JSON

  1. Paste JSON: Paste a sample API response or config file, or load the example.
  2. Name the schema: Set a schema name such as "user" to get userSchema and a User type.
  3. Copy the schema: Copy the generated code into your project and adjust any strings to .email(), .url() or enums as needed.

Frequently Asked Questions

How are optional fields detected?

Every object in an array is merged into one schema. A key that appears in some items but not others gets .optional(). A key that is sometimes null gets .nullable(). Paste several real records to get the most accurate schema.

Does the output work with Zod 3 and Zod 4?

Yes. The generator only uses z.object, z.array, z.string, z.number().int(), z.boolean, z.null, z.union, .optional() and .nullable(), which behave the same in both versions.

Why is a number field typed as z.number().int()?

If every sample value for that field is a whole number, the schema requires integers. If any sample has a decimal, it becomes z.number(). Remove .int() if your API can send decimals that your sample did not include.

Why use Zod instead of just a TypeScript interface?

TypeScript types disappear at runtime, so they cannot catch a malformed API response. A Zod schema validates the data when it arrives, and z.infer gives you the matching TypeScript type from the same source.

Cite this Tool

Using this tool for a technical blog, research, or documentation? Copy a pre-formatted citation below to link back to devtoolspack.

APA Citation
devtoolspack. (2026). JSON to Zod Schema. https://devtoolspack.dev/json-to-zod
MLA Citation
"JSON to Zod Schema." devtoolspack, 2026, https://devtoolspack.dev/json-to-zod.