JSON to Zod schema converter

Paste JSON to generate a Zod schema and a TypeScript type. Everything runs in your browser, so your data never leaves your machine.

import * as z from 'zod'

export const rootSchema = z.object({
  page: z.int(),
  hasMore: z.boolean(),
  users: z.array(z.object({
    id: z.uuid(),
    name: z.string(),
    email: z.email(),
    website: z.url().optional(),
    age: z.int(),
    rating: z.number(),
    createdAt: z.iso.datetime(),
    lastLogin: z.iso.datetime().nullable(),
    roles: z.array(z.string()),
    address: z.object({
      city: z.string(),
      country: z.string(),
    }).optional(),
  })),
})

export type Root = z.infer<typeof rootSchema>

23 keys read, 4 levels deep.

How the schema is inferred

The converter walks every value in your sample and combines the types it sees. Arrays give it the most to work with: it merges every item in an array into one element type, which is how it spots optional and nullable fields.

In your JSONIn the schema
A key missing from some array items.optional()
A value that is sometimes null.nullable()
Sometimes missing, sometimes null, sometimes a value.nullish()
Whole numbers onlyz.int() (z.number() once a decimal appears)
Mixed types in one positionz.union([...])
An empty arrayz.array(z.unknown())
Email, http(s) URL, UUID, ISO date or date-timeThe matching Zod format
A date-time with a +05:00 style offsetz.iso.datetime({ offset: true })

The tool only keeps a string format when every sample in that field matches it. One plain string among ten emails drops the field back to z.string(). Dates have to be real calendar dates, and date-times need seconds, because that's what Zod accepts.

Using the output

Install Zod with npm install zod, save the schema to a file, and parse untrusted data at the edge of your app: an API response, a webhook body, or JSON returned by a language model.

import { rootSchema } from './schema'

const response = await fetch('https://api.example.com/users')
const result = rootSchema.safeParse(await response.json())

if (!result.success) {
  console.error(result.error.issues)
} else {
  console.log(result.data.users.length) // fully typed
}

For model output, you can pass the same schema straight to a structured output API. See OpenAI structured outputs with Zod and validating an LLM JSON response.

Questions

Is my JSON uploaded anywhere?
No. Everything runs in your browser tab and the page makes no network request with your input, so it's safe to paste internal API responses.
How does it decide a field is optional?
When an array has several objects, the tool compares every item. If a key is missing from any item, it's marked optional. If it's null in one item and set in another, it's marked nullable. A single object doesn't give it enough to go on, so paste an array of several real records if you have them.
What is the difference between the Zod 4 and Zod 3 output?
Zod 4 moved string formats to top-level functions, so the output uses z.email(), z.url(), z.uuid() and z.iso.datetime(), plus z.int() and z.strictObject(). The Zod 3 output uses the chained forms such as z.string().email() and z.number().int().
Can I trust the generated schema as is?
Treat it as a starting point. A JSON sample can't tell the tool about constraints like minimum length, allowed enum values or which numbers have to be positive. Check the detected formats, then add the rules your app needs.