嵌套 JSON 生成 JSON Schema:递归展开每一层
嵌套结构是 JSON Schema 的强项。工具会递归处理 object 与 array,把每一层都展开为对应的 <code>properties</code> / <code>items</code>。
嵌套样本
{
"id": 1024,
"name": "Ada Lovelace",
"active": true,
"tags": ["math", "computing"],
"profile": { "city": "London", "followers": 1200 },
"scores": [98.5, 87, 91]
}
递归展开结果
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"active": { "type": "boolean" },
"tags": { "type": "array", "items": { "type": "string" } },
"profile": {
"type": "object",
"properties": {
"city": { "type": "string" },
"followers": { "type": "integer" }
},
"required": ["city", "followers"],
"additionalProperties": false
},
"scores": { "type": "array", "items": { "type": "number" } }
},
"required": ["id", "name", "active", "tags", "profile", "scores"],
"additionalProperties": false
}
要点
- 对象 →
properties+required。 - 数组 →
items描述元素结构。 - 同名字段在各元素出现时才进
required。
常见问题
数组元素是对象、且字段不完全一致?
工具按并集合并属性,保证 Schema 能覆盖所有样本。
想让某些字段可选?
在生成的 Schema 里把字段从 required 移除即可。
相关阅读