【发布时间】:2022-01-21 16:46:36
【问题描述】:
我想验证我的 json 输入 -- my_json。我预计会出现异常(因为 job1 不是工作。
如何验证这个 json?
import json
from jsonschema import validate
# Describe what kind of json you expect.
schema = {
"job" : {"type" : "string"},
"big_list": [
{
"id": 1,
"code": "qqq"
},
{
"id": 2,
"code": ""
}
# many items
]
}
# Convert json to python object.
my_json = {'job1': "as", 'big_list': [{'id': 1, 'code': 'qqq'}, {'id': 2, 'code': ''}]}
validate(instance=my_json, schema=schema) # I expected exception, but have no exceptions
【问题讨论】:
标签: python json jsonschema