JSON to Zod

Skip to editor
Explorer
Open Files

No files open

0 files open

jsoneditor.io v2

{
  "orderId": "ord_8F2K1",
  "status": "shipped",
  "customer": {
    "id": 4182,
    "name": "Ada Lovelace",
    "email": "ada@example.com"
  },
  "items": [
    {
      "sku": "KB-104",
      "name": "Mechanical keyboard",
      "qty": 1,
      "price": 89.99
    },
    {
      "sku": "MS-220",
      "name": "Wireless mouse",
      "qty": 2,
      "price": 24.5
    }
  ],
  "total": 138.99,
  "paid": true,
  "notes": null
}
JSONUTF-8
Spaces: 2

How do I generate a Zod schema from JSON?

To generate a Zod schema from JSON, paste a sample above, open Convert and press Zod. The schemas open in a new tab with the TypeScript type inferred from them at the bottom.

Each nested object gets its own schema, declared before the schemas that use it, so the file runs as it is. Keys missing from some items get .optional(), values that are sometimes null get .nullable() and whole numbers use z.number().int().

  1. Paste a sample JSON response into the editor.
  2. Open Convert and press Zod.
  3. Copy the schemas into your project.

Example

JSON
{
  "orderId": "ord_8F2K1",
  "status": "shipped",
  "customer": {
    "id": 4182,
    "name": "Ada Lovelace",
    "email": "ada@example.com"
  },
  "items": [
    {
      "sku": "KB-104",
      "name": "Mechanical keyboard",
      "qty": 1,
      "price": 89.99
    },
    {
      "sku": "MS-220",
      "name": "Wireless mouse",
      "qty": 2,
      "price": 24.5
    }
  ],
  "total": 138.99,
  "paid": true,
  "notes": null
}
Zod
import { z } from 'zod'

const CustomerSchema = z.object({
  id: z.number().int(),
  name: z.string(),
  email: z.string(),
})

const ItemSchema = z.object({
  sku: z.string(),
  name: z.string(),
  qty: z.number().int(),
  price: z.number(),
})

export const RootSchema = z.object({
  orderId: z.string(),
  status: z.string(),
  customer: CustomerSchema,
  items: z.array(ItemSchema),
  total: z.number(),
  paid: z.boolean(),
  notes: z.null(),
})

export type Root = z.infer<typeof RootSchema>

Questions people ask

How accurate is the generated Zod schema?

As accurate as your sample. The generator only knows what it can see, so a field that can be null but never is in your sample won't be marked nullable. Paste a response with a few varied items for the best result.

How do I validate data with the schema?

RootSchema.parse(data) returns typed data or throws with every problem listed, and RootSchema.safeParse(data) returns a result object instead of throwing.

Is my data uploaded anywhere?

No, this runs entirely in your browser, so nothing you paste is sent to a server. The only feature that sends data anywhere is the optional AI assistant, and only when you actually ask it something.