定义从模式到json的键-值对

时间:2023-01-25 18:17:50

I am trying to define an object with key value pairs from a JSON schema and validating it at: Json Schema Validator but I am having no joy as there does not seem to be instructions to do so in all the JSON schema sites I have looked up.

我正在尝试从JSON模式中定义一个具有键值对的对象,并在:JSON模式验证器中验证它,但是我没有什么乐趣,因为在我查询过的所有JSON模式站点中似乎都没有这样做的指令。

My object schema definition is as follows:

我的对象模式定义如下:

                "gum guards" : {
                    "type": "object",

                        "properties": {
                        "Color":      { "type": "string" },
                        "product code": { "type": "string" },
                        "color code": { "type": "string"}
                     },
                    "enum" : ["Color", "product code", "color code"]
                }

The resulting JSON file should give me values such as:

得到的JSON文件应该为我提供如下值:

"gum guards" : [
    { "Color" : "Black", "product code" : "gg-7890", "color code" : "#000000" },
    { "Color" : "White", "product code" : "gg-7891", "color code" : "#ffffff" }
]

However, the validator is giving me the following error message:

但是,验证器给了我以下错误消息:

[ {
  "level" : "error",
  "schema" : {
   "loadingURI" : "#",
   "pointer" : ""
 },
  "instance" : {
   "pointer" : ""
  },
  "domain" : "validation",
  "keyword" : "type",
  "message" : "instance type (object) does not match any allowed primitive type (allowed:          [\"array\"])",
   "found" : "object",
    "expected" : [ "array" ]
    } ]

How do you define an array with key-value/pairs in JSON schema?

如何在JSON模式中定义具有键值/对的数组?

SCHEMA:

模式:

 {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "List of products",
"type": "array",

    "items": {
        "title": "Product",
        "type": "object",
            "properties": {
                "id": {
                    "description": "The unique identifier for a product",
                    "type": "number"
                },
                "Category" : {
                    "type": "string"
                },
                "Product Name" : {
                    "type" : "string"
                },

                "gum guards" : {
                    "type": "array",

                        "items": {
                           "Color": { "type": "string" },
                           "product code": { "type": "string" },
                           "color code": { "type": "string"}
                         },
                    "required" : ["Color", "product code", "color code"]
                },
                "Summary" : {
                "type": "object",
                    "properties": {
                        "Description": {
                            "oneOf": [
                                {"$ref" : "json/product_summary.json#1110/description"},
                                {"$ref" : "json/product_summary.json#1111/description"},
                                {"$ref" : "json/product_summary.json#1112/description"},
                                {"$ref" : "json/product_summary.json#1114/description"},
                            ]
                        }
                    }
                }


            }






    }

OUTPUT:

输出:

 {
"id" : 1110,
"Device Type" : "handset",
"Product Name" : "Pack of accessories",
"variants" : [
    { "Color" : "Black", "product code" : "gg-09090", "color code" : "#000000" },
    { "Color" : "White", "product code" : "gg-09091", "color code" : "#ffffff" }
],
"Summary" : {

    "description" : "Pack of fighter products with chosen colour guard"
}

}

}

1 个解决方案

#1


1  

The issue is here:

这里的问题是:

            "gum guards" : {
                "type": "object",

You've declared that "gum guards" must be an object, like:

你说过“口香糖卫士”一定是一个对象,比如:

     "gum guards": {"Color" : ...},

If you want "gum guards" to be an array, then use "type": "array", and specify the schema for the items using "items":

如果您希望“gum guard”是一个数组,那么使用“type”:“array”,并使用“items”指定项目的模式:

"gum guards": {
    "type": "array",
    "items": {
        "type": "object",
        "properties": {...},
        "required": ["Color", "product code", "color code"]
    }
}

(I've also corrected "enum" to "required", because that looked like a mistake.)

(我还将“enum”改为“required”,因为这看起来像是一个错误。)

#1


1  

The issue is here:

这里的问题是:

            "gum guards" : {
                "type": "object",

You've declared that "gum guards" must be an object, like:

你说过“口香糖卫士”一定是一个对象,比如:

     "gum guards": {"Color" : ...},

If you want "gum guards" to be an array, then use "type": "array", and specify the schema for the items using "items":

如果您希望“gum guard”是一个数组,那么使用“type”:“array”,并使用“items”指定项目的模式:

"gum guards": {
    "type": "array",
    "items": {
        "type": "object",
        "properties": {...},
        "required": ["Color", "product code", "color code"]
    }
}

(I've also corrected "enum" to "required", because that looked like a mistake.)

(我还将“enum”改为“required”,因为这看起来像是一个错误。)