JSON to Zod Schema Generator - Online Converter
A starting schema for untrusted input
Zod earns its keep at the edges of an app: webhook handlers, form posts, third-party APIs, anything where the
data didn't come from your own code. Writing the first version of that schema by hand from a payload is tedious.
Paste a real payload here and you get a z.object per shape, each followed by
export type X = z.infer<typeof xSchema>, so the validator and the TypeScript type can't drift
apart.
Example: a billing webhook
Input, with the root type name set to InvoiceEvent:
{
"event": "invoice.paid",
"created": 1731196800,
"data": {
"invoiceId": "in_1Q2w3E",
"amount": 49.5,
"currency": "usd",
"customerEmail": null
},
"lines": [
{ "sku": "PRO-M", "qty": 1 },
{ "sku": "SEAT", "qty": 3, "discount": 0.1 }
]
} Output:
import { z } from "zod";
export const dataSchema = z.object({
invoiceId: z.string(),
amount: z.number(),
currency: z.string(),
customerEmail: z.null(),
});
export type Data = z.infer<typeof dataSchema>;
export const lineSchema = z.object({
sku: z.string(),
qty: z.number().int(),
discount: z.number().optional(),
});
export type Line = z.infer<typeof lineSchema>;
export const invoiceEventSchema = z.object({
event: z.string(),
created: z.number().int(),
data: dataSchema,
lines: z.array(lineSchema),
});
export type InvoiceEvent = z.infer<typeof invoiceEventSchema>;
Schemas come out child-first, so dataSchema is declared before the schema that uses it and the
file runs top to bottom without hoisting problems. qty and created were whole numbers
in every sample, so they get .int(); amount had a fraction, so it doesn't.
discount only appears on one line item and becomes .optional().
Tightening it by hand
The generator describes the sample. It can't know your business rules, so plan on a few edits before this guards a production endpoint:
event: z.string()is probablyz.enum(["invoice.paid", "invoice.voided"]).customerEmail: z.null()should bez.string().email().nullable()(in Zod 4,z.email().nullable()).currencycould bez.string().length(3).- If you want
createdas aDate, add.transform((s) => new Date(s * 1000)); it's in seconds, soz.coerce.date()would read it as milliseconds.
z.coerce is the right tool when the input arrives as strings, such as query parameters or
environment variables: z.coerce.number().int() accepts "3". JSON bodies already have
real numbers, so the generator doesn't emit it.
Using it in a handler
const result = invoiceEventSchema.safeParse(await req.json());
if (!result.success) return new Response(result.error.message, { status: 400 });
const event: InvoiceEvent = result.data; If you only need the types and not the validator, JSON to TypeScript gives you plain interfaces. If a non-TypeScript service has to validate the same payload, generate a JSON Schema instead and check documents against it with the JSON Schema validator.
Same JSON, other languages
Every generator here reads the sample the same way, so a key that is optional or nullable in one language is optional or nullable in the others. Paste the same input into: TypeScript, Go, Python, Rust, Java, C#, JSON Schema.
Related Tools
JSON to YAML Converter
Convert JSON data to clean, readable YAML format instantly.
JSON to XML Converter
Transform JSON objects and arrays into well-formed XML documents.
JSON to CSV Converter
Flatten JSON arrays into downloadable CSV spreadsheets in one click.
YAML to JSON Converter
Convert YAML configuration files to JSON format instantly.