JSON to Zod Schema Transformer — Numio
Generates a Zod schema from a JSON sample with the json-to-zod library. Each value maps to z.string(), z.number(), z.boolean(), z.null() or nested z.object(); empty arrays become z.array(z.unknown()).
Drafting a runtime validator for an API response you only have an example of.
When to use it
- Drafting a runtime validator for an API response you only have an example of.
- Adding Zod validation to a form or config loader without writing the schema by hand.
How to use
- Paste a representative JSON sample into the left editor.
- Open settings and set Root Schema Name if you want something other than schema.
- Copy the code, then tighten it: add .optional(), .email(), .min() and similar rules from your real spec.
JSON sample
{
"id": 42,
"email": "ada@example.com",
"roles": ["admin", "editor"],
"profile": { "nickname": null, "verified": true },
"tags": []
}Zod schema (TypeScript)
import { z } from "zod"
export const schema = z.object({
id: z.number(),
email: z.string(),
roles: z.array(z.string()),
profile: z.object({ nickname: z.null(), verified: z.boolean() }),
tags: z.array(z.unknown()),
})Details and limitations
Generation runs in the browser; the library is loaded on demand.
Types come from one sample. null becomes z.null() only, not .nullable(), and missing keys are never marked optional.
Arrays with different element shapes become z.union([...]) of each shape, which may need manual merging.
Formats such as email, date or UUID are not detected; they are all z.string().
FAQ
Can I convert a JSON array at the root?
Yes. A root array becomes z.array(...). If its objects have different keys, each distinct object becomes a member of a z.union.
How are null values typed?
As z.null(), which only accepts null. If the field can also hold a string, change it to z.string().nullable().
Does it add .optional() for missing fields?
No. Every key in the sample is required. Mark optional fields yourself.