Trexmi
Converter Ready

JSON to Zod Schema

Convert representative JSON into a Zod schema with nested objects, arrays, primitive types, null values, and optional strict object handling.

Generate an importable Zod schema Infer nested object properties Infer arrays from their first item Preserve non-identifier JSON keys
A sample cannot reveal optional fields, enums, formats, refinements, or every array shape. Review the schema before production use.
INPUT Sample JSON *
0 chars0 words0 lines

Tool settings

Ctrl / ⌘ + Enter
Zod schema Compile and test the generated schema with your installed Zod version.
About the tool

What JSON to Zod Schema does

JSON to Zod Schema turns a sample JSON value into TypeScript code using Zod. Objects become z.object() or z.strictObject(), arrays become z.array(), and primitive JSON values become the corresponding Zod schemas.

The generated schema is a starting point based only on observed values. It cannot infer whether a property may be omitted, whether a string is an email or date, whether a number has a range, or whether an array contains more than one valid shape. Empty objects remain object schemas; empty arrays use z.unknown() for their item type until you replace it.

JSON converted into a nested Zod validation schema
Generate a Zod scaffold from JSON, then add the rules the sample cannot express.

How to use

  1. Add representative JSON. Include nested objects, arrays, booleans, numbers, and nulls that reflect real payloads.
  2. Name the schema. Choose a stable TypeScript identifier such as UserSchema.
  3. Choose object behavior. Standard z.object() strips unrecognized keys; strict objects reject them.
  4. Generate the code. Review keys and inferred value types from top to bottom.
  5. Complete the contract. Add optional, nullable, enum, format, length, range, union, transform, and refinement rules.
Built for the task

Why use JSON to Zod Schema?

Focused controls, predictable output, and a workflow designed around this exact transformation.

01

Import-ready scaffold

Receive a Zod import and exported schema constant ready for a TypeScript project.

02

Nested inference

Generate recursive objects and array item schemas from the sample structure.

03

Unknown-key choice

Choose standard object stripping or strict rejection for every generated object.

04

Valid property syntax

JSON property names are emitted as quoted keys, including spaces and punctuation.

Useful answers

Questions about JSON to Zod Schema

Practical details about input, output, privacy, limits, and the best way to use this tool.

01 Does the generator use Zod 4 syntax?

It generates current core APIs such as z.object, z.strictObject, z.array, and primitive schemas. Verify the code with the installed Zod version in your project.

02 Are generated object fields required?

Yes. Zod object properties are required by default. Add .optional() only where the real contract allows omission.

03 What is the difference between standard and strict objects?

Standard z.object() strips unrecognized keys during parsing. z.strictObject() rejects unrecognized keys.

04 How are null values handled?

An observed null becomes z.null(). Replace it with a nullable union such as z.string().nullable() when both a real value and null are allowed.

05 How are empty arrays handled?

An empty array provides no item evidence, so the generator emits z.array(z.unknown()). Replace unknown with the intended item schema.

06 Can it infer email, URL, UUID, or date validation?

No. JSON stores those values as strings. Add the appropriate Zod format rule only when the API contract confirms it.

07 Can one example reveal optional properties or unions?

No. Missing variants, heterogeneous arrays, discriminated unions, defaults, and transformations require manual schema design.

08 Does the tool run or install Zod?

No. It generates code only. Install Zod, compile the TypeScript, and test valid and invalid payloads in your project.

Learn JSON

Read the complete JSON Guide

Learn JSON objects, arrays, value types, validation, formatting, parsing, API workflows, and common syntax errors.

  • Objects, arrays, and values
  • Validation and formatting
  • API and parsing workflows
Read guide Practical explanations and examples

Examples

Generate a nested user schema

Nested objects are represented recursively and all observed keys are required by default.

Input
Name: UserSchema
Strict: off
JSON: {"id":42,"name":"Ada","active":true,"profile":{"city":"Kyiv"}}
Output
import { z } from "zod";\n\nexport const UserSchema = z.object({\n  "id": z.number(),\n  "name": z.string(),\n  "active": z.boolean(),\n  "profile": z.object({\n    "city": z.string()\n  })\n});

Reject unknown object keys

Strict mode uses z.strictObject at every generated object level.

Input
Name: ProductSchema
Strict: on
JSON: {"sku":"A-10","price":19.95}
Output
export const ProductSchema = z.strictObject({\n  "sku": z.string(),\n  "price": z.number()\n});

Infer an array item

A non-empty array uses its first item as the initial item schema.

Input
Name: OrderListSchema
JSON: [{"id":101,"paid":true}]
Output
export const OrderListSchema = z.array(z.object({\n  "id": z.number(),\n  "paid": z.boolean()\n}));

Keep empty objects distinct from arrays

The generator no longer treats an empty JSON object as an empty array.

Input
Name: ConfigSchema
JSON: {"settings":{},"items":[]}
Output
settings → z.object({})\nitems → z.array(z.unknown())

How JSON values become Zod schemas

Strings, booleans, numbers, and null map to z.string(), z.boolean(), z.number(), and z.null(). JSON has one numeric data type, so an integer sample does not prove that decimals are forbidden. Add integer and range constraints from documentation, not appearance.

Objects are processed recursively. JSON keys are quoted in the generated TypeScript, so names such as user-id remain valid properties without being renamed.

Required fields and unknown object keys

All observed object properties are required because Zod object properties are required by default. A sample cannot demonstrate a field that was omitted. Add .optional() deliberately and distinguish omission from an explicit null value.

Standard Zod objects strip unknown keys from parsed output. Enable strict objects when extra input must cause an error. Neither choice can be inferred safely from one sample.

Arrays, nulls, and mixed shapes

The first array item provides the generated item schema. A later item with another type or object shape is invisible to this inference rule. Empty arrays use unknown items. Review arrays for unions, tuples, minimum lengths, and heterogeneous records.

A null sample produces only z.null(); it does not reveal the intended non-null type. Use nullable or nullish helpers according to whether the field may be null, omitted, or both.

What the generator cannot infer

The generator does not infer optional fields, literal values, enums, discriminators, formats, branded types, defaults, coercion, preprocessing, transforms, refinements, recursive schemas, descriptions, or custom error messages. Generated code is not executed by Trexmi.

Compare the shape with JSON to TypeScript, validate contract rules with JSON Schema Validator, or generate Python models with JSON to Pydantic Model. For function arguments, use JSON to OpenAI Function Schema.

Official Zod and TypeScript references

Review the current Zod API documentation and TypeScript type documentation.