Skip to main content

JSON to Zod

Generate Zod schemas from JSON

JSON input

Limits: 5 MiB per file or 5,242,880 characters, 256 nesting levels.

Zod schema

Every array item is inspected: differing types become z.union(...), and keys missing from some objects become .optional(). Treat the result as a starting point and tighten it with formats, enums or limits.

How to use

  1. 1.Paste sample JSON, upload a .json file, drop a file onto the editor, or load the example.
  2. 2.Set the root schema name, for example UserSchema; the exported type becomes User.
  3. 3.Review the generated code and any notices about uncertain types.
  4. 4.Copy the code or download it as a .ts file. Clear empties the editor.

What JSON to Zod does

JSON to Zod turns a sample JSON payload into a Zod schema you can use for runtime validation in TypeScript. The JSON is first inferred into a library-neutral schema tree and then emitted as Zod code, so the result covers strings, numbers, booleans, null, nested objects, arrays and arrays of objects. The download is a ready-to-import .ts module with import { z } from "zod", the exported schema and an exported type built with z.infer.

How types are inferred
  • •Every array item is inspected, not just the first one. Items of different kinds become z.union([...]).
  • •Objects inside the same array are merged: a key missing from some objects becomes .optional(), and a key that is sometimes null becomes a union with z.null().
  • •An empty array has no evidence, so its items are z.unknown() and a notice is shown.
  • •A value that is only ever null stays z.null() with a notice, because the real type cannot be known from the sample.
  • •Keys are always quoted, so names such as "user-id" or "__proto__" are handled safely.
Limits and accuracy

A sample shows what data looked like once, not the full contract. Add formats (email, url, datetime), enums, min/max or .strict() yourself where your API guarantees them. Integers beyond JavaScript's safe range are flagged because they lose precision when parsed. Input is limited to 5 MiB and 256 nesting levels, and everything runs in your browser.

Frequently asked questions
How are optional fields and unions detected?
Every item of an array is inspected. If objects in the same array have different keys, keys missing from some objects become .optional(). If a value has different types across items, the schema uses z.union([...]); a key that is sometimes null becomes a union with z.null().
What happens with empty arrays or values that are always null?
A sample cannot reveal their real type, so empty arrays use z.array(z.unknown()) and null-only values use z.null(). Both are listed as notices with their JSON path so you can replace them with the real type.
Which Zod version does the generated code target?
The output only uses core APIs available in current Zod releases: z.object, z.array, z.union, z.string, z.number, z.boolean, z.null and z.unknown, plus an exported type built with z.infer. The .ts file starts with import { z } from "zod".

CodingTool is officially live on