JSON to Zod
Paste or drop your data here, or open a file.
New to Bracely?
Everything stays in your browser.
Output appears here
Paste or open your data in the input box. It never leaves your browser.
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?
- Paste a JSON sample.
- Set the root type name, for example Order.
- 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:
- JSON to GraphQL
- JSON to Protobuf
- JSON to Pydantic
- JSON to TypeBox
- JSON to Valibot
- JSON to Yup
- JSON to Prisma
- JSON to Drizzle
- JSON to Scala
- JSON to Ruby
- JSON Schema builder
- JSON Schema validator
- JSON data dictionary
- OpenAPI from JSON
- MessagePack ⇄ JSON
- Parquet viewer
- JSON hash
- Edge-case test data
- SQL formatter
- HTML formatter
- JSON Formatter
- JSON Validator
- JSON Viewer
- JSON Repair
- JSON Diff
- JSON to YAML
- JSON to CSV
- jq Playground
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