JSON to Zod schema
Paste a JSON sample and get a Zod schema that validates it, with a TypeScript type inferred from the schema and optional fields from the data.
Paste JSON
Tree
The tree of your JSON appears here.
Paste JSON in first; its readable text, tables and types appear here.
Before
After
Differences
Paste valid JSON on both sides to compare them. Key order and spacing are ignored.
How to generate a Zod schema
- Paste the JSON in View & search.
- Open Text, table & types and choose Zod.
- Set the root type name if you want something other than Root.
- Copy the schema; use
RootSchema.parse(data)to validate.
Validate what your API really returns
TypeScript types disappear at runtime; a Zod schema does not. Generating the schema from a real response gives you a validator for the boundary of your app: parse the response with the schema and you get either typed data or an error that says which field did not match. Each object gets its own schema, nested schemas are reused, and z.infer gives you the matching type without writing it twice.
Checked against your sample
In pastejson's automated tests every generated schema must parse the very JSON it was generated from, and the code must compile with strict TypeScript settings. That catches mistakes such as a field typed as a number when the sample also contains null.
Tips
- Paste a response that includes the optional parts so they are marked
.optional(). - When the root is a list, the root schema is
z.array(...)of an item schema. - Generation happens in your browser; nothing is uploaded.
Questions
Which Zod version is the code for?
The output uses the standard z.object, z.array and z.infer API that works with current Zod versions.
How do I use the schema?
Import it and call RootSchema.parse(data) or safeParse(data) on the data you receive.
Are nullable and optional fields handled?
Yes. Fields missing in some items become .optional(), and null values become z.null() or a union with it.
Is my JSON uploaded?
No. The schema is generated in your browser.
Can I get plain TypeScript types instead?
Yes. Choose TypeScript in the same tab.