我正在尝试使用JSON模式验证JSON。在下面的json中,“ industry”的类型为“ object”,并且“ not required”。但是我需要找出json中是否提供了“行业”。
这是我的json模式
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"enum": ["Russia", "Canada"]
},
"name": {
"type": "string"
},
"industry": {
"$ref": "#/definitions/industry"
}
},
"required": [
"id",
"name"
],
"definitions": {
"industry": {
"type": "object",
"additionalProperties": false,
"properties": {
"type": {
"type": "string"
},
"codes": {
"type": "array",
"items": {
"type": "integer"
}
}
},
"required": [
"codes",
"type"
],
"title": "industry"
}
}
}
这里是我的json
{
"id": "Russia",
"price": 10.50
}
我想知道如果json中存在“工业”对象,则给定的json bcos中是否存在“工业”对象。我需要做点其他的事情。目前,如果我像上面那样发送json并尝试使用以下if语句,例如下面的代码。即使json中不存在“ industry”对象,它也会作为true传递。我相信它正在考虑像这样的{}而不是null的“工业”对象。
"if":{
"properties": {"industry" : { "type": "object" }}
},
任何用于验证json对象中是否存在“ industry”对象的解决方案都是有帮助的。谢谢。
如果属性不存在,则包含“属性”的架构将评估为true。您希望“ if”作为条件的内容是“ required”:
"if": {"required":["industry"]}, "then": { ... }