json - 具有不同属性的嵌套对象的 JSON 模式
问题描述
整个 JSON 文件相当大,所以我只取出了我遇到问题的小节。
{
"diagrams": {
"5f759d15cd046720c28531dd": {
"_id": "5f759d15cd046720c28531dd",
"offsetX": 320,
"offsetY": 42,
"zoom": 80,
"modified": 1604279356,
"nodes": {
"5f9f5c3ccd046720c28531e4": {
"nodeID": "5f9f5c3ccd046720c28531e4",
"type": "start",
"coords": [
360,
120
],
"data": {
"name": "Start",
"color": "standard",
"ports": [
{
"type": "",
"target": "5f9f5c3ccd046720c28531e6"
}
],
"steps": []
}
},
"5f9f5c3ccd046720c28531e5": {
"nodeID": "5f9f5c3ccd046720c28531e5",
"type": "block",
"coords": [
760,
120
],
"data": {
"name": "Help Message",
"color": "standard",
"steps": [
"5f9f5c3ccd046720c28531e6",
"5f9f5c3ccd046720c28531e7"
]
}
},
"5f9f5c3ccd046720c28531e6": {
"nodeID": "5f9f5c3ccd046720c28531e6",
"type": "speak",
"data": {
"randomize": false,
"dialogs": [
{
"voice": "Alexa",
"content": "You said help. Do you want to continue?"
}
],
"ports": [
{
"type": "",
"target": "5f9f5c3ccd046720c28531e7"
}
]
}
},
"5f9f5c3ccd046720c28531e7": {
"nodeID": "5f9f5c3ccd046720c28531e7",
"type": "interaction",
"data": {
"name": "Choice",
"else": {
"type": "path",
"randomize": false,
"reprompts": []
},
"choices": [
{
"intent": "",
"mappings": []
},
{
"intent": "",
"mappings": []
}
],
"reprompt": null,
"ports": [
{
"type": "else",
"target": null
},
{
"type": "",
"target": null
},
{
"type": "",
"target": "5f9f5c3ccd046720c28531e9"
}
]
}
},
"5f9f5c3ccd046720c28531e8": {
"nodeID": "5f9f5c3ccd046720c28531e8",
"type": "block",
"coords": [
1170,
260
],
"data": {
"name": "Exit",
"color": "standard",
"steps": [
"5f9f5c3ccd046720c28531e9"
]
}
},
"5f9f5c3ccd046720c28531e9": {
"nodeID": "5f9f5c3ccd046720c28531e9",
"type": "exit",
"data": {
"ports": []
}
}
},
"children": [],
"creatorID": 42661,
"variables": [],
"name": "Help Flow",
"versionID": "5f759d15cd046720c28531db"
}
}
}
我拥有的当前 JSON 模式定义是:
{
"$schema":"http://json-schema.org/schema#",
"type":"object",
"properties":{
"diagrams":{
"type":"object"
}
},
"required":[
"diagrams",
]
}
我遇到的问题是图表中包含多个对象,其名称为随机字符串,例如“5f759d15cd046720c28531dd”。
然后在该对象内有我想要表达的属性,例如 (_id, offsetX) 以及一个节点对象,它再次包含多个具有任意名称的对象,例如 ("5f9f5c3ccd046720c28531e4", "5f9f5c3ccd046720c28531e5", ...)一个独特的节点定义,其中一些节点与其他节点具有不同的属性(nodeID、type、data vs nodeID、type、data、coords)。
我的问题是所有这些任意的东西,例如随机名称以及每个节点的不同属性。如何将其转换为 1 个 JSON 模式定义,该定义涵盖了如何制作图表/节点的所有情况。
解决方案
您可以使用additionalProperties
或来执行此操作patternProperties
。
additionalProperties
properties
适用于未在or中声明的任何属性patternProperties
。
{
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"_id": { ... },
"offsetX": { ... },
...
}
}
}
您的属性名称似乎始终是十六进制数字。如果您想强制这些属性名称始终是十六进制数字,您可以使用patternProperties
. 任何与正则表达式匹配的属性都必须符合该模式。
{
"type": "object",
"patternProperties": {
"^[0-9a-f]{24}$": {
"type": "object",
"properties": {
"_id": { ... },
"offsetX": { ... },
...
}
}
},
"additionalProperties": false
}
推荐阅读
- python-3.x - 我怎么知道哪个异步任务没有完成?
- pandas - Azure Application Insights 自定义用户流
- database - 在更新查询中使用组合框
- magento - Magento 2 自定义下拉属性添加选项未显示
- wso2 - 自从迁移到 WSO2IS 后,Moodle 插件“Respondus”将无法进行身份验证
- python - 让尝试更快地捕捉 Python
- java - SimpleJdbcCall 适用于 windows 但不适用于 linux
- javascript - 在 laravel 中创建一个固定的支持按钮
- java - Keycloak:使查询字符串参数在标记 FreeMarker 模板中可用
- oauth-2.0 - OAuth 2 将服务器-服务器 API 调用的身份验证与授权分开