一个用于在 Python Web 服务中检查传入 JSON 数据的小工具

192 阅读1分钟

维护之前的代码时发现,Python 中没有类型检查 json 数据很容易传错,所以写了一个检查 JSON 的东西...不知道是不是重复造轮子...

  • 检查 JSON key 数目
  • 检查 JSON key 名字
  • 检查 JSON 的 value 的 type,若为 dict, 则递归地检查它.

jsoncensor_github

求 star ฅʕ•̫͡•ʔฅ

pip install jsoncensor
from jsoncensor import JsonCensor
standard = {"test":"test"}
suspect = {"tes":"test"}
jc = JsonCensor(standard, suspect)
ret = jc.check()
if ret['statu'] != True:
	print(ret)

# 输出
# {'statu': False, 'error': 'KeyNameError', 'error_item': 'tes', 'should_be': 'test'}