我有一个json文件并创建了一个jsonSchema。现在我进行了验证。它工作正常。在错误消息中,它显示如下错误:
['id', 4180, 'name', 'amount']
2.0 is greater than the maximum of 1
我也要打印发生错误的ID号,以便可以轻松地在JSON文件中跟踪错误。
我尝试了以下代码:
jsondata = json.loads(jsonInput.read())
jsonschema = json.loads(schemaInput.read())
validator = jsonschema.Draft7Validator(jsonschema )
errors = sorted(validator.iter_errors(jsondata ),key=str) # get all validation errors
error_count=1;
for error in errors:
print(error)
我该怎么做?
您需要进一步检查得到的错误。
这里是the documentation的示例:
for error in errors:
for suberror in sorted(error.context, key=lambda e: e.schema_path):
print(list(suberror.schema_path), suberror.message, sep=", ")