Generate a Zod schema from JSON

What converting JSON to a Zod schema gets right, what a single sample cannot reveal, and how to review generated validation code before you rely on it.

Turning a JSON sample into a Zod schema is a quick way to get runtime validation and an inferred TypeScript type without writing every field by hand. The catch is that a generator only sees the example you give it. It records the shape of one payload, not the rules of the data source. This review covers what the conversion actually produces, where it goes wrong, and when you are better off writing the schema yourself or generating it from something more authoritative.

Is a JSON-to-Zod conversion suitable for your use?

Use it as a first draft for structure. The main benefit is speed and accuracy on the tedious part: nested objects, arrays and key names get transcribed without typos, which matters for large or deeply nested payloads. The decisive tradeoff is that the generated schema describes the sample, not the contract. Optional fields, nullable values, unions, enums, string formats and numeric constraints are invisible in a single example. You will need to tighten or loosen many fields by hand, and an unreviewed schema can reject valid data or accept invalid data.

A generated Zod schema is a poor fit, and a different starting point is preferable, in these situations:

  • You only need compile-time types and never validate at runtime. A plain TypeScript type or interface is simpler and adds no dependency.
  • The API already publishes an OpenAPI document or a JSON Schema. Generating from that specification captures required fields, formats and enums that a sample cannot show.
  • The schema enforces security-relevant or business-critical rules. Those rules should be written deliberately and reviewed, not inferred from whatever one response happened to contain.
  • The sample contains sensitive data and the only available converter is a hosted web tool whose handling of pasted input you cannot verify.

What the conversion actually changes

This is a code-generation step, not a character or display transformation. Your JSON data is not altered. The generator parses it, walks the resulting values, and writes new TypeScript source that calls Zod builders matching each value's type. Words like reverse, mirror or font do not apply here. The output is ordinary source text that you store, edit and commit like any other code.

Most converters follow a direct mapping from JSON value types to Zod builders. Exact choices vary by tool, so check what yours emits for the cases in the right-hand column.

JSON value in the sample | Typical generated schema | What the sample cannot tell you
string | z.string() | Whether it is an email, URL, UUID, ISO date-time, or one of a fixed set of values
number | z.number() | Whether it must be an integer, has a range, or can exceed the safe integer limit
boolean | z.boolean() | Usually nothing; booleans are the least ambiguous case
null | z.null() | What the field holds when it is not null
object | z.object({ ... }) | Which keys are optional, and whether unknown keys should be stripped, kept or rejected
array of one type | z.array(...) | Minimum or maximum length, and whether other element types are allowed
empty array | z.array(z.unknown()) or similar | The element type entirely
array of mixed types | z.union or z.array(z.unknown()), depending on the tool | Whether the mix is intentional or a data error

Here is an illustrative sample and the kind of output a straightforward converter produces. The data is invented for demonstration.

{
  "id": 42,
  "name": "Ada",
  "tags": ["admin", "beta"],
  "manager": null,
  "createdAt": "2026-01-15T09:30:00Z",
  "settings": { "theme": "dark", "notifications": true },
  "aliases": [],
  "display-name": "Ada L."
}
import { z } from "zod";

export const UserSchema = z.object({
  id: z.number(),
  name: z.string(),
  tags: z.array(z.string()),
  manager: z.null(),
  createdAt: z.string(),
  settings: z.object({
    theme: z.string(),
    notifications: z.boolean(),
  }),
  aliases: z.array(z.unknown()),
  "display-name": z.string(),
});

export type User = z.infer<typeof UserSchema>;

Each line matches the sample faithfully, and several are still wrong for real use. The manager field typed as z.null() will reject every record that actually has a manager. The createdAt field accepts any string, including ones that are not dates. The aliases field says nothing useful because the sample array was empty. The key display-name is not a valid JavaScript identifier, so it has to stay quoted; check that your tool does this, or the output will not compile.

A reviewed version encodes what you know about the source rather than what one response contained. The choices below are assumptions you would confirm against documentation or more samples:

export const UserSchema = z.object({
  id: z.number().int(),
  name: z.string(),
  tags: z.array(z.string()),
  manager: z.string().nullable(),
  createdAt: z.string().datetime(),
  settings: z.object({
    theme: z.enum(["light", "dark"]),
    notifications: z.boolean(),
  }),
  aliases: z.array(z.string()),
  "display-name": z.string().optional(),
});

Version matters for some of these calls. Zod 4 introduced top-level format helpers such as z.iso.datetime() and dedicated strictObject and looseObject builders, while the older chained forms remain available but are marked as legacy. Confirm which major version your project installs and whether the generator targets it. Mismatched output may still run, but it can produce deprecation warnings or differ in default behavior.

Review points a single sample hides

  • Optional versus required: every key in the sample becomes required unless you mark it otherwise. Feeding the generator several varied samples, if it supports that, can reveal which keys come and go.
  • Null handling: decide between nullable(), optional() and nullish() based on whether the source sends null, omits the key, or does both.
  • Unknown keys: z.object strips unrecognized keys by default. If you need to reject them or pass them through, choose that behavior explicitly.
  • Large integers: JSON.parse turns integers beyond the safe integer range into imprecise numbers before Zod ever sees them, so IDs of that size are often better modeled as strings at the source.
  • Dates: JSON has no date type. A generated schema will treat timestamps as strings unless you add a format check or a transform.
  • Recursive or repeated shapes: a sample of a tree or a list of similar objects is usually expanded literally rather than expressed as a reusable or recursive schema, which may need manual refactoring with z.lazy or shared named schemas.
  • Discriminated unions: if a field such as type selects between different object shapes, one sample shows only one branch.

Will the result stay usable after copying or sharing?

The output is plain ASCII-friendly source code, so copying it into an editor normally works. The practical risks come from the path the text takes:

  • Copying through chat apps, word processors or some documentation tools can replace straight quotes with curly quotes or collapse indentation, which breaks compilation. Paste into a code editor or use a raw code block when sharing.
  • The snippet needs the matching import from zod and a compatible installed version; a schema copied without its import or into a project on a different major version may fail or behave differently.
  • Non-ASCII keys and values from the source JSON should survive as long as the file is saved as UTF-8 and the keys are quoted where required. Check the generated code rather than assuming a particular tool handles every key correctly.
  • For review and onboarding, the inferred TypeScript type is often easier to read than the schema itself. Exporting both, as in the example above, gives teammates a plain description of the shape alongside the runtime check.

Screen readers and other assistive technology treat this output as ordinary code text. Unlike styled Unicode characters, the conversion introduces no substitute glyphs or reordered text, so accessibility depends on the editor or documentation platform where the code is displayed, not on the schema.

Privacy when using an online converter

Sample payloads often contain real names, tokens or internal identifiers. A converter's appearance or marketing language does not tell you whether pasted input is processed only in your browser, sent to a server, logged or retained. Unless the tool's own documentation states its handling and you trust it, anonymize the sample first, or use a local library or editor extension that runs entirely on your machine.

What this review does not cover

No specific converter was tested for this article, so nothing here compares named tools, their output quality, version support or data handling. The behaviors described are the general mechanics of inferring a Zod schema from JSON and documented Zod features. Before adopting a particular generator, run it against a few representative samples, including ones with nulls, empty arrays, missing keys and unusual key names, and compare the output with the review points above.