Skip to content
cosmodev ~/tools/json-to-zod

JSON sang Schema Zod

Converters & Formatters

Tạo schema xác thực Zod từ JSON. Suy luận z.string, z.number, z.boolean, z.object, z.array, z.null và z.union cho mảng hỗn hợp.

(Tài liệu bằng tiếng Anh)

What it does

Generate a Zod validation schema from JSON. Scalars map to their validator (z.string, z.number, z.boolean, z.null), objects recurse into inline z.object({...}), arrays become z.array(...) — and a mixed-type array becomes z.union([...]) of each distinct element type. Drop the output straight into a TypeScript project that uses Zod for runtime validation: API responses, config files, form payloads, fixtures. 100% client-side.

How to use it

  1. Paste JSON into the JSON box (a sample is prefilled — Sample restores it, Clear empties it).
  2. Optionally set a Root name (defaults to Root, used for the const Root = ... binding).
  3. The schema renders live on the right, pinned while you scroll the input.
  4. Copy it, or Download it as schema.ts.

Examples

A flat object — {"id":1,"name":"Ada","active":true,"tags":["a","b"]} →

const Root = z.object({
  id: z.number(),
  name: z.string(),
  active: z.boolean(),
  tags: z.array(z.string()),
});

Nested objects recurse inline — {"user":{"name":"Ada"}} →

const Root = z.object({
  user: z.object({
    name: z.string(),
  }),
});

Mixed arrays become array-of-union — {"xs":[1,"a"]} →

const Root = z.object({
  xs: z.array(z.union([
    z.number(),
    z.string(),
  ])),
});

Edge cases — empty arrays become z.array(z.unknown()); null maps to z.null(); anything the parser rejects shows in the error box instead of the schema.

Good to know

  • Private: parsing is local — safe for sensitive payloads.
  • The generated schema is a starting point: add .min()/.max() refinements where your real data has constraints the sample didn’t show.
  • Related tools: JSON to TypeScript, JSON Validator.
Mã nguồn14 ngôn ngữ

100% phía client · dữ liệu của bạn không bao giờ rời khỏi trình duyệt. Thấy hữu ích? Chia sẻ với đồng nghiệp của bạn.