Paste sample JSON, such as an API response or a form payload, and get a ready-to-use Zod schema with nested object schemas, arrays, .optional() and .nullable() detected from your data and exported z.infer types. Switch to Zod 3 or Yup with one click. Everything runs in your browser.
Last updated:
Your JSON is parsed locally in this tab and never uploaded. Tip: Ctrl + Enter generates.
Advertisement
Advertisement
How to use the JSON to Zod Schema
Paste sample JSON into the left box or open a .json file. If you have several examples, paste them as an array so optional keys can be detected.
Name the root schema, for example user, which produces userSchema and the type User.
Choose the library (Zod 4, Zod 3 or Yup) and whether nested objects get their own named schemas.
Set the options: optional detection, type exports, .int(), string formats and strict objects.
Copy or download the schema and validate data with userSchema.safeParse(data).
Example: infer a Zod schema from JSON
Pasting two sample orders as an array lets the generator see that coupon is sometimes missing and note is sometimes null:
Use several samples. One object cannot show which keys are optional. Paste an array of real responses, including edge cases such as empty lists and null values.
Tighten the result by hand. Inference only sees your sample. Add constraints you know about, such as .min(1), .max(100), .positive() or z.enum(['draft', 'published']) for fixed sets of values.
Parse at the boundary. Call schema.parse() or safeParse() where untrusted data enters your app (API responses, form submissions, environment variables) and use the inferred type everywhere else.
Need a language-neutral schema? Use the JSON Schema Generator for a draft 2020-12 JSON Schema instead.
Frequently asked questions
How does the generator decide what is optional or nullable?
It reads every value in your sample. A key that is missing from some objects in an array becomes .optional(), a value that is null somewhere becomes .nullable(), and a value that is both becomes .nullish(). With a single object nothing can be missing, so for best results paste an array of several real examples. You can also mark every field optional.
What is the difference between the Zod 4 and Zod 3 output?
Zod 4 has top-level string formats, so an email is z.email(), a URL z.url() and an ISO timestamp z.iso.datetime(), and z.record() takes a key and a value schema. Zod 3 uses the method style, such as z.string().email() and z.string().datetime(). Everything else, including z.object, z.array and z.infer, is the same.
How do I get a TypeScript type from the schema?
With Export z.infer type on, every named schema gets a matching type, for example export type User = z.infer<typeof userSchema>. The type is derived from the schema, so you only maintain one source of truth. For Yup the generator writes yup.InferType<typeof userSchema>.
Why is a field typed z.null() or z.union()?
If a value is null everywhere in the sample, its real type cannot be inferred, so the schema accepts only null; replace it with something like z.string().nullable() once you know. If a field holds different types in different places, such as a string in one item and a number in another, the generator writes z.union([...]) with each type it saw.
Are string formats like email and UUID always detected?
A format is only added when every string value of that field matches it, for example every value looks like an email address. Dates must be full ISO 8601 timestamps such as 2024-05-01T09:30:00Z (with { offset: true } when the sample uses +02:00 style offsets). Turn format detection off if your real data may not always follow the format.
Does Yup support everything Zod does?
Most of it. Yup has no union type, so mixed-type fields become yup.mixed(). Required fields use .required(), null-able but present fields use .nullable().defined(), and missing keys are left optional, which is Yup's default.
Is my JSON uploaded?
No. Your JSON is parsed and the schema is generated by JavaScript in this page. Nothing is sent to a server.
Are all the tools on FreeToolCabin tested for quality and accuracy?
Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.