"category": "code-generator",
JSON Schema to Zod Converter
"tldr": Paste a JSON Schema and get a Zod schema with the inferred TypeScript type - runtime validation and static types from one source.
Paste a JSON Schema and get an equivalent Zod schema: z.object with .optional() derived from the required array, z.enum for string enums, .nullable() for ["type","null"] arrays, and z.number().int() preserving the integer constraint - plus the inferred TypeScript type via z.infer.
Zod has become the default validation layer in the TypeScript ecosystem (tRPC, React Hook Form, OpenAI SDKs) because one definition yields both the runtime check and the static type. If your contracts live as JSON Schema, this conversion bridges them into that ecosystem.
How to convert JSON Schema to Zod
- 1Paste your JSON Schema into the input panel.
- 2Properties outside required get .optional(); null-including type arrays get .nullable().
- 3integer becomes z.number().int(); string enums become z.enum([...]).
- 4Copy the schema and parse with RootSchema.parse(data) - invalid data throws with precise paths.
Convert JSON Schema to Zod schema in code
const result = RootSchema.safeParse(await res.json());
if (!result.success) {
console.error(result.error.issues); // [{ path: ["score"], message: ... }]
return;
}
result.data.name; // fully typedexport const RootSchema = z.object({ /* generated */ });
export type Root = z.infer<typeof RootSchema>;
// no separate interface to keep in syncFrequently asked questions
parse or safeParse - which should I use?
safeParse for external input you expect to sometimes be invalid (API responses, form data) - it returns a discriminated result instead of throwing. parse for cases where invalid data is a bug and an exception is the right behavior. Both give you structured error paths.
What is the difference between .optional() and .nullable()?
Mirrors JSON Schema exactly: .optional() means the key may be absent (not in required); .nullable() means the value may be null (type includes "null"). A field can be both - absent, null, or a value are three distinct states APIs often distinguish.
Are validation constraints like minLength carried over?
The structural shape and integer constraint are. String/number constraints (minLength, pattern, minimum) are not expanded automatically - add them as .min(), .regex(), .gte() on the generated fields. For full-fidelity conversion of constraint-heavy schemas, the json-schema-to-zod npm package covers more keywords.
Can I go the other way - Zod to JSON Schema?
Yes: Zod 4 has z.toJSONSchema() built in, and the zod-to-json-schema package covers earlier versions. Teams commonly keep Zod as the source of truth in TypeScript services and export JSON Schema for cross-language consumers.
Last updated:
You might also need
Free and in-browser, like everything on JSON Console. Browse all tools →