JSON to Zod
Paste a JSON sample. Get Zod types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.
An example
This JSON:
{
"id": 42,
"name": "Ada Lovelace",
"active": true,
"joined": "2020-01-02T03:04:05Z",
"tags": ["engineer", "writer"],
"address": { "city": "London", "postcode": null },
"scores": [9.5, 8, 10]
}
becomes this Zod:
import * as z from "zod";
export const AddressSchema = z.object({
"city": z.string(),
"postcode": z.null(),
});
export type Address = z.infer<typeof AddressSchema>;
export const RootSchema = z.object({
"id": z.number(),
"name": z.string(),
"active": z.boolean(),
"joined": z.coerce.date(),
"tags": z.array(z.string()),
"address": AddressSchema,
"scores": z.array(z.number()),
});
export type Root = z.infer<typeof RootSchema>;
With the reader and the writer
The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.
import * as z from "zod";
export const AddressSchema = z.object({
"city": z.string(),
"postcode": z.null(),
});
export type Address = z.infer<typeof AddressSchema>;
export const RootSchema = z.object({
"id": z.number(),
"name": z.string(),
"active": z.boolean(),
"joined": z.coerce.date(),
"tags": z.array(z.string()),
"address": AddressSchema,
"scores": z.array(z.number()),
});
export type Root = z.infer<typeof RootSchema>;
What you need in your project
`npm install zod`.
A Zod schema is the answer to the hole in a TypeScript type: the type is removed when the program is built, and the schema is not. The schema checks the data when the program runs.
The last line gives you the type as well: `z.infer<typeof Schema>`. So one source gives both the check and the type, and they cannot drift apart.
Use it where the data enters your program: an API answer, a form, a message from a queue, a file on disk.
The trap to know about
A date in JSON is text. `z.coerce.date()` turns it into a Date, but it accepts anything a Date accepts, which is more than ISO-8601. Tighten it when the format matters.
The options you can change
These belong to Zod, so the row of switches changes with the language.
| Option | What it does | Default |
|---|
Questions
How do I turn JSON into Zod?
Paste your JSON into the box, choose Zod, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.
Does my JSON go to a server?
No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.
What do I need to add to my Zod project?
`npm install zod`.
How does it know which fields are optional?
From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.
Other languages
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to Code