[英]How would you design JSON Schema for an arbitrary key?
I have the following JSON output data:我有以下 JSON 输出数据:
{
"label_name_0" : 0,
"label_name_5" : 3,
.
.
.
"label_name_XXX" : 4
}
The output is simple: a key[1] name associated with integer value.输出很简单:一个与整数值关联的键 [1] 名称。 If the key name doesn't change, I can easily come up with JSON Schema similar to this:如果键名没有改变,我可以很容易地想出类似于这样的 JSON Schema:
{
"type": "array"
"title": "Data output",
"items" :{
"properties": {
"label_name": {
"type": "integer",
"default": 0,
"readonly": True,
}
}
},
Since the key name itself is not known and keep changing, I have to design schema for it.由于密钥名称本身是未知的并且不断变化,因此我必须为其设计架构。 The only thing I know is that the key is string
and not more than 100 characters.我唯一知道的是密钥是string
并且不超过 100 个字符。 How do I define a JSON Schema for the key lable_name_xxx
that keeps changing.如何为不断变化的键lable_name_xxx
定义 JSON 模式。
[1] Not sure if I am using the right terminology [1] 不确定我是否使用了正确的术语
On json-schema.org you will find something appropriate in the File System Example section.在 json-schema.org 上,您会在文件系统示例部分找到合适的内容。 You can define patternProperties
inside an object
.您可以在object
内定义patternProperties
。
{
"type": "object",
"properties": {
"/": {}
},
"patternProperties": {
"^(label_name_[0-9]+)+$": { "type": "integer" }
},
"additionalProperties": false,
}
The regular expression (label_name_[0-9]+)+
should fit your needs.正则表达式(label_name_[0-9]+)+
应该适合您的需要。 In JSON Schema regular expressions are explicitly anchored with ^
and $
.在 JSON Schema 中,正则表达式明确地用^
和$
锚定。 The regular expressions defines, that there has to be at least one property ( +
).正则表达式定义,必须至少有一个属性 ( +
)。 The property consists of label_name_
and a number between 0
and 9
whereas there has to be at least one number ([0-9]+)
, but there can also arbitrary many of them.该属性由label_name_
和一个0
到9
之间的数字组成,而必须至少有一个数字([0-9]+)
,但也可以有任意多个。
By setting additionalProperties
to false
it constrains object properties to match the regular expression.通过将additionalProperties
设置为false
它会约束对象属性以匹配正则表达式。
As Konrad's answer stated, use patternProperties
.正如康拉德的回答所说,使用patternProperties
。 But use in place of properties
, which is not needed, and I think Konrad just pasted from his reference example that was expecting a path starting with /
.但是使用代替properties
,这是不需要的,我认为 Konrad 刚刚从他的参考示例中粘贴了期望以/
开头的路径。 In the example below, the pattern match regex .*
accepts any property name and I am allowing types of string or null only by using "additionalProperties": false
.在下面的示例中,模式匹配regex .*
接受任何属性名称,并且我仅通过使用"additionalProperties": false
来允许字符串或 null 类型。
"patternProperties": {
"^.*$": {
"anyOf": [
{"type": "string"},
{"type": "null"}
]
}
},
"additionalProperties": false
Simpler solution than patternProperties, since OP does not have any requirement on the key names ( documentation ):比 patternProperties 更简单的解决方案,因为 OP 对键名( 文档)没有任何要求:
{
"type": "object",
"additionalProperties": {
"type": "integer",
"default": 0,
"readonly": true,
}
}
default
and readonly
included because they were included in the OP's initial suggestion, but they are not required. default
和readonly
包含,因为它们包含在 OP 的初始建议中,但它们不是必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.