JSON KitJSON to Zod Schema

JSON to Zod Schema

Generate named, split Zod schemas with type exports from any JSON sample — ready for runtime validation.

Loading editor…
Loading editor…

Related Tools

What is JSON to Zod Schema?

This tool converts a JSON sample into a complete Zod validation schema. Unlike tools that generate one giant inline expression, JSON Kit generates named, split schemasfor every nested object — idiomatic Zod that's composable, importable, and readable in a real codebase.

The generator also detects string formats automatically: email addresses become z.string().email(), ISO datetimes become z.string().datetime(), UUIDs become z.string().uuid(). Every schema includes a z.infer type export so your TypeScript types stay in sync with your validators with no duplication.

How to Use This Tool

  1. Paste your JSON sample into the Input panel.
  2. Set the Root name to match your domain object (e.g. User, Order).
  3. Choose how null fields are handled: .nullable(), .optional(), or .nullish().
  4. Copy the output into a .ts file and install Zod: npm install zod.
  5. Replace any z.unknown() fields where you know the actual type from the null fields.

Example Output

import { z } from "zod";

export const AddressSchema = z.object({
  city: z.string(),
  country: z.string(),
});

export const UserSchema = z.object({
  id: z.number().int(),
  email: z.string().email(),
  createdAt: z.string().datetime({ offset: true }),
  address: AddressSchema,
});

export const RootSchema = z.object({
  user: UserSchema,
});

export type Address = z.infer<typeof AddressSchema>;
export type User = z.infer<typeof UserSchema>;
export type Root = z.infer<typeof RootSchema>;

Frequently Asked Questions

What is a Zod schema?
Zod is a TypeScript-first schema validation library. A Zod schema describes the shape of a value and can validate data at runtime — unlike TypeScript interfaces which are erased at compile time. When you parse an API response with a Zod schema, you get a type error at runtime if the shape doesn't match, rather than a silent type mismatch that blows up later.
Why use Zod instead of just TypeScript interfaces?
TypeScript interfaces only exist at compile time. Zod schemas also validate at runtime — critical when you're receiving JSON from an API, an LLM, or user input. If the API changes its response shape, a Zod schema catches it immediately. TypeScript alone won't. The two complement each other: use the JSON to TypeScript tool to get your types, and this tool to get runtime validators that match those types.
What does 'named schemas' mean and why does it matter?
Instead of generating one giant nested z.object({...z.object({...})}) expression, this tool generates a separate named constant for each nested object: AddressSchema, UserSchema, RootSchema. This is idiomatic Zod — it lets you import and reuse individual sub-schemas, compose validators, and write tests targeting specific shapes without extracting manually.
How are null fields handled?
When a field is null in your JSON sample, the true underlying type is unknown (null just means 'this value wasn't populated'). The tool generates z.unknown().nullable() for null fields. You should replace z.unknown() with the actual expected type once you know it — e.g., z.string().nullable() for a nullable string field. The 'Null handling' option lets you choose .nullable(), .optional(), or .nullish() depending on your semantics.
What format detections does this tool do automatically?
When 'Detect formats' is enabled, the tool inspects string values and upgrades the schema: ISO datetime strings → z.string().datetime(), email addresses → z.string().email(), UUIDs → z.string().uuid(), HTTP URLs → z.string().url(). Disable this if your strings are coincidentally formatted like these and you don't want the validation.
What is 'coerce date' mode for datetime fields?
By default, datetime fields generate z.string().datetime() which validates the string format but keeps it as a string. With 'coerce date' mode, they generate z.coerce.date() which converts the string to a JavaScript Date object during parsing. Use coerce.date() when you want Date objects in your application; use string mode when you prefer to keep dates as ISO strings.
How do I handle JSON arrays at the root?
If your JSON is a top-level array (e.g., [{"id": 1},...]), the tool generates an item schema (e.g., UserSchema) and wraps the root in z.array(UserSchema). The naming uses a singularized version of your root name — so a root named 'Users' generates a UserSchema item type and a UsersSchema array type.
What does 'export types' do?
When enabled, the output includes export type Foo = z.infer<typeof FooSchema> for each generated schema. This means you only need to write the Zod schema — the TypeScript type is inferred from it automatically, staying in sync as you edit the schema. This is the recommended Zod pattern for most projects.

About This Tool

Zod has become the dominant runtime validation library in the TypeScript ecosystem, especially in Next.js, tRPC, and React Hook Form projects. JSON Kit's Zod generator is built specifically for the AI era — where JSON comes from LLMs, external APIs, and webhook payloads that you don't control. It runs 100% in your browser, handles arbitrarily nested JSON, detects common string formats, and generates idiomatic split schemas rather than hard-to-read one-liners.