iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💎

A Blazingly Fast Tool for Generating Zod Schemas from JSON

に公開

Introduction

Zod is becoming the de facto standard for validation libraries in TypeScript.
While its type inference is excellent and the developer experience is great, there is one tedious task.

"The work of defining schemas to match existing JSON data."

Handwriting z.object({ ... }) while looking at API responses or configuration file JSON is a repetitive task that can be exhausting.

So, I created a tool that instantly converts JSON to a Zod schema just by pasting it.

https://devtoolkits.app/ja/tools/json-to-zod

What kind of tool?

"JSON to Zod Converter" is a browser-based tool that outputs the corresponding Zod schema definition (TypeScript code) when you input JSON.

Features

  • 🚀 Blazing Fast Conversion: Converted the moment you input it.
  • 📦 Array & Nesting Support: Recursively interprets arrays of objects and deeply nested objects.
  • 🧩 Type Inference:
    • In addition to strings, numbers, and booleans, it also considers the possibility of null or undefined (depending on optional settings).
    • It intelligently infers types, such as z.array(z.string()) when array contents are uniform, or z.array(z.union([...])) when they are mixed.
  • 📋 Copy & Paste: Just paste the generated code directly into VSCode to start validating immediately.

How to Use

  1. Access JSON to Zod Converter.
  2. Paste the JSON on the left side.
  3. The generated Zod schema will be displayed on the right side.

Conversion Example

Input (JSON):

{
  "user": {
    "id": 1,
    "name": "Alice",
    "roles": ["admin", "user"]
  },
  "isActive": true,
  "lastLogin": null
}

Output (Zod Schema):

import { z } from "zod";

export const schema = z.object({
  user: z.object({
    id: z.number(),
    name: z.string(),
    roles: z.array(z.string())
  }),
  isActive: z.boolean(),
  lastLogin: z.null()
});

(* Root variable names can be changed in the option settings, or you can simply adjust the output code.)

Conclusion

Zod is powerful, but I hope this tool lowers the barrier (boilerplate writing) to getting started.
Please feel free to use it as a companion for your TypeScript development.

https://devtoolkits.app/ja/tools/json-to-zod

Discussion