JSON to Zod

Input
Output

JSON to Zod

Paste a JSON sample, such as an API response, and Bracely writes Zod schemas and TypeScript types for it. Each object becomes a z.object() schema, with an inferred TypeScript type exported next to it.

Example: validating a sign-up form's request body

Two sign-up requests, one with a referral code and one without. Each object becomes a z.object() schema in dependency order, referralCode becomes .optional(), and a TypeScript type is inferred from each schema with z.infer, so the type and the validation can't drift apart:

Input

[
  {
    "email": "[email protected]",
    "password": "correct-horse-battery",
    "age": 29,
    "marketing": true,
    "address": {
      "city": "Pune",
      "country": "IN"
    },
    "referralCode": "FRIEND10"
  },
  {
    "email": "[email protected]",
    "password": "another-long-pass",
    "age": 34,
    "marketing": false,
    "address": {
      "city": "Delhi",
      "country": "IN"
    }
  }
]

Output

import { z } from "zod";

export const AddressSchema = z.object({
  city: z.string(),
  country: z.string(),
});
export type Address = z.infer<typeof AddressSchema>;

export const SignupRequestSchema = z.object({
  email: z.string(),
  password: z.string(),
  age: z.number().int(),
  marketing: z.boolean(),
  address: AddressSchema,
  referralCode: z.string().optional(),
});
export type SignupRequest = z.infer<typeof SignupRequestSchema>;

export const SignupRequestListSchema = z.array(SignupRequestSchema);
export type SignupRequestList = z.infer<typeof SignupRequestListSchema>;

How do I use the JSON to Zod?

  1. Paste a JSON sample.
  2. Set the root type name, for example Order.
  3. Copy the Zod code into your project.

What does the JSON to Zod do?

  • z.object schemas in dependency order
  • z.infer types exported for each schema
  • .optional() for fields that can be missing
  • Nested objects become their own named types
  • 20 other languages from the same sample, including TypeScript

When to use the JSON to Zod

TypeScript types disappear at runtime, so a response or form body that doesn't match them fails somewhere deep in your code. A Zod schema checks the data when it arrives, and z.infer gives you the TypeScript type from the same schema, so the two can't drift apart. Generating the schema from a real request or response gets you there in one step.

Each object becomes a z.object() schema, written in dependency order so every schema is defined before it's used, with an exported type beside it. Whole numbers become z.number().int(), a field missing from some items gets .optional(), a value that's sometimes null gets .nullable(), and values whose types disagree become z.unknown(). The sample can't tell a plain string from an email or a URL, so add refinements like .email() or .url() where they belong.

Frequently asked questions

How do I use the schema?
Call SignupRequestSchema.parse(body) to get typed data or an error, or safeParse to handle the error yourself. Use the exported SignupRequest type everywhere else.
Zod or the other validators?
Zod is the most widely used. Pick TypeBox for JSON Schema output, Valibot for the smallest bundle, or Yup if your forms already use it: all are in Language, from the same sample.
How does it pick Zod types?
From the values in your sample: whole numbers, decimals, text, true/false, arrays and nested objects. When array items disagree, the field is made optional or widened.
Can I generate other languages?
Yes. Change Language to any of the others: TypeScript, Go, Python, Pydantic, Java, C#, Kotlin, Swift, Rust, Scala, Ruby, Dart, PHP, TypeBox, Valibot, Yup, GraphQL, Protobuf, Prisma and Drizzle.
Is my data uploaded?
No. Bracely runs entirely in your browser, so nothing you paste or open leaves your device.

More JSON tools

Bracely has 108 JSON tools in one page. Related ones:

Runs entirely in your browser. Nothing you paste is uploaded. Made by Saurabh Pagrut · About · Privacy policy
Last updated · Version 6.7 · Changelog
Saurabh Pagrut on GitHub · LinkedIn · X · YouTube · Instagram