JSON to Zod Schema
Paste JSON and get a Zod schema that validates the same shape at runtime, with TypeScript types available through z.infer. Free, no signup, and generated locally in your browser.
16 lines · processed in your browser, nothing is uploaded
Zod output · root.schema.ts
Generated code appears here.Example: JSON to Zod
This JSON sample, with the root type named User, produces the Zod below using the default options.
{
"id": 1024,
"name": "Ada Lovelace",
"email": "ada@example.com",
"active": true,
"score": 98.5,
"tags": [
"admin",
"beta"
],
"address": {
"city": "London",
"postcode": "NW1 6XE"
}
}import * as z from "zod";
export const AddressSchema = z.object({
"city": z.string(),
"postcode": z.string(),
});
export type Address = z.infer<typeof AddressSchema>;
export const UserSchema = z.object({
"id": z.number(),
"name": z.string(),
"email": z.string(),
"active": z.boolean(),
"score": z.number(),
"tags": z.array(z.string()),
"address": AddressSchema,
});
export type User = z.infer<typeof UserSchema>;
How to convert JSON to Zod
- 1Paste a representative JSON sample into the input.
- 2Select Zod as the output language.
- 3Name the root schema, for example User.
- 4Copy the schema into your project, which needs the zod package installed.
- 5Call schema.parse() or safeParse() on incoming data.
One schema, both runtime checks and types
A Zod schema is a value, so it exists at runtime and can reject bad data. Derive the static type from it with type User = z.infer<typeof UserSchema> instead of maintaining a separate interface.
This keeps validation and types in sync: change the schema and every usage of the inferred type updates with it.
parse vs safeParse
schema.parse(data) returns typed data or throws a ZodError. schema.safeParse(data) never throws and returns an object with a success flag, which is easier to handle in request handlers and form code.
Validate at the boundaries of your app, such as fetch responses, webhook bodies and localStorage reads, rather than deep inside business logic.
Refining the generated schema
The generator infers basic types from your sample. Add the rules JSON cannot express, such as .email(), .min(1) or z.enum([...]) for fields that only take fixed values.
Fields missing from some records are generated as optional. If a field may be null, check that it uses .nullable(), which is different from .optional() in Zod.