Skip to content
TypeParser
All tools

JSON to Zod

Generate Zod schema from a JSON sample.

beats transform.tools edge: Deep nesting + union narrowing
JSON
paste JSON
Guide

About JSON to Zod

Generate a Zod schema (<code>z.object</code>, <code>z.array</code>, <code>z.string</code>, <code>z.number</code>, <code>z.boolean</code>, <code>z.null</code>, <code>z.union</code>) from a JSON sample. Handles deep nesting, optional fields, and union types via array inspection. Pairs with TypeScript's <code>z.infer</code> for runtime-validated, typed data flow.

Why Zod won

TypeScript types are erased at runtime. The moment data crosses a system boundary — fetch response, form input, queue message — types stop helping. Zod fills the gap: a single schema is both runtime validator AND TypeScript type. Define once, use everywhere, get safety on both sides.

const User = z.object({
  id: z.number(),
  email: z.string().email(),
  name: z.string(),
  tags: z.array(z.string()).optional(),
});

type User = z.infer<typeof User>;
const user: User = User.parse(apiResponse);  // throws if invalid

The generator infers the schema from data. Manual refinement (.email(), .url(), .uuid()) you add by hand based on knowledge the JSON cannot reveal.

Common workflows

Validate API responses. Generate schema from a sample, save in src/schemas/, parse responses through it. Bad API → loud error, not silent data corruption.

Validate form input. Generate schema from the expected shape, layer on .email(), .min(), .refine() constraints. Hook into React Hook Form.

Generate types for an external API. Skip the manual interface authoring. Generate, refine, infer.

Migrate from JSON Schema. If you have JSON Schema, this tool produces equivalent Zod, often more readable.

When Zod isn’t right

For very large schemas, Zod runtime cost adds up. For schemas you control end-to-end (internal RPC), TypeScript types alone may be enough. For protocol-level validation (gRPC, GraphQL), use the protocol’s own type system.

Frequently asked questions

Why Zod over plain TypeScript?
TS types vanish at runtime — your code trusts the API response. Zod validates at runtime, so a malformed payload errors out instead of corrupting state.
Does it generate optional fields?
Yes. From an array of objects, fields present in some but not all become z.string().optional(). The threshold is configurable.
How are dates handled?
ISO 8601 strings are detected and emitted as z.string().datetime(). Other formats default to z.string().
Can I get strict mode?
Yes. Toggle Strict to emit .strict() on every object — extra keys cause validation errors.
What about Zod 4?
We default to v3 syntax (the dominant in production today). Toggle Zod 4 for the v4 syntax with the slightly different optional/nullable handling.
Can I infer the TS type?
The output includes a type X = z.infer<typeof schema> line so you get the TS type for free without running [JSON to TypeScript](/json-to-typescript).

Related tools

Last updated: 2025-01-15