如何为任意键设计JSON模式?

时间:2021-05-04 12:56:11

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. If the key name doesn't change, I can easily come up with JSON Schema similar to this:

输出很简单:与整数值关联的关键[1]名称。如果键名不变,我可以很容易地得到类似以下的JSON模式:

    {
        "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. How do I define a JSON Schema for the key lable_name_xxx that keeps changing.

由于键名本身不为人知,并且不断变化,所以我必须为它设计模式。我只知道键是字符串,不超过100个字符。如何为不断变化的关键lable_name_xxx定义一个JSON模式。

[1] Not sure if I am using the right terminology

[1]不确定我是否使用了正确的术语

2 个解决方案

#1


23  

On json-schema.org you will find something appropriate in the Advanced Examples section. You can define patternProperties inside an object.

在json-schema.org上,您将在高级示例部分找到一些合适的内容。您可以在对象内部定义模式属性。

{
    "type": "object",
    "properties": {
        "/": {}
    },
    "patternProperties": {
        "^(label_name_[0-9]+)+$": { "type": "integer" }
    },
    "additionalProperties": false,
 }

The regular expression (label_name_[0-9]+)+ should fit your needs. In JSON Schema regular expressions are explicitly anchored with ^ and $. 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]+)应该适合您的需要。在JSON模式正则表达式是明确固定^和$。正则表达式定义,必须至少有一个属性(+)。属性由label_name_和一个介于0和9之间的数字组成,而必须至少有一个数字([0-9]+),但是也可以有很多。

By setting additionalProperties to false it constrains object properties to match the regular expression.

通过将additionalProperties设置为false,它限制了对象属性与正则表达式匹配。

#2


9  

As Konrad's answer stated, use 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 /. 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.

正如Konrad的回答所说,使用模式属性。但是,使用不需要的属性,我认为Konrad只是从他的引用示例中粘贴过来的,它期望从/开始的路径。在下面的示例中,模式匹配regex .*接受任何属性名,我只允许使用“additionalProperties”:false来表示字符串类型或null类型。

  "patternProperties": {
    "^.*$": {
      "anyOf": [
        {"type": "string"},
        {"type": "null"}
      ]
    }
  },
  "additionalProperties": false

#1


23  

On json-schema.org you will find something appropriate in the Advanced Examples section. You can define patternProperties inside an object.

在json-schema.org上,您将在高级示例部分找到一些合适的内容。您可以在对象内部定义模式属性。

{
    "type": "object",
    "properties": {
        "/": {}
    },
    "patternProperties": {
        "^(label_name_[0-9]+)+$": { "type": "integer" }
    },
    "additionalProperties": false,
 }

The regular expression (label_name_[0-9]+)+ should fit your needs. In JSON Schema regular expressions are explicitly anchored with ^ and $. 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]+)应该适合您的需要。在JSON模式正则表达式是明确固定^和$。正则表达式定义,必须至少有一个属性(+)。属性由label_name_和一个介于0和9之间的数字组成,而必须至少有一个数字([0-9]+),但是也可以有很多。

By setting additionalProperties to false it constrains object properties to match the regular expression.

通过将additionalProperties设置为false,它限制了对象属性与正则表达式匹配。

#2


9  

As Konrad's answer stated, use 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 /. 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.

正如Konrad的回答所说,使用模式属性。但是,使用不需要的属性,我认为Konrad只是从他的引用示例中粘贴过来的,它期望从/开始的路径。在下面的示例中,模式匹配regex .*接受任何属性名,我只允许使用“additionalProperties”:false来表示字符串类型或null类型。

  "patternProperties": {
    "^.*$": {
      "anyOf": [
        {"type": "string"},
        {"type": "null"}
      ]
    }
  },
  "additionalProperties": false