01What is JSON to Zod?
Zod is a TypeScript library that checks data at runtime and gives you a static type from the same schema. Writing that schema by hand for a big payload is slow and easy to get wrong. This converter reads a JSON sample and writes the schema for you. The output starts with import { z } from "zod". Then it defines one const for each nested object, named after its key, like addressSchema. If two objects have exactly the same shape, they share one schema, so billing can reuse addressSchema. A name that's already taken gets a number added. Last comes the exported root schema and a type built with z.infer. One paste gives you both validation and a TypeScript type. The type mapping is simple and predictable. Strings become z.string(). Whole numbers become z.number().int(), other numbers z.number(), and booleans z.boolean(). A null in the sample becomes z.null() literally. Arrays become z.array() of their element schema. If the elements differ, you get z.array(z.union([...])). An array mixing 1 and 1.5 just becomes z.array(z.number()), and an empty array becomes z.array(z.unknown()). Keys that aren't valid JavaScript identifiers, such as "first-name", are quoted in the object. Generated const names are always valid, so a 2fa key gets _2faSchema. Top-level arrays and primitives work too: the input 42 with Root name count gives countSchema = z.number().int(). The Root name field sets both the schema constant and the type name, and my-thing becomes myThingSchema and MyThing. The default, root, gives rootSchema and Root, and a name that already ends in Schema isn't doubled: UserSchema gives userSchema and User. The calls it emits (z.object, z.array, z.union, z.infer) exist in both Zod 3 and Zod 4, per the official Zod docs. Here's a typical run. Paste {"id": 1, "email": "[email protected]", "score": 4.5} with Root name user. You get userSchema with id: z.number().int(), email: z.string() and score: z.number(), followed by export type User = z.infer<typeof userSchema>. The limits are worth knowing before you ship it. The schema matches one sample, so every field is required. Formats aren't detected, so emails, URLs, UUIDs and ISO dates all stay plain z.string(). A null field accepts only null until you change it. Objects in an array with different keys produce a union, not one object with optional fields. All of this runs as plain JavaScript in your browser tab, and the JSON you paste isn't uploaded.
TypeScript developers who want a runtime validator and a matching type for a JSON payload without writing the Zod schema by hand. The tool runs entirely in your browser, so your data never leaves your device.
02How to use JSON to Zod
- Paste a JSON sample into JSON Input.
- Set Root name, for example user, to name the schema and the type.
- Read the generated code in Zod Schema Output.
- Click Copy and paste it into a .ts file in a project with zod installed.
- Tighten the inferred rules by hand where your data allows.
03Worked examples
- Input:
{"id":1,"email":"[email protected]","score":4.5,"active":true,"tags":["x"]} (Root name: user) - Result:
import { z } from "zod"; export const userSchema = z.object({ id: z.number().int(), email: z.string(), score: z.number(), active: z.boolean(), tags: z.array(z.string()), }); export type User = z.infer<typeof userSchema>; - Whole numbers get .int(), decimals don't. The type comes from z.infer.
- Input:
{"first-name":"A","meta":null,"items":[]} (Root name: user) - Result:
export const userSchema = z.object({ "first-name": z.string(), meta: z.null(), items: z.array(z.unknown()), }); - Import and type lines trimmed. The invalid identifier is quoted, null is z.null() and the empty array is z.array(z.unknown()).
- Input:
[{"id":1,"n":"x"},{"id":2}] (Root name: user) - Result:
export const userSchema = z.array(z.union([ z.object({ id: z.number().int(), n: z.string(), }), z.object({ id: z.number().int(), }) ])); - Import and type lines trimmed. Different element shapes produce a union, not optional fields.
- Input:
{"address":{"city":"Oslo"},"billing":{"city":"Bergen"},"2fa":{"on":true},"nums":[1,1.5]} (Root name: my-thing) - Result:
const addressSchema = z.object({ city: z.string(), }); const _2faSchema = z.object({ on: z.boolean(), }); export const myThingSchema = z.object({ address: addressSchema, billing: addressSchema, "2fa": _2faSchema, nums: z.array(z.number()), }); export type MyThing = z.infer<typeof myThingSchema>; - Import line trimmed. billing reuses addressSchema, 2fa gets a valid const name, and mixed 1 and 1.5 become z.number().
- Input:
{"created":"2024-01-05T10:00:00Z","url":"https://x.io","id":"550e8400-e29b-41d4-a716-446655440000"} (Root name: user) - Result:
export const userSchema = z.object({ created: z.string(), url: z.string(), id: z.string(), }); - Import and type lines trimmed. Dates, URLs and UUIDs aren't detected, so add format checks yourself.
- Input:
42 (Root name: count) - Result:
import { z } from "zod"; export const countSchema = z.number().int(); export type Count = z.infer<typeof countSchema>; - A top-level primitive works too.
04Common use cases
- Validating a third-party API response in a Next.js route handler before trusting it
- Writing a schema for form data handled with React Hook Form and zodResolver
- Checking a JSON config file at app startup so a typo fails fast
- Defining tRPC input and output schemas from example payloads
- Validating structured JSON output from an LLM before using it in application logic
- Replacing a hand-written TypeScript interface with a schema plus z.infer type
05Tips
- A null in the sample becomes z.null(), which rejects real values. Change it to something like z.string().nullable() once you know the type.
- Swap z.string() for z.email(), z.url() or z.uuid() in Zod 4. In Zod 3 the forms are z.string().email() and so on.
- Objects in an array with different keys produce a union. If they are really one shape, merge them into one z.object and mark the missing fields .optional().
- z.object strips unknown keys by default. Add .strict() if extra fields should fail validation.
- Use .safeParse() at API boundaries so a bad payload returns an error object instead of throwing.
06Related concepts
Zod, runtime validation, schema inference, z.infer, TypeScript types.
Frequently Asked Questions
What is JSON to Zod?
JSON to Zod is a free, browser-based developer tool on AIDToolStack. Convert JSON to Zod schemas It requires no installation or sign-up and works entirely in your browser.
Is JSON to Zod free to use?
Yes. No sign-up, no fees and no usage cap.
Does JSON to Zod store my data?
No. It runs in your browser; nothing is uploaded.