1 简介
-
官网:https://goessner.net/articles/JsonPath/;
-
JsonPath
是一种简单的方法来提取给定JSON
文档的部分内容;
-
JsonPath
支持多种编程语言,如Javascript
,Java
,Python
和PHP
。
2 官方实例
复制
| |
| { "store": { |
| |
| "book": [ |
| |
| { "category": "reference", |
| |
| "author": "Nigel Rees", |
| |
| "title": "Sayings of the Century", |
| |
| "price": 8.95 |
| |
| }, |
| |
| { "category": "fiction", |
| |
| "author": "Evelyn Waugh", |
| |
| "title": "Sword of Honour", |
| |
| "price": 12.99 |
| |
| }, |
| |
| { "category": "fiction", |
| |
| "author": "Herman Melville", |
| |
| "title": "Moby Dick", |
| |
| "isbn": "0-553-21311-3", |
| |
| "price": 8.99 |
| |
| }, |
| |
| { "category": "fiction", |
| |
| "author": "J. R. R. Tolkien", |
| |
| "title": "The Lord of the Rings", |
| |
| "isbn": "0-395-19395-8", |
| |
| "price": 22.99 |
| |
| } |
| |
| ], |
| |
| "bicycle": { |
| |
| "color": "red", |
| |
| "price": 19.95 |
| |
| } |
| |
| } |
| |
| } |
复制
3 JsonPath与XPath语法对比
Xpath | JsonPath | 描述 |
---|
/ | $ | 根节点 |
. | @ | 现行节点 |
/ | . 或 [] | 取子节点 |
… | 无 | 取父节点,Jsonpath 未支持 |
@ | 无 | 根据属性访问,Jsonpath 未支持,因为Json 是个Key-value 递归结构,不支持属性访问 |
* | * | 匹配所有元素节点 |
[] | [] | 迭代器标示(可以在里面做简单的迭代操作,如数组下标,根据内容选值等) |
竖线 | [,] | 支持迭代器中做多选。连接操作符在XPath 结果合并其它结点集合。Jsonpath 允许name 或者数组索引。 |
[] | ?() | 支持过滤操作 |
无 | [start: end: step] | 数组分割操作从ES4借鉴 |
无 | () | 脚本表达式,使用底层脚本引擎。支持表达式计算 |
() | 无 | Xpath 分组;JsonPath 不支持 |
4 实例说明JsonPath与XPath语法
XPath | JsonPath | 描述 |
---|
/store/book/author | $.store.book[*].author | 获取店内所有书籍的作者 |
//author | $..author | 获取所有作者 |
/store/* | $.store.* | 获取store 的所有元素。所有的book 和bicycle |
/store//price | $.store..price | 获取store 里面所有东西的价格 |
//book[3] | $..book[2] | 获取第三本书的所有信息 |
//book[last()] | $..book[(@.length-1)] 或$..book[-1:] | 获取最后一本书的所有信息 |
//book[position()<3] | $..book[0,1] 或 $..book[:2] | 获取前面两本书的所有信息 |
//book[isbn] | $..book[?(@.isbn)] | 过滤出所有的包含isbn 的书信息 |
//book[price<10] | $..book[?(@.price<10)] | 过滤出价格低于10的书 |
//* | $..* | 获取所有元素 |
5 Python中JsonPath模块
复制

6 Python中JsonPath使用
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import jsonpath as jp |
| |
| |
| data = { "store": { |
| |
| "book": [ |
| |
| { "category": "reference", |
| |
| "author": "Nigel Rees", |
| |
| "title": "Sayings of the Century", |
| |
| "price": 8.95 |
| |
| }, |
| |
| { "category": "fiction", |
| |
| "author": "Evelyn Waugh", |
| |
| "title": "Sword of Honour", |
| |
| "price": 12.99 |
| |
| }, |
| |
| { "category": "fiction", |
| |
| "author": "Herman Melville", |
| |
| "title": "Moby Dick", |
| |
| "isbn": "0-553-21311-3", |
| |
| "price": 8.99 |
| |
| }, |
| |
| { "category": "fiction", |
| |
| "author": "J. R. R. Tolkien", |
| |
| "title": "The Lord of the Rings", |
| |
| "isbn": "0-395-19395-8", |
| |
| "price": 22.99 |
| |
| } |
| |
| ], |
| |
| "bicycle": { |
| |
| "color": "red", |
| |
| "price": 19.95 |
| |
| } |
| |
| } |
| |
| } |
复制
| |
| |
| author = jp.jsonpath(data, '$.store.book[*].author') |
| |
| print(author) |
| |
| |
| |
| |
| ['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien'] |
复制
| |
| |
| |
| |
| all_aythor = jp.jsonpath(data, '$..author') |
| |
| print(all_aythor) |
| |
| |
| |
| |
| ['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien'] |
复制
| |
| |
| |
| book_bicycle = jp.jsonpath(data, '$.store.*') |
| |
| print(book_bicycle) |
| |
| |
| |
| |
| [[{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}, {'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}, {'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}], {'color': 'red', 'price': 19.95}] |
复制
| |
| |
| |
| price = jp.jsonpath(data, "$.store..price") |
| |
| print(price) |
| |
| |
| |
| |
| [8.95, 12.99, 8.99, 22.99, 19.95] |
复制
| |
| |
| |
| last_book = jp.jsonpath(data, '$.store..book[-1:]') |
| |
| print(last_book) |
| |
| |
| |
| |
| [{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}] |
复制
| |
| |
| |
| p = jp.jsonpath(data, '$.store..book[?(@.price<10)]') |
| |
| print(p) |
| |
| |
| |
| |
| [{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}] |
复制
7 结合接口测试的实例
| |
| |
| |
| http://127.0.0.1/zentao/api.php/v1/tokens |
| |
| data = {"account": "admin", "password": "123456"} |
复制
| |
| |
| |
| http://127.0.0.1/zentao/api.php/v1/user |
复制
复制
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import jsonpath as jp |
| |
| import requests |
| |
| import json |
| |
| |
| base_url = "http://127.0.0.1/zentao/api.php/v1" |
| |
| token_url = base_url + "/tokens" |
| |
| info_rurl = base_url + "/user" |
| |
| |
| header = {"Content-Type": "application/json"} |
| |
| data = {"account": "admin", "password": "ZenTao123456"} |
| |
| |
| r_data = json.dumps(data) |
| |
| r = requests.post(url=token_url, data=r_data, headers=header) |
| |
| |
| r_token = jp.jsonpath(r.json(), '$..token') |
| |
| print(r_token) |
| |
| |
| data1 = {"token": r_token[0]} |
| |
| headers = dict(header, **data1) |
| |
| r1 = requests.get(url=info_rurl, headers=headers) |
| |
| print(r1.text) |
复制
| |
| ['14333e8b34595c5d22794e14c401a125'] |
| |
| {"profile":{"id":1,"company":0,"type":"inside","dept":0,"account":"admin","role": |
| |
| {"code":"","name":""}, |
| |
| "realname":"admin","pinyin":"","nickname":"","commiter":"","avatar":null,"birthday":null,"gender":"f","email":"","skype":"","qq":"","mobile":"","phone":"","weixin":"","dingding":"","slack":"","whatsapp":"","address":"","zipcode":"","nature":null,"analysis":null,"strategy":null,"join":null,"visits":5,"visions":",rnd,lite,","ip":"127.0.0.1","last":"2023-07-31T06:14:45Z","fails":0,"locked":null,"feedback":"0","ranzhi":"","ldap":"","score":0,"scoreLevel":0,"resetToken":"","deleted":"0","clientStatus":"offline","clientLang":"zh-cn","admin":true,"superReviewer":false,"view": |
| |
| {"account":"admin","programs":"","products":"","projects":"","sprints":""}}} |
复制
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:【文末自行领取】
这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!
