跳到主要内容

JSON 转 Zod Schema

UpBox优盒工具箱,免费在线 JSON 转 Zod Schema 工具,自动推断类型、嵌套对象与数组,识别 email / URL / UUID,生成可直接使用的 TypeScript 校验代码。

无需注册 · 数据不上传No sign-up · nothing uploaded

源 JSONSource JSON

Zod SchemaZod schema

粘贴 JSON 后自动生成
粘贴 JSON 后自动生成

数组元素缺失的字段自动标记为 optional

本工具所有运算在本地浏览器完成,你的文件、文本不会上传到服务器

工具介绍

把 JSON 粘贴进来,工具会生成 `z.object({...})` 结构,数字区分整数与浮点,数组用 `z.array()`,可选字段追加 `.optional()`。 Paste JSON and the tool builds z.object({...}) shapes, distinguishing integers from floats, wrapping arrays in z.array(), and appending .optional() to optional fields.

开启格式识别后,email、URL、UUID 字段会自动带上对应校验;同时输出 schema 常量与 `z.infer` 类型,省去手写接口定义。 With format detection on, email, URL and UUID fields get their validators; the schema constant and z.infer type are both emitted, so no hand-written interface is needed.

  • 类型自动推断Automatic type inference
  • 格式校验识别Format validation detection
  • 嵌套与数组Nested objects and arrays
  • 类型导出Type export
  • 本地处理Local processing

使用说明

  1. 粘贴 JSON 自动生成 schema。Paste JSON to generate the schema.
  2. 修改 Schema 名称,按需勾选可选字段。Rename the schema and toggle optional fields.
  3. 关闭格式识别可输出纯 z.string()。Turn format detection off for plain z.string().
  4. 复制或下载 .ts 文件。Copy or download a .ts file.

常见问题

需要安装 zod 吗?Do I need zod installed?
需要。生成的代码依赖 zod,请先 `npm i zod`,仅输出代码不回传数据。Yes — run npm i zod first; only code is produced, no data leaves the page.
数组元素结构不同怎么办?What if array items differ?
字段会合并,缺失字段自动 `.optional()`,类型按首个出现的值推断。Fields are merged, missing ones become .optional(), and types follow the first value.
日期字段会自动转 Date 吗?Are date fields converted to Date?
不会,JSON 中的日期仍是字符串,可生成后手动改成 `z.coerce.date()`。No — dates stay strings; switch to z.coerce.date() manually if needed.
数据会上传吗?Is data uploaded?
不会,解析与生成都在本地完成。No, parsing and generation are local.
广告位

此处为广告位,可放置你的推广素材

支持图片、文字或 HTML 代码,替换本区块内容即可投放

浏览其他分类