gpt4 book ai didi

具有未知属性名称的 JSON Schema

转载 作者:IT老高 更新时间:2023-10-28 12:52:52 26 4
gpt4 key购买 nike

我想在对象数组中拥有一个属性名称未知的 JSON 模式。一个很好的例子是网页的元数据:

      "meta": {
"type": "array",
"items": {
"type": "object",
"properties": {
"unknown-attribute-1": {
"type": "string"
},
"unknown-attribute-2": {
"type": "string"
},
...
}
}
}

请问有什么想法,或者其他方法可以达到同样的效果吗?

最佳答案

使用 patternProperties而不是 properties .在下面的示例中,模式匹配正则表达式 .*接受任何属性名称,我允许 string 的类型或 null只能使用 "additionalProperties": false .

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

...或者如果您只想在“对象”中允许一个字符串(就像在原始问题中一样):

  "patternProperties": {
"^.*$": {
{"type": "string"},
}
},
"additionalProperties": false

关于具有未知属性名称的 JSON Schema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32044761/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com