Tool Kumar 工具
JSON Schema 生成器
用一份有代表性的 JSON 文档创建 JSON Schema 草稿。结果描述示例中的结构,可以作为校验的起点。
如何使用这个工具
- 粘贴一份示例文档。
- 在“转换”视图中选择 JSON Schema。
- 检查推断出的属性,然后保存 schema。
示例演示
这个 schema 根据示例对象生成,包括嵌套的 profile 和 tags。这个对象包含了每一个属性,所以每个属性都是必填。
输入
{"name":"Ada","age":36,"active":true,"tags":["json","tools"],"profile":{"city":"London"}}输出
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Root",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
},
"active": {
"type": "boolean"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"profile": {
"$ref": "#/$defs/Profile"
}
},
"required": ["name", "age", "active", "tags", "profile"],
"$defs": {
"Profile": {
"type": "object",
"properties": {
"city": {
"type": "string"
}
},
"required": ["city"]
}
}
}
required 属性是如何确定的
根据多条记录生成 schema 时,required 列表只保留每条记录都有的键。在这个示例中,两个用户都有 id 和 email,但只有第一个用户有 phone,所以 phone 会被描述,但不会列入 required。
Input:
[{"id":1,"email":"ada@example.com","phone":"+44 20 7946 0000"},{"id":2,"email":"lin@example.com"}]
Generated schema:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Root",
"type": "array",
"items": {
"$ref": "#/$defs/RootItem"
},
"$defs": {
"RootItem": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"email": {
"type": "string",
"format": "email"
},
"phone": {
"type": "string"
}
},
"required": ["id", "email"]
}
}
}选项与特殊情况
- 嵌套的对象和数组会被递归描述。
- 如果某个位置上所有被采样的对象都有某个属性,它就会列入 required。因此,一份完整的示例会让每个出现过的属性都成为必填;在对象数组中,只有部分对象具有的键则是可选的。
- 在部分对象中为 null 的值,不会让属性变成可选,而是把 null 加入该属性的类型。
- 输出只是一个起点。请检查 required,补充 format、取值范围和 pattern,并删除你的 API 契约没有承诺的内容。
- 本页用于生成 schema,不会根据 schema 校验文档。生成之前,可以先在“JSON 校验工具”页面检查 JSON 语法。
默认保护隐私
你的文档只保留在这个浏览器中。Tool Kumar 在你的设备上解析、转换并生成下载文件,无需账号,也没有上传步骤。
JSON Schema 生成器:常见问题
- 生成器会把每个属性都标为必填吗?
- 只有每个被采样的对象都具有的属性才会标为必填。单独一份完整的示例会把它的所有属性都标为必填,而数组中只有部分对象具有的键则是可选的。发布 schema 之前,请对照你的 API 契约检查 required 列表。
- 使用哪个草案版本?
- 输出声明为 JSON Schema Draft 2020-12,并使用标准的 type 和 properties 关键字。
- 能推断数组元素的结构吗?
- 能。对象数组会根据示例中的对象结构生成 items schema。