跳到主内容
UniKit

JSON Schema

从 JSON 示例反推 JSON Schema(draft-07):推断类型、必填字段、date-time / email / uri / ipv4 等 format、同字段的 enum、数组 items 与嵌套对象,可选 additionalProperties: false、$defs 复用与 YAML 输出。

浏览器本地运行所有计算都在你的浏览器里完成,数据不会离开本机。

示例 JSON 与推断选项

输出格式
推断选项
additionalProperties: false只允许示例里出现过的字段
推断 format(date-time / email / uri / ipv4)
把重复结构抽到 $defs

生成的 Schema

输出的是 draft-07;$defs 是 2019-09 起的写法,draft-07 里的同义关键字是 definitions。

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    },
    "name": {
      "type": "string"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "homepage": {
      "type": "string",
      "format": "uri"
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "ipv4": {
      "type": "string",
      "format": "ipv4"
    },
    "active": {
      "type": "boolean"
    },
    "score": {
      "type": "number"
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string",
        "enum": [
          "math",
          "engine"
        ]
      }
    },
    "address": {
      "type": "object",
      "properties": {
        "city": {
          "type": "string"
        },
        "zip": {
          "type": "string"
        }
      },
      "required": [
        "city",
        "zip"
      ]
    },
    "orders": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "sku": {
            "type": "string",
            "enum": [
              "A-1",
              "B-2"
            ]
          },
          "qty": {
            "type": "integer",
            "enum": [
              2,
              1
            ]
          },
          "address": {
            "type": "object",
            "properties": {
              "city": {
                "type": "string",
                "enum": [
                  "London",
                  "Paris"
                ]
              },
              "zip": {
                "type": "string",
                "enum": [
                  "SW1A",
                  "75001"
                ]
              }
            },
            "required": [
              "city",
              "zip"
            ]
          }
        },
        "required": [
          "sku",
          "qty",
          "address"
        ]
      }
    }
  },
  "required": [
    "id",
    "name",
    "email",
    "homepage",
    "createdAt",
    "ipv4",
    "active",
    "score",
    "tags",
    "address",
    "orders"
  ]
}

统计

属性数18
对象4
数组2
enum 数量5
format 数量4
$defs 数量0
最大深度3

这个工具能做什么

  • 接口联调前先定契约:把后端返回的真实 JSON 粘进来,直接得到带 type、required、format 的 draft-07 Schema,比手写快也更不容易漏字段。
  • 给配置文件写校验规则:把示例配置转成 Schema,再用任意 JSON Schema 校验器检查用户填的配置,字段写错、类型不对立刻报错。
  • 对接低代码或表单引擎:同字段取值有限时自动生成 enum,前端下拉框可以直接复用这份取值列表。
  • OpenAPI 里的 components.schemas 不知道怎么写时,先用它把示例对象推成 Schema,再粘进 OpenAPI 文档的 schema 段。

示例

输入

{
  "id": 1024,
  "name": "Ada Lovelace",
  "email": "ada@example.com",
  "createdAt": "2024-05-06T07:08:09Z",
  "tags": ["math", "engine"],
  "address": { "city": "London", "zip": "SW1A" },
  "billing": { "city": "London", "zip": "SW1A" }
}

输出

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    },
    "name": {
      "type": "string"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string",
        "enum": [
          "math",
          "engine"
        ]
      }
    },
    "address": {
      "$ref": "#/$defs/Address"
    },
    "billing": {
      "$ref": "#/$defs/Address"
    }
  },
  "required": [
    "id",
    "name",
    "email",
    "createdAt",
    "tags",
    "address",
    "billing"
  ],
  "$defs": {
    "Address": {
      "type": "object",
      "properties": {
        "city": {
          "type": "string"
        },
        "zip": {
          "type": "string"
        }
      },
      "required": [
        "city",
        "zip"
      ]
    }
  }
}

address 与 billing 结构完全相同,被抽成 $defs.Address 并用 $ref 引用;tags 只有两个取值,所以生成了 enum。

常见问题

为什么所有字段都被标成必填?

单个示例对象里出现的字段都算必填,因为推断只看这一份数据。想让必填判断更准,就把对象放进数组里放多个样本:只有每个样本都出现的字段才会进 required。

enum 是什么时候生成的?

同一个位置有多个样本、且不同取值不超过阈值(默认 6 个,可以调成 0 关闭)时才会生成。单个示例对象没有对照样本,所以不会出现 enum。

format 会不会误判?

只有该位置所有字符串都匹配同一个格式时才会写入 format,例如全部是邮箱才标 email。如果只是长得像(比如版本号 1.2.3 不会命中 date-time),会退回普通 string。识别不了时可以在界面上关掉 format 推断。

$defs 和 definitions 有什么区别?

结构完全相同、出现两次以上的对象会被抽到 $defs,原位置换成 $ref。$defs 是 JSON Schema 2019-09 起的名字,draft-07 里对应的关键字叫 definitions;两者内容一样,按你的校验器支持哪个改一下键名即可。

生成的 Schema 能直接用在 OpenAPI 里吗?

可以,OpenAPI 3.1 直接使用 JSON Schema,把 properties 段粘进 components.schemas 即可;OpenAPI 3.0 需要把 $schema 去掉,并把 nullable 之类的写法单独调整。

关键词:json schemaschema generatordraft-07json 校验json schema 生成类型推断infer schemaapi contract数据校验openapi schema

同类工具