Json 以键作为数据的架构
Json schema with keys as data
我很难处理 json 架构。
假设这是初始简单 json。
[
{
"Field1": 1,
"Description": "Default"
},
{
"Field1": 77,
"Description": "NonDefault"
}
]
这是有效的编写模式
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"Field1": {
"type": "integer"
},
"Description": {
"type": "string"
}
}
}
}
我想更改 Json,以使用“Field1”作为键。
“Field1”是整数值。
我不知道值以及 json.
中会有多少 Field1
这是最终的JSON
{
"1": {
"Description": "Default"
},
"77": {
"Description": "NonDefault"
}
}
但是如何为此 JSON 编写 json-schema?
您可以将“propertyNames”与“pattern”元素结合使用来创建一个正则表达式来限定您的键名,而不是将它们全部枚举。我个人更喜欢你的第一个布局。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"patternProperties": {
"^[0-9]*$": { "type": "string" }
}
}
TimRoberts 的答案就快出来了。您想要的是描述 patternProperties
子模式中的项目,类似于示例模式中的 items
。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"patternProperties": {
"^[0-9]*$": {
"type": "object",
"properties": {
"Description": { "type": "string" }
}
}
}
}
我很难处理 json 架构。
假设这是初始简单 json。
[
{
"Field1": 1,
"Description": "Default"
},
{
"Field1": 77,
"Description": "NonDefault"
}
]
这是有效的编写模式
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"Field1": {
"type": "integer"
},
"Description": {
"type": "string"
}
}
}
}
我想更改 Json,以使用“Field1”作为键。 “Field1”是整数值。 我不知道值以及 json.
中会有多少 Field1这是最终的JSON
{
"1": {
"Description": "Default"
},
"77": {
"Description": "NonDefault"
}
}
但是如何为此 JSON 编写 json-schema?
您可以将“propertyNames”与“pattern”元素结合使用来创建一个正则表达式来限定您的键名,而不是将它们全部枚举。我个人更喜欢你的第一个布局。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"patternProperties": {
"^[0-9]*$": { "type": "string" }
}
}
TimRoberts 的答案就快出来了。您想要的是描述 patternProperties
子模式中的项目,类似于示例模式中的 items
。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"patternProperties": {
"^[0-9]*$": {
"type": "object",
"properties": {
"Description": { "type": "string" }
}
}
}
}