Output will appear here
Output will appear here
Paste a JSON payload and get a Zod schema for it, plus a z.infer type for every object. Strings that look like a UUID, email, URL, date or ISO timestamp get the matching check, such as z.string().email(). Labels that repeat become z.enum. Keys missing from some records get .optional(), while keys that hold null get .nullable().
By Ankit Aglawe · Updated · Runs entirely in your browser
Four string formats are picked up from one sample: .uuid(), .email(), .url() and .datetime({ offset: true }). The nested data object becomes its own dataSchema, declared before rootSchema so the reference resolves. Unix timestamps such as created are plain integers, so they get z.number().int().
{
"id": "evt_1Q2w3E",
"type": "invoice.paid",
"created": 1730112000,
"livemode": false,
"data": {
"invoiceId": "3f6c2a1e-8b4d-4c7a-9e21-5d0f7b8a9c10",
"customerEmail": "[email protected]",
"amountPaid": 4900,
"currency": "usd",
"hostedUrl": "https://pay.example.com/i/3f6c2a1e",
"paidAt": "2026-10-28T09:14:00Z"
}
}import { z } from "zod";
export const dataSchema = z.object({
invoiceId: z.string().uuid(),
customerEmail: z.string().email(),
amountPaid: z.number().int(),
currency: z.string(),
hostedUrl: z.string().url(),
paidAt: z.string().datetime({ offset: true }),
});
export type Data = z.infer<typeof dataSchema>;
export const rootSchema = z.object({
id: z.string(),
type: z.string(),
created: z.number().int(),
livemode: z.boolean(),
data: dataSchema,
});
export type Root = z.infer<typeof rootSchema>;priority repeats two values across four rows, so it becomes z.enum(["high", "low"]) and a third value would be rejected. dueDate is a calendar date or null, giving z.string().date().nullable(). Only one task has assignee, so it is .optional(). A top-level array also gets rootArraySchema.
[
{ "id": 101, "title": "Fix login redirect", "priority": "high", "dueDate": "2026-11-02", "labels": ["auth", "bug"] },
{ "id": 102, "title": "Write release notes", "priority": "low", "dueDate": null, "labels": [] },
{ "id": 103, "title": "Bump zod", "priority": "low", "dueDate": "2026-11-10", "labels": ["deps"], "assignee": "priya" },
{ "id": 104, "title": "Audit CSP headers", "priority": "high", "dueDate": null, "labels": ["security"] }
]import { z } from "zod";
export const rootSchema = z.object({
id: z.number().int(),
title: z.string(),
priority: z.enum(["high", "low"]),
dueDate: z.string().date().nullable(),
labels: z.array(z.string()),
assignee: z.string().optional(),
});
export type Root = z.infer<typeof rootSchema>;
export const rootArraySchema = z.array(rootSchema);Every code generator on this site is run on the same awkward input: a 64-bit id, a float written as 1.0, a null, a date string, a reserved word as a key, an empty array, a nested array and an array of objects whose keys differ. This is the exact output for Zod, generated when the page was built.
{
"id": 9007199254740993,
"price": 19.99,
"ratio": 1.0,
"deleted_at": null,
"created_at": "2024-01-15T10:30:00Z",
"class": "premium",
"tags": [],
"matrix": [[1, 2], [3, 4.5]],
"items": [{ "sku": "A1", "qty": 1 }, { "sku": "B2", "qty": 2, "note": "gift" }]
}import { z } from "zod";
export const itemSchema = z.object({
sku: z.string(),
qty: z.number().int(),
note: z.string().optional(),
});
export type Item = z.infer<typeof itemSchema>;
export const rootSchema = z.object({
id: z.number(),
price: z.number(),
ratio: z.number().int(),
deleted_at: z.null(),
created_at: z.string().datetime({ offset: true }),
class: z.string(),
tags: z.array(z.unknown()),
matrix: z.array(z.array(z.number())),
items: z.array(itemSchema),
});
export type Root = z.infer<typeof rootSchema>;A key present with null gets .nullable(), which accepts null but still requires the key. A key absent from some objects gets .optional(), which accepts a missing key or undefined but not null. In the edge-case output, note is optional because the first item has no note.
deleted_at is null in the sample, so it becomes z.null(). That is strict on purpose: the first real timestamp will fail validation and tell you to paste a richer sample, instead of the schema silently accepting anything.
Whole numbers get z.number().int(). 1.0 counts as whole, because JSON.parse turns it into 1. Zod caps .int() at Number.MAX_SAFE_INTEGER, so a field holding a larger value, like the 64-bit id in the edge-case sample, gets plain z.number() instead. The value has already been rounded by JSON.parse by then, so send such ids as strings if the exact digits matter.
RFC 3339 strings like 2024-01-15T10:30:00Z or 2024-01-15T10:30:00+02:00 become z.string().datetime({ offset: true }), which accepts both forms (plain .datetime() only accepts Z). Looser forms such as 2024-01-15 10:30:00 or a timestamp with no zone stay z.string(), because Zod would reject them.
Object keys in a z.object({...}) literal can be any identifier, so class, from and type stay bare. Keys that are not identifiers, like user-name or 2fa, are quoted. The schema keys always match the JSON exactly.
An empty array becomes z.array(z.unknown()). Nested arrays nest: [[1, 2], [3, 4.5]] is z.array(z.array(z.number())). A mixed array becomes a union, for example z.array(z.union([z.string(), z.number().int()]).nullable()).
| JSON value | Zod schema | Notes |
|---|---|---|
| string | z.string() | Plus .email(), .url(), .uuid(), .date() or .datetime({ offset: true }) when detected |
| repeated small set of strings | z.enum([...]) | Needs enough repeats in the sample |
| integer | z.number().int() | Safe-integer range only |
| float | z.number() | |
| true / false | z.boolean() | |
| null | .nullable() | z.null() if never seen with a value |
| key missing in some objects | .optional() | |
| object | nameSchema const | Plus type Name = z.infer<...> |
| empty array | z.array(z.unknown()) |
Enter any valid JSON in the input editor. The converter works with objects, arrays, and primitive values of any depth.
The output pane shows a complete Zod schema with z.object(), z.array(), z.string(), z.number(), and format-specific validators like z.string().email().
Copy the schema or download it as a .ts file. Import it in your project to get both compile-time types and runtime validation from a single source of truth.
Paste the JSON on the left. The schemas appear on the right, one const per object shape, with rootSchema last. Copy them into a .ts file next to import { z } from "zod".
Yes. Every schema is followed by export type Name = z.infer<typeof nameSchema>, so you validate and type from one source and never write the interface twice.
The project test suite parses every fixture with Zod 4 and checks that each generated schema accepts the JSON it came from. The methods used (z.object, z.enum, .datetime({ offset: true }), .nullable()) also exist in Zod 3.
Call rootSchema.parse(await res.json()) to throw on bad data, or safeParse to get a result object with success and error. The returned value is typed as Root.
The field held a few distinct values that each repeated, which looks like a closed set such as a status. If the field is really free text, replace z.enum([...]) with z.string().
No. Inference and code generation run in your browser tab. Nothing about the payload is sent to a server.
Need several formats at once? Convert All shows every output from one paste. Or browse all JSON converters.