Skip to content
gohacks.dev
Standard Library

JSON to Zod Schema

Generate a Zod schema and its inferred TypeScript type from a JSON sample, with nested objects, optional keys, nullable fields and integer checks.

Free · No sign-up · Your inputs stay in your browser

Input

Generate a Zod schema from JSON

Output

Zod

469 characters

Whole numbers become z.number().int(), keys missing from some array items .optional(), and nulls .nullable(). Tighten it by hand: .email(), .url() and enums are facts a single sample cannot show.

Runs in this browser. Nothing you paste is sent to a server.

One definition, checked at runtime

A TypeScript type only exists while compiling. A Zod schema checks the data when it arrives, and z.infer gives the static type from the same definition, so the two cannot drift. Parse at the boundary: const user = userSchema.parse(await res.json()) throws if the API sent something else.

Refine the guess

A sample cannot show rules, so add them: z.string().email(), z.enum([...]) for fixed values, .min() and .max() for ranges. safeParse() returns a result instead of throwing, which suits form validation. See the Zod documentation for the full set.

Share

Frequently asked questions

What is the difference between .optional() and .nullable()?
optional() allows the key to be missing (undefined); nullable() allows it to be present with the value null. A field can need both: .nullable().optional().
Does the output work with Zod 4?
Yes. z.object, z.string, z.number().int(), z.array, z.union, .nullable() and .optional() are the same in Zod 3 and 4.
Is what I paste sent anywhere?
No. The tool runs entirely in your browser with JavaScript. Nothing you type or paste is sent to our server or stored.

Keep planning

Similar tools