使用 Python 的 jsonschema 对 json 做验证
在OpenStack中, 使用了Python的 jsonschema包, 对json字符串做了验证.
Python JSON Schema Library
https://pypi.python.org/pypi/jsonschema
JSON Schema Introduction
http://json-schema.org/
做法比较简单
1) 定义一个文件 json schema. json schema 类似于一个模板定义文件, 定义了json中的节点名称, 节点值类型
以tempest中的一个schema定义为例 (tempest/api_schema/compute/agents.py)
- list_agents = {
- 'status_code': [200],
- 'response_body': {
- 'type': 'object',
- 'properties': {
- 'agents': {
- 'type': 'array',
- 'items': {
- 'type': 'object',
- 'properties': {
- 'agent_id': {'type': 'integer'},
- 'hypervisor': {'type': 'string'},
- 'os': {'type': 'string'},
- 'architecture': {'type': 'string'},
- 'version': {'type': 'string'},
- 'url': {'type': 'string', 'format': 'uri'},
- 'md5hash': {'type': 'string'}
- },
- 'required': ['agent_id', 'hypervisor', 'os',
- 'architecture', 'version', 'url', 'md5hash']
- }
- }
- },
- 'required': ['agents']
- }
- }
2) 使用jsonschema包, 对json字符串和json schema做对比, 进行验证
以下代码来自于 /tempest/common/rest_client.py.
tempest对每一个REST api的返回值, 都使用json schema做了校验
- @classmethod
- def validate_response(cls, schema, resp, body):
- # Only check the response if the status code is a success code
- # TODO(cyeoh): Eventually we should be able to verify that a failure
- # code if it exists is something that we expect. This is explicitly
- # declared in the V3 API and so we should be able to export this in
- # the response schema. For now we'll ignore it.
- if resp.status in HTTP_SUCCESS:
- cls.expected_success(schema['status_code'], resp.status)
- # Check the body of a response
- body_schema = schema.get('response_body')
- if body_schema:
- try:
- jsonschema.validate(body, body_schema)
- except jsonschema.ValidationError as ex:
- msg = ("HTTP response body is invalid (%s)") % ex
- raise exceptions.InvalidHTTPResponseBody(msg)
- else:
- if body:
- msg = ("HTTP response body should not exist (%s)") % body
- raise exceptions.InvalidHTTPResponseBody(msg)
- # Check the header of a response
- header_schema = schema.get('response_header')
- if header_schema:
- try:
- jsonschema.validate(resp, header_schema)
- except jsonschema.ValidationError as ex:
- msg = ("HTTP response header is invalid (%s)") % ex
- raise exceptions.InvalidHTTPResponseHeader(msg)
Java 中, 也有一个json-schema-validator的实现, 用法可以参考
http://stackoverflow.com/questions/14511468/java-android-validate-string-json-against-string-schema
使用 Python 的 jsonschema 对 json 做验证相关推荐
- python软件是干什么用的-python中的django是做什么的
Django是什么? Django是一个基于Python的Web应用框架.它与Python的另外一个Web 框架 Flask最大的区别是,它奉行 "包含一切" 的哲学.该理念即为: ...
- Python:数据提取之JSON与JsonPATH
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它使得人们很容易的进行阅读和编写.同时也方便了机器进行解析和生成.适用于进行数据交互的场景,比如网站前台与 ...
- 一篇长文带你在python里玩转Json数据
Json简介 Json(JavaScript Object Notation) 很多网站都会用到Json格式来进行数据的传输和交换. 这因为Json是一种轻量级的数据交换格式,具有数据格式简单,读写方 ...
- 使用阿里巴巴json映射_使用JSON模式验证来映射稀疏JSON
使用阿里巴巴json映射 在本文中,我们将探讨在创建和共享API时出现的问题. 特别是,需要: 表达数据的结构 提供对该数据的验证 允许将来改变主意 通过您拥有的部分数据与客户进行沟通 缺少数据时填写 ...
- 使用JSON模式验证来映射稀疏JSON
在本文中,我们将介绍创建和共享API时出现的问题. 特别是,需要: 表达数据的结构 提供对该数据的验证 允许将来改变主意 通过您拥有的部分数据与客户进行沟通 缺少数据时填写空白 人们通常使用版本编号的 ...
- python做地图导航_「Python」利用高德地图做你想做之事
玩grasshopper基本上都知道OpenStreetMap 这个地图网站,毕竟有一个好用的地图插件,可以在Rhino中绘制出所需,但是一个不好的地方就在于国内的数据量太少,无法满足我们的需求. 此 ...
- hutol json null值没了_一篇长文带你在python里玩转Json数据
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 朱小五/凹凸玩数据 PS:如有需要Python学习资料的小伙伴可以加 ...
- C# json解析字符串总是多出双引号_一篇长文带你在python里玩转Json数据
Json简介 Json(JavaScript Object Notation) 很多网站都会用到Json格式来进行数据的传输和交换,就像上篇我提到的网易云音乐接口,它们返回的数据都是Json格式的. ...
- python做地图导航_【Python】利用高德地图做你想做之事
原标题:[Python]利用高德地图做你想做之事 [版权声明] 作者:CPF 如需转载请联系CPF 如有侵权请联系大水牛平台 玩grasshopper基本上都知道OpenStreetMap 这个地图网 ...
最新文章
- Oracle存储过程和存储函数创建
- 这次来个BGP反射器的简单小实验
- PythonOpencv-分类器—SVM,KNearest,RTrees,Boost,MLP
- unity3d 任务头上的血条
- 【牛客 - 1080B】tokitsukaze and Hash Table(STLset,并查集,Hash)
- 小哥哥你有98K吗?利用Python制作一款多功能变声器!
- c语言实现判断两个子图是否同构问题_经典问题-回溯法-最大团问题
- np.where使用:根据条件生成新的数组
- [Windows]win10时间同步错误的解决方法
- 3.企业应用架构模式 --- 映射到关系数据库
- C语言范例学习03-上
- 曾用一个肾买的iPhone4s,现在能换两个不锈钢脸盆
- axure导入的元件库不见了_Axure教程丨制作自己的Axure元件库
- 谈谈创业这点事(4)
- 抓考研英语单词主要矛盾的经验分享,考研英语真题词频统计
- 《App违法违规收集使用个人信息自评估指南》
- 拼多多商品详情百亿补贴数据采集接口代码展示
- 小米HR:说说对API有多少的理解? 看了后,和面试官扯皮,吹牛逼!绰绰有余!
- hackinglab-脚本关10——基情燃烧的岁月
- php微信使用腾讯地图进行定位,微信小程序在腾讯地图上选择定位