JSON 需要架构数组
JSON schema array required
我正在尝试弄清楚如何设置架构中所需的全局级数组。我的示例 JSON 文件是:
[
{
"firstname": "Paul",
"lastname": "McCartney"
},
{
"firstname": "John",
"lastname": "Lennon"
},
{
"firstname": "George",
"lastname": "Harrison"
},
{
"firstname": "Ringo",
"lastname": "Starr"
}
]
如上所示,我希望顶层结构是一个数组,而不是一个对象。我从 jsonschema.net 得到的模式是(稍作修改):
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "/",
"type": "array",
"items": {
"id": "elements",
"type": "object",
"properties": {
"firstname": {
"id": "firstname",
"type": "string"
},
"lastname": {
"id": "lastname",
"type": "string"
}
},
"required": [
"firstname",
"lastname"
]
},
"required": [
"/"
]
}
但是 jsonschema validator 失败了。你能帮我为顶级数组提供正确的 JSON 架构吗?
要使您的输入数据有效,您只需要遵循以下架构:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"id": "http://jsonschema.net",
"items": {
"type": "object",
"properties": {
"firstname": {
"type": "string"
},
"lastname": {
"type": "string"
}
},
"required": [
"firstname",
"lastname"
]}
}
我正在尝试弄清楚如何设置架构中所需的全局级数组。我的示例 JSON 文件是:
[
{
"firstname": "Paul",
"lastname": "McCartney"
},
{
"firstname": "John",
"lastname": "Lennon"
},
{
"firstname": "George",
"lastname": "Harrison"
},
{
"firstname": "Ringo",
"lastname": "Starr"
}
]
如上所示,我希望顶层结构是一个数组,而不是一个对象。我从 jsonschema.net 得到的模式是(稍作修改):
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "/",
"type": "array",
"items": {
"id": "elements",
"type": "object",
"properties": {
"firstname": {
"id": "firstname",
"type": "string"
},
"lastname": {
"id": "lastname",
"type": "string"
}
},
"required": [
"firstname",
"lastname"
]
},
"required": [
"/"
]
}
但是 jsonschema validator 失败了。你能帮我为顶级数组提供正确的 JSON 架构吗?
要使您的输入数据有效,您只需要遵循以下架构:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"id": "http://jsonschema.net",
"items": {
"type": "object",
"properties": {
"firstname": {
"type": "string"
},
"lastname": {
"type": "string"
}
},
"required": [
"firstname",
"lastname"
]}
}