{"57ecf565817bc3932d8de349": {
"prices": {
"2017-07-01": {
"summer": -1,
"winter": -1,
"xmas": -1},
"2017-08-05": {
"summer": -1,
"winter": -1,
"xmas": -1}
}
}
}
如何编写每个密钥不同的JSON模式(此处:2017-07-01,2017-08-05),因此我无法使用items
,例如{{3 (产品架构集)。
这是我的架构:
{
"type": "object",
"properties": {
"57ecf565817bc3932d8de349": {
"type": "object",
"properties": {
"prices": {
"type": "object",
"properties": {
"2017-07-01": {
"type": "object",
"properties": {
"summer": {
"type": "integer"
},
"winter": {
"type": "integer"
},
"xmas": {
"type": "integer"
}
},
"required": [
"summer",
"winter",
"xmas"
]
},
"2017-08-05": {
"type": "object",
"properties": {
"summer": {
"type": "integer"
},
"winter": {
"type": "integer"
},
"xmas": {
"type": "integer"
}
},
"required": [
"summer",
"winter",
"xmas"
]
}
},
"required": [
"2017-07-01",
"2017-08-05"
]
}
},
"required": [
"prices"
]
}
},
"required": [
"57ecf565817bc3932d8de349"
]
}
在我原来的JSON中,我有很多这样的日期:2017-07-01,我想避免重复每个日期的架构。
答案 0 :(得分:1)
您可以使用additionalProperties
关键字执行此操作。在此示例中,对象中的每个属性都必须针对给定的架构进行验证。
{
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"summer": { "type": "integer" },
"winter": { "type": "integer" },
"xmas": { "type": "integer" }
},
"required": ["summer", "winter", "xmas"]
}
}
答案 1 :(得分:0)
架构定义静态数据结构。
因此,您无法为动态数据结构编写架构。
这不仅适用于JSON,而且适用于XSD / SQL / protobuf等。模式是一个严格的物理蓝图,用于显示某些内容。
您最好为数据定义静态类型,并执行以下操作:
{
"type":"object",
"properties":{
"resource":{
"type":"object",
"properties":{
"hash":{
"type":"string"
},
"prices":{
"type":"array",
"items":{
"$ref":"#/definitions/price"
},
"minItems":1
}
},
"required":[
"hash",
"prices"
]
}
},
"definitions":{
"price":{
"type":"object",
"properties":{
"date":{
"type":"string"
},
"summer":{
"type":"integer"
},
"winter":{
"type":"integer"
},
"xmas":{
"type":"integer"
}
},
"required":[
"date",
"summer",
"winter",
"xmas"
]
}
}
}
这将允许您拥有您的实例:
{
"resource":{
"hash":"57ecf565817bc3932d8de349",
"prices":[
{
"date":"2017-07-01",
"summer":-1,
"winter":-1,
"xmas":-1
},
{
"date":"2017-08-05",
"summer":-1,
"winter":-1,
"xmas":-1
}
]
}
}
答案 2 :(得分:0)
使用patternProperties
https://json-schema.org/understanding-json-schema/reference/object.html#pattern-properties
类似
{
"type": "object",
"patternProperties": {
"^\d{4}-\d{2}-\d{2}": {
"type": "object",
"properties": {
"summer": {
"type": "integer"
},
"winter": {
"type": "integer"
etc...