Avoid mistakes when you convert JSON to a Zod schema by treating the generated code as a reviewable inference from one sample, not as proof of the production contract. A small, well-chosen fixture plus a deliberate review catches more problems than a large sample copied without inspection.
The JSON to Zod Schema Converter turns one valid JSON value into readable Zod source code, including an import, a schema declaration, and an inferred TypeScript type. It runs locally and does not upload the sample, but it emits source text only: the page does not load Zod or execute the generated schema.

Separate sample inference from contract truth
Schema inference describes only what the sample exposes. A required property means the key appeared in every sampled object; that does not mean the API requires it in every response. An optional property means it was missing from at least one sampled object; that does not mean the server may omit it in every case.
A field present in one example may still be optional in production, and a value absent from the sample may still be valid. Treat generated code as a draft for reconciliation with API documentation and broader fixtures, never as proof of the contract.
Choose a sample that represents the payload
Use one valid JSON object, array, or primitive. Select a known-good fixture that reflects the payload you intend to validate and includes the nested objects, arrays, nulls, and meaningful value variations relevant to that shape.
A representative sample is not necessarily the largest sample. Deep or very wide examples can be poor contract documentation, even when they process successfully. Keep the fixture focused and split independent payload shapes into separate schemas.
Audit every inferred choice
Generate the source before changing it by hand, then compare each inference with what the authoritative contract says. Pay particular attention to decisions that cannot be established from a value’s spelling alone.
| Observed JSON | Inferred Zod choice | Review question |
|---|---|---|
| String | z.string() | Does the contract require a format, enum, or other restriction? |
| Finite JSON number | z.number() | Is it an integer, bounded value, or exact decimal? |
| Boolean | z.boolean() | Is the field genuinely boolean across the contract? |
| Null by itself | z.null() | Is null the complete allowed type? |
| Homogeneous array | One element schema reused | Could a different element type arrive later? |
| Mixed non-null array values | Deterministic z.union([...]) | Are all inferred branches valid under the contract? |
| Null plus another observed type | Nullable form of the observed type | Does null mean present-but-null rather than absent? |
| Empty array | z.array(z.unknown()) | Which authoritative element type should replace unknown? |
| Objects merged from array samples | Required when always seen; optional when missing from at least one | Do the API documentation and broader fixtures agree? |
| Unsafe object key | Quoted with JSON string escaping | Will the original key survive validation unchanged? |
Arrays are inspected across every element within the input budget. Object properties follow source encounter order, while valid JavaScript identifier keys remain unquoted and unsafe names are escaped. Mixed values use deterministic ordering, identical schemas are deduplicated, and repeated conversion of the same JSON produces stable source.
Convert the sample with a controlled workflow
- Validate the input as strict JSON. The converter parses before inference and rejects JavaScript comments, trailing commas, NaN, Infinity, undefined, BigInt syntax, single-quoted strings, and object literals rather than silently repairing them. If needed, use the JSON Formatter to locate syntax errors first.
- Choose one representative value and a valid identifier. Paste the JSON value, then enter a valid JavaScript identifier for the exported schema, such as userResponseSchema. A name containing a hyphen, such as user-response, is not valid.
- Generate and inspect the source. Check optional fields, nullable values, union branches, numeric mappings, empty arrays, and quoted keys before copying. The output contains an import, schema declaration, and inferred TypeScript type.
- Compare inference with authoritative requirements. Check API documentation and broader fixtures. Set required and optional fields according to the contract, and add constraints only for documented behavior.
- Copy the source into the application project. Install and pin the intended Zod version there, format the generated source, and compile it. The converter page does not install or run Zod. Confirm construct names and validation APIs against the pinned version using the official Zod API documentation.
- Test accepted and rejected production cases. Run the representative fixture plus cases for missing required fields, null values, inferred union branches, and values that should fail. Compare every result with the expected behavior of the authoritative contract.
Keep optional and nullable fields distinct
Optionality and nullability solve different problems. An optional property may be absent, while a nullable property may be present with a null value. Conflating them commonly creates schemas that reject valid payloads or accept values the API never returns.
The converter marks a property optional when it is missing from at least one sampled object and keeps it required when it appears in every sampled object. When null is combined with another observed type, the type becomes nullable. A key can therefore be required and nullable, while an optional key does not automatically accept null.
For arrays containing objects, their keys are merged before presence is judged, and values observed under the same key are recursively merged. These results remain sample evidence, so verify them against the contract rather than treating them as fixed rules.
Preserve number and key semantics
JSON numbers have already passed through JSON.parse and therefore follow JavaScript number precision. Identifiers or currency requiring exact decimal semantics should usually remain strings; represent them that way in the source contract rather than relying on an imprecise numeric literal. A generated z.number() does not establish integer, minimum, maximum, or positivity constraints.
Likewise, z.string() does not establish email, URL, UUID, date, or enum behavior. Unsafe object keys are emitted with JSON string escaping so names containing spaces, hyphens, numeric starts, quotes, or other unsafe characters remain valid source and preserve their original values.
Add only contract-backed constraints
The generator deliberately 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.
Add these rules manually only when an authoritative contract states them. For example, replace z.array(z.unknown()) after learning the real element type rather than treating unknown as a business decision. If the sample is silent about a valid field, preserve that uncertainty until documentation or broader fixtures resolve it.
Test the schema in the destination project
A useful test set includes the representative fixture, a missing field that the contract says is required, a present null where null is allowed, a null where it is forbidden, each inferred union branch, and values that must be rejected after documented constraints are added. Include empty collections where the real API permits them and object keys that require escaping.
A schema can compile and still be wrong if its requiredness is too strict, its unions are too narrow, or its unknown elements remain unresolved. The generated source starts the validation process; it does not show that validation has passed.
Respect the converter’s hard limits
The converter has four explicit processing bounds:
| Resource | Maximum |
|---|---|
| Input length | 500,000 characters |
| Visited JSON values | 50,000 values |
| Nesting depth | 40 levels |
| Generated source length | 1,000,000 characters |
If any limit is exceeded, the converter returns an explicit error and emits no partial schema. Passing every bound still does not make a deep or very wide example good contract documentation. Prefer a small representative fixture and split independent payloads into separate schemas.
Run a final preflight check
- Confirm the input is strict JSON and represents the intended payload.
- Compare every required, optional, and nullable field with authoritative documentation.
- Review mixed arrays, empty arrays, numeric values, and escaped object keys.
- Add only constraints supported by the contract or broader fixtures.
- Pin Zod, format and compile the source, and test accepted and rejected cases.
That review turns the generated schema into a documented starting point rather than a false promise about the API.
For a deeper look, see Validate JSON in VS Code: Catch Syntax Errors Precisely.
For a deeper look, see Java Convert Properties to JSON: Reader-Style Workflow.