根据JSON模式设置默认值
我正在使用 jsonschema
python
中的软件包来验证我的jsons。我可以在 jsonschema
中设置默认
。让我们假设以下架构:
from jsonschema import validate
schema = {
"type": "object",
"properties": {
"key_1": {},
"key_2": {
"type": "string",
"default": "do_not_overwrite_if_key_exists",
},
"key_3": {
"type": "string",
"default": "use_it_if_key_does_not_exist",
},
},
"required": ["key_1"],
}
json_dict = {"key_1": "key_1_value", "key_2": "key_2_value"}
validate(json_dict, schema)
我的 JSON_DICT
是完全有效的。我有两个问题:
-
默认
关键字真的如此“无用”(请参见下文)?这就是你得到的吗?
“默认”关键字没有效果,但是对于架构的读者来说,可以更轻松地识别默认行为。
。
- 我想拥有一种将我的
json_dict
转换为以下方法,根据默认
关键字。你知道这样的包裹吗?
json_dict = {"key_1": "key_1_value", "key_2": "key_2_value", "key_3": "use_it_if_key_does_not_exist"}
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
jsonschema_default
链接下面链接的软件包,以从JSON架构中生成默认的JSON对象。https://pypi.org/project/jsonschema-default/
JSON_DICT 中的丢失密钥
”默认
You can use the
jsonschema_default
package linked below to generate a default JSON object from your JSON schema.https://pypi.org/project/jsonschema-default/
Then you can use the default JSON object to fill in the missing key in your
json_dict
Like so:
默认
关键字在此处记录: https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.9.2.2确实不是验证关键字作为注释。当通过模式评估生成时,决定如何使用此注释,这取决于单个应用程序。
The
default
keyword is documented here: https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.9.2It is indeed not a validation keyword, and only acts as an annotation. It is up to individual applications to decide how to use this annotation when it is generated from schema evaluation.
@AJack的JSonschema_default解决方案无法与部分填充的嵌套对象一起使用:键将存在,因此不会添加默认值。
假设我有此默认值:
并提供此JSON:
然后仅将
“其他”
添加使用默认值。“ bar_2”
不会添加,因为“ foo”
存在。带有递归的一种解决方案是搜索所有默认的嵌套结构以填充。这是我的解决方案的尝试:
The jsonschema_default solution from @ajack wouldn't work with partially filled nested objects: The key would exist thus missing default values would not be added.
Say I have this default:
And provide this json:
Then only
"other"
would be added with the default value."bar_2"
wouldn't be added because"foo"
exists.One solution with recursion is to search all nested structures with defaults to fill. This is my attempt of a solution:
您可以使用
fill_default
来自/code>
用其模式填充现有实例中的所有缺失默认值:
我创建了
jsonschema-fill-default
,tom-tbt的答案是fill_default
的灵感来源。填充是针对所有缺失和嵌套默认值的递归完成的。它与
“ properties”的所有组合一起使用,
,“ allof”
,“ anyof”
,“ oneof” ,“ depententsChemas”
,“ if-then(-else)”
,“ prefixItems”
和“关键字。
注意:
参见示例在项目的github上,有关详细信息。
You can use
fill_default
fromjsonschema-fill-default
to fill all missing defaults in an existing instance with its schema:I created
jsonschema-fill-default
with Tom-tbt's answer as inspiration forfill_default
.The filling is done recursively for all missing and nested defaults. It works with all combinations of
"properties"
,"allOf"
,"anyOf"
,"oneOf"
,"dependentSchemas"
,"if-then(-else)"
,"prefixItems"
, and"items"
keywords.Note:
See Examples on the project's GitHub for details.