Skip to content

JSON to Zod Schema Converter

Infer a reviewable Zod schema from one JSON sample, including nested objects, arrays, unions, nullable values, and optional merged fields.

Privacy: your files never leave your device. All processing happens locally in your browser.

How to use

  1. 1.Paste one valid and representative JSON value and enter a valid schema identifier.
  2. 2.Generate the Zod source, then inspect optional, nullable, union, numeric, and empty-array choices.
  3. 3.Copy the source into a project with Zod installed and test it against the authoritative data contract.

About JSON to Zod Schema Converter

JSON to Zod Schema Converter turns one valid JSON value into readable Zod source code. Paste an object, array, or primitive, choose a valid JavaScript identifier for the exported schema, and generate an import, schema declaration, and inferred TypeScript type. The output is plain source text for review and copying; this page does not load Zod, execute generated code, or upload the sample.

Primitive JSON values map to the corresponding documented Zod constructors: strings to z.string, finite JSON numbers to z.number, Booleans to z.boolean, and null to z.null. Objects become z.object calls whose properties follow source encounter order. Keys that are valid JavaScript identifiers remain unquoted; spaces, hyphens, numeric starts, quotes, and other unsafe names are emitted with JSON string escaping so the generated object shape remains valid source.

Arrays are inferred from every element within the stated input budget. Homogeneous samples produce one element schema. Mixed primitive or structural samples produce a deterministic z.union. Null combined with another observed type becomes nullable. An empty array has no evidence about its future element type, so it deliberately becomes z.array(z.unknown()) instead of guessing string or never. Nested arrays and objects are handled recursively.

When an array contains objects, their keys are merged. A property observed in every sampled object remains required. A property missing from at least one object becomes optional, and values observed under the same key are recursively merged. This is sample inference, not a proof of the real contract. A field present in one example may still be optional in production, and a value absent from the sample may be valid. Review the result against API documentation and representative fixtures before validation.

The generator does not infer string formats, minimums, maximums, integer constraints, enums, literals, discriminated unions, dates, UUIDs, URLs, email addresses, brands, coercions, defaults, transformations, refinements, records, tuples, strict-object behavior, recursive references, or business rules. It avoids those guesses because spellings and small samples cannot establish intent. Add such rules manually only from an authoritative contract.

Mixed values are ordered deterministically so repeated conversion of the same JSON produces stable source. Identical schemas are deduplicated. JSON parsing happens before inference, so JavaScript comments, trailing commas, NaN, Infinity, undefined, BigInt syntax, single-quoted strings, and object literals are rejected rather than silently repaired. Numbers have already passed through JSON.parse and therefore follow JavaScript number precision; identifiers or currency requiring exact decimal semantics should usually remain strings.

Work is bounded at 500,000 input characters, 50,000 visited values, 40 nesting levels, and 1,000,000 generated characters. A limit returns an explicit error and never emits a partial schema. Deep or very wide examples can still be poor contract documentation even under those limits. Prefer a small representative fixture and split independent payloads into separate schemas.

The eight external syntax fixtures cover all JSON primitives, homogeneous and mixed arrays, required object properties, and an optional field created by merging object samples. Additional tests cover nullability, quoted property names, exported type naming, invalid JSON, and invalid schema identifiers. After copying, install and pin the intended Zod version in the destination project, format the source, compile it, and test both accepted and rejected production cases.

Methodology & sources

Parses strict JSON, recursively maps primitives and structures to documented Zod expressions, merges array element schemas, marks missing object fields optional, turns null-plus-type into nullable, quotes unsafe keys, deduplicates deterministic unions, and fails before any silent truncation.

Frequently asked questions

Does the generated schema prove my API contract?
No. It describes observations in one sample. Required fields, formats, ranges, enums, and business rules must be checked against authoritative documentation and broader fixtures.
Why does an empty array use z.unknown()?
An empty sample provides no evidence about element type. Unknown is an explicit uncertainty signal and avoids inventing a string, number, or never constraint.
Does this page install or run Zod?
No. It emits source text only. Install the intended Zod version in your own project, compile the output, and run validation tests there.

Developer Tools guides

View all