Skip to content

JSON to Zod Schema Generator - Online Converter

Input JSON
Zod Output

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 probably z.enum(["invoice.paid", "invoice.voided"]).
  • customerEmail: z.null() should be z.string().email().nullable() (in Zod 4, z.email().nullable()).
  • currency could be z.string().length(3).
  • If you want created as a Date, add .transform((s) => new Date(s * 1000)); it's in seconds, so z.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

Frequently Asked Questions

Does the output work with Zod 3 and Zod 4?
Yes. It only uses calls that exist in both: `z.object`, `z.array`, `z.union`, `z.string()`, `z.number().int()`, `.nullable()`, `.optional()` and the two-argument `z.record(z.string(), z.unknown())`, which Zod 4 requires.
When do I get .optional() and when .nullable()?
`.optional()` means the key may be missing, and it is added when a key is absent from at least one object in an array. `.nullable()` means the key is present with a null value, and it is added when a null appears next to a real value. If your API does both, chain them or swap in `.nullish()`.
Why did a field come out as z.null()?
It was null in every sample, so null is all the generator knows about it. Replace it with the real type plus `.nullable()`, for example `z.string().email().nullable()`, or paste a record where the field has a value.
What happens to keys that aren't in the schema?
`z.object` strips unknown keys by default, so `parse` returns only the fields you declared. Add `.strict()` if extra keys should be an error, which is useful in tests against a fixture.
Should I use parse or safeParse?
`parse` throws a `ZodError` on bad input, which suits code where a bad payload is a bug. `safeParse` returns `{ success, data, error }` so you can respond with a 400 or log the issues without try/catch.