Xerobit

JSON to Zod Converter

Paste any valid JSON to generate a corresponding Zod schema. This json to zod converter handles nested objects, arrays, mixed types, null values, and unicode characters deterministically. It runs entirely in the browser, so nothing is uploaded to a server.

Paste a JSON object, array, or primitive value.

Zod Schema

export const schema = z.object({
  age: z.number(),
  name: z.string(),
});

How to use the json to zod converter

  1. Paste your JSON into the JSON input text area.
  2. Optionally set an Export name to wrap the result in a const declaration.
  3. The Zod schema appears instantly as you type.
  4. Copy the generated code into your TypeScript project.

How the conversion works

The converter parses your input with JSON.parse and walks the resulting tree recursively. Each JSON type maps directly to a Zod method:

  • Strings become z.string()
  • Numbers become z.number()
  • Booleans become z.boolean()
  • null becomes z.null()
  • Arrays become z.array(...), inferring the element type from the first item or using z.unknown() if empty
  • Objects become z.object({...}) with keys sorted alphabetically for deterministic output

When an array contains multiple objects, the converter merges all keys found across every element. Keys missing from any single element are marked with .optional().

Type mapping reference

This table shows how common JSON values translate into Zod schema calls.

JSON valueZod schema
"hello"z.string()
42z.number()
truez.boolean()
nullz.null()
[1, 2]z.array(z.number())
{"a": 1}z.object({ a: z.number() })

Mixed-type arrays (for example [1, "two"]) fall back to z.unknown() because Zod requires a single element type for arrays.

Handling nested objects and arrays

Nested structures are converted recursively. For example, given this JSON:

{"user": {"name": "Ada", "roles": ["admin"]}}

The converter produces:

z.object({ user: z.object({ name: z.string(), roles: z.array(z.string()) }) })

Keys at every level are sorted alphabetically. This ensures that running the same JSON through the tool always yields identical output, which is useful when regenerating schemas after API changes.

Common edge cases

A few situations require attention when generating a Zod schema from JSON:

  • Empty arrays produce z.array(z.unknown()) since there is no element to infer a type from.
  • Null values on their own map to z.null(). If the same field is null in one array element and a real value in another, the converter combines them into z.string().nullable() and similar. A single object with one null cannot reveal the intended type, so change that field by hand.
  • Unicode strings are preserved exactly as parsed by JSON.parse, including emoji and non-Latin scripts.
  • Invalid JSON surfaces the exact error message from the parser, making it easy to fix syntax issues before converting.

If you need to convert JSON into other formats, tools like json to go struct, json to c# class, or json to python class follow similar parsing logic but target different languages.

Using the generated schema

After copying the output, install Zod in your project with npm install zod. Import it and use the generated constant to validate incoming data:

import { z } from 'zod';

const result = schema.safeParse(incomingData);

if (!result.success) { console.error(result.error.format()); }

Because the converter outputs standard Zod syntax, the schema integrates directly with frameworks like tRPC, Fastify, or Express middleware without modification.

Why choose Zod for validation

Zod has become a standard choice for runtime validation in TypeScript projects because its schema definitions double as static types. When you define a schema, you can extract the corresponding TypeScript interface automatically using z.infer<typeof schema>. This eliminates the duplication that often occurs when maintaining separate interfaces and validation logic.

The json to zod workflow bridges the gap between raw API responses and strongly typed application code. Instead of writing schemas by hand, you paste a representative JSON payload and receive a working validator immediately. From there, you can refine the output by adding constraints such as .min(), .max(), or .email() to match your specific business rules.

Worked examples

ExampleInputResult
Simple user objectJSON input: {"name": "Ada", "age": 36, "active": true}; Export name: UserSchemaexport const UserSchema = z.object({ / active: z.boolean(), / age: z.number(), / name: z.string(), / });
Nested object with nullJSON input: {"id": 1, "profile": {"bio": null, "tags": ["dev", "admin"]}}; Export name: ProfileSchemaexport const ProfileSchema = z.object({ / id: z.number(), / profile: z.object({ / bio: z.null(), / tags: z.array(z.string()), / }), / });
Array of objectsJSON input: [{"x": 1}, {"x": 2, "y": 3}]z.array(z.object({ / x: z.number(), / y: z.number().optional(), / }))

FAQ

Does this tool upload my JSON to a server?

No. The json to zod conversion happens entirely in your browser using JavaScript. Your data never leaves your device.

How are optional fields determined?

When an array contains multiple objects, the converter collects all keys across every object. Any key that is missing from at least one object is marked with .optional() in the resulting Zod schema.

What happens if my JSON is invalid?

The tool uses JSON.parse internally. If parsing fails, it displays the exact error message returned by the parser so you can correct the syntax.

Can I convert a JSON array directly?

Yes. Pasting a JSON array produces a z.array(...) schema. If the array contains objects, their keys are merged and inferred just like a top-level object.

Why are the object keys sorted alphabetically?

Sorting keys ensures deterministic output. Running the same JSON through the converter multiple times always produces identical text, which helps with version control and diffing.

Does it handle null values correctly?

A null value on its own becomes z.null(). When the same field is null in one array element and a real value in another, the converter combines them into a nullable schema such as z.string().nullable(). A single object with one null cannot reveal the intended type, so adjust that field by hand.