AZ Tools

JSON Schema 生成器

开发

遍历粘贴的 JSON,推断类型(integer / number / string / boolean / null / array / object),检测常见字符串格式(email、URI、UUID、IPv4、date、date-time),输出可用于验证的 JSON Schema。可选地将每个属性标记为 required 并把值嵌入 `examples`。

JSON Schema (draft-07)
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "user": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string",
          "format": "email"
        },
        "verified": {
          "type": "boolean"
        },
        "roles": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "address": {
          "type": "object",
          "properties": {
            "street": {
              "type": "string"
            },
            "city": {
              "type": "string"
            }
          },
          "required": [
            "street",
            "city"
          ]
        }
      },
      "required": [
        "id",
        "name",
        "email",
        "verified",
        "roles",
        "address"
      ]
    }
  },
  "required": [
    "user"
  ]
}

使用方法

  1. 粘贴 JSON 样本到输入框。
  2. 选择所有属性是否都标为 required(适合严格 API 契约)或仅现有键(适合部分 / patch 负载)。
  3. 可选把值作为 `examples` 包含,使生成的文档在类型旁显示样本。

常见问题

面向哪个 draft?
draft-07 — 在验证器、代码生成器和 Stoplight / Redocly 等编辑器工具中支持最广的版本。输出也与 OpenAPI 3.0 使用的 JSON Schema 片段兼容。
如何处理混合类型数组?
若每个元素产生相同 schema,`items` 折叠为该 schema。元素不同则 `items.type` 变为联合(`['string', 'integer']`)。不生成 `anyOf` / `oneOf` — 保持尽可能简单。
自动检测哪些字符串格式?
`date-time`、`date`、`email`、`uri`、`uuid`、`ipv4`。必须整字符串匹配,不是子串。
可空字段呢?
输入中的 null 变为 `"type": "null"`。对于 optional-nullable 字段,在代表性样本上运行生成器再合并 — 工具保持最小 schema,而非猜测。

相关工具