如何测试和检查JSON文件并报告错误

发布于 2025-02-09 08:59:42 字数 609 浏览 1 评论 0原文

我当前正在使用JSonschema python检查我的JSON文件。我创建了一个schema.json检查已循环的JSON文件,并且根据我在schema.json 。 目前,我无法在正在检查的JSON文件中显示和突出显示错误,例如,

    "data_type": {
      "type": "string",
      "not": { "enum": ["null", "0", "NULL", ""] }
    },

如果JSON文件具有值“ data_type”:“ nenull”或Eneum中类似的内容,它将显示json文件无效,但我希望它突出显示错误并显示您在data_type字段中添加了null我可以使用jsonschema做到吗?

如果没有使用jsonschema的另一种方式,则可以使用;主要测试是在文件夹中检查多个JSON文件(可以循环循环),并根据几组规则检查它们是否有效,并显示一个干净且不错的报告,该报告专门告诉我们问题。

I am currently using jsonschema with python to check my json files. I have created a schema.json that checks the json files that are looped over, and I display the file is invalid or valid according to the checks i have added in my schema.json.
Currently i am not able to display and highlight the error in the json file which is being checked, for example

    "data_type": {
      "type": "string",
      "not": { "enum": ["null", "0", "NULL", ""] }
    },

if the json file has the value "data_type" : "null" or anything similar in the enum, it will display json file is invalid but i want it to highlight the error and display you added null in the data_type field can i do it with jsonschema?

If there is another way without using jsonschema that will work; the main testing is to check multiple json files in a folder (which can be looped over) and check whether they are valid according to few set of rules and display a clean and nice report which specifically tells us the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城孤痞 2025-02-16 08:59:42

我所做的是从jsonschema中使用的draft7validator,它允许显示错误消息

validator = jsonschema.Draft7Validator(schema)
errors = sorted(validator.iter_errors(jsonData[a]), key=lambda e: e.path)
            for error in errors:
                print(f"{error.message}")
                report.write(f"In {file_name}, object {a}, {error.message}{N}")
                error_report.write(f"In {file_name}, object {a},{error.json_path[2:]} {error.message}{N}")

此打印 -

在test1.json中,对象0,apy''不是类型'number'

在test2.json中

有用的链接,它帮助我实现了this- 处理验证错误

What I did is used Draft7Validator from jsonschema and it allows to display error message

validator = jsonschema.Draft7Validator(schema)
errors = sorted(validator.iter_errors(jsonData[a]), key=lambda e: e.path)
            for error in errors:
                print(f"{error.message}")
                report.write(f"In {file_name}, object {a}, {error.message}{N}")
                error_report.write(f"In {file_name}, object {a},{error.json_path[2:]} {error.message}{N}")

This prints -

In test1.json, object 0, apy '' is not of type 'number'

In test2.json, object 0, asset 'asset_contract_address' is a required property

Helpful link that helped me achieve this- Handling Validation Errors

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文