小编Cla*_*one的帖子

如何通过 Python 使用 jsonschema 验证字典列表

我有一个这样的字典列表:

list_of_dictionaries = [{'key1': True}, {'key2': 0.2}]
Run Code Online (Sandbox Code Playgroud)

我想使用 jsonschema 包来验证它。

我创建了这样的架构:

schema = {
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "key1": {
                "type": "boolean"
            },
            "key2": {
                "type": "number"
            }
        },
        "required": ["enabled"]
    }
}
Run Code Online (Sandbox Code Playgroud)

但这对于我的列表来说是不正确的,因为要使其正常工作,我的列表应该是这样的:

list_dict = [{'key1': True, 'key2': 0.5}]
Run Code Online (Sandbox Code Playgroud)

如何创建正确的架构来验证我的列表?先感谢您。

python jsonschema python-jsonschema

2
推荐指数
1
解决办法
942
查看次数

标签 统计

jsonschema ×1

python ×1

python-jsonschema ×1