JSON 整理与类型生成可运行案例
账单明细:从样本起草 Draft-07 Schema
从 账单明细 的样本推断字段与类型。样本无法证明全部必填项、合法范围和业务状态,发布前应补正常与异常样本,再用 Schema 校验工具核对。
试一试
在完整工具中打开 ↗示例工作台
JSON 整理与类型生成修改下方数据后重新运行
运行结果
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"ok": {
"type": "boolean"
},
"data": {
"type": "object",
"properties": {
"invoices": {
"type": "array",
"items": {
"type": "object",
"properties": {
"invoiceId": {
"type": "string"
},
"title": {
"type": "string"
},
"amountCents": {
"type": "integer"
},
"settled": {
"type": "boolean"
},
"currency": {
"type": "string"
},
"lines": {
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"cents": {
"type": "integer"
}
},
"required": [
"name",
"cents"
]
}
},
"taxIncluded": {
"type": "boolean"
}
},
"required": [
"items",
"taxIncluded"
]
}
},
"required": [
"invoiceId",
"title",
"amountCents",
"settled",
"currency",
"lines"
]
}
},
"summary": {
"type": "object",
"properties": {
"total": {
"type": "integer"
},
"note": {
"type": "null"
}
},
"required": [
"total",
"note"
]
}
},
"required": [
"invoices",
"summary"
]
}
},
"required": [
"ok",
"data"
]
}原例已核验,可修改输入后重新运行。
⌘ / Ctrl + Enter 运行01
场景与操作说明
账单样本用整数分表示金额,并明确币种。下面的两条账单各有明细数组,适合练习提取账单号、区分金额数字与字符串,以及比较币种或明细变化。
amountCents 是最小货币单位的示例整数;不同币种的最小单位需另行核对。
- 核对 账单明细 的字段层级,保留 invoiceId 的字符串类型。
- 修改一项输入并运行,观察结果中的值或路径如何变化。
- 载入对照样本,复现下面的变化;恢复原例会替换当前输入。
02
对照样本
对照样本没有 invoices 元素,无法推断元素结构。比较两份 Schema,确认哪些约束来自样本证据,哪些仍需人工补充。
查看对照输入
{
"ok": true,
"data": {
"invoices": [],
"summary": {
"total": 2,
"note": null
}
}
}对照样本的实际结果
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"ok": {
"type": "boolean"
},
"data": {
"type": "object",
"properties": {
"invoices": {
"type": "array",
"items": {}
},
"summary": {
"type": "object",
"properties": {
"total": {
"type": "integer"
},
"note": {
"type": "null"
}
},
"required": [
"total",
"note"
]
}
},
"required": [
"invoices",
"summary"
]
}
},
"required": [
"ok",
"data"
]
}