JSON 未命名数组的架构?

JSON schema for an unnamed array?

我需要为直接在根对象中作为数组出现的数据创建一个 JSON 模式,未命名。这种 JSON 的 MWE 是:

{
  [ 
    {
      "veggieName": "potato",
      "veggieLike": true
    },
    {
      "veggieName": "broccoli",
      "veggieLike": false
    }
  ]
}

我看过一些模式示例,这些模式验证未嵌套在对象中的此类数组。我还看到了在命名数组时起作用的示例,例如

{
  vegetables : [ 
    {
      "veggieName": "potato",
      "veggieLike": true
    },
    {
      "veggieName": "broccoli",
      "veggieLike": false
    }
  ]
}

第二个示例可以通过模式验证

{
  "$id": "https://example.com/arrays.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "description": "A representation of a person, company, organization, or place",
  "type": "object",
  "properties": {
    "vegetables": {
      "type": "array",
      "items": { "$ref": "#/definitions/veggie" }
    }
  },
  "definitions": {
    "veggie": {
      "type": "object",
      "required": [ "veggieName", "veggieLike" ],
      "properties": {
        "veggieName": {
          "type": "string",
          "description": "The name of the vegetable."
        },
        "veggieLike": {
          "type": "boolean",
          "description": "Do I like this vegetable?"
        }
      }
    }
  }
}

但问题是,一旦名称 "vegetables" 被删除,我就无法找到定义有效模式的方法。如何在模式中正确表示我的数据结构?

(源自 http://json-schema.org/learn/miscellaneous-examples.html 的 MWE)。

您要查找的架构如下:

{
   "$id":"https://example.com/arrays.schema.json",
   "$schema":"http://json-schema.org/draft-07/schema#",
   "description":"A representation of a person, company, organization, or place",
   "type":"array",
   "items":{
      "type":"object",
      "required":[
         "veggieName",
         "veggieLike"
      ],
      "properties":{
         "veggieName":{
            "type":"string",
            "description":"The name of the vegetable."
         },
         "veggieLike":{
            "type":"boolean",
            "description":"Do I like this vegetable?"
         }
      }
   }
}

您还需要修改您的基本数组实例,您的原始数组实例("unnamed" 数组)无效 JSON:

[
   {
      "veggieName":"potato",
      "veggieLike":true
   },
   {
      "veggieName":"broccoli",
      "veggieLike":false
   }
]

与 XML 不同,在 XML 中,每个文档仅允许一个根节点,在 JSON 中,您可以将类型或数组作为根类型。