JSONPath断言

一、简介

JSONPath表达式与XPath表达式相似,它们通常与XML文档结合使用。由于JSON结构通常是匿名的,并且不一定具有“根成员对象”,因此JSONPath假定分配给外部级别对象的抽象名称$。在测试工作中,通常利用jsonpath解析json数据,并进行断言。

二、JsonPath与XPath语法对比

XPath JSONPath Description
/ $ 根节点
. @ 当前节点
/ . or [] 子节点
.. n/a 父节点
// .. 子孙节点
* * 匹配所有元素节点
@ n/a 属性节点, JSON没有属性
[] [] 迭代器标示
| [,] 支持迭代器中做多选
[] ?() 过滤操作表达式
n/a () script 表达式
() n/a 分组

三、JSONPath 示例

{ "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
    }
  }
}
XPath JSONPath Result
/store/book/author $.store.book[*].author 获取书店所有书籍作者
//author $..author 获取所有书籍作者名称
/store/* $.store.* 获取书店所有信息
/store//price $.store..price 获取书店所有商品价格信息
//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)] 过滤操作(获取price小于10元的书籍信息)
//* $..* 获取所有信息
  • 获取书店所有书籍作者
info = jsonpath.jsonpath(data,"$.store.book[*].author")
print(info)

结果:

['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien']
  • 获取书店所有书籍作者
info = jsonpath.jsonpath(data,"$..author")
print(info)

结果:

['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien']
  • 获取书店所有信息
info = jsonpath.jsonpath(data,"$.store.*")
print(info)

结果

[[{'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}]
  • 获取书店所有价格
info = jsonpath.jsonpath(data,"$.store..price")
print(info)

结果:

[8.95, 12.99, 8.99, 22.99, 19.95]
  • 获取第三本书的信息
info = jsonpath.jsonpath(data,"$..book[2]")
print(info)

结果:

[{'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}]
  • 获取最后一本书的信息
info = jsonpath.jsonpath(data,"$..book[-1:]")
# info = jsonpath.jsonpath(data,"$..book[(@.length-1)]")
print(info)

结果:

[{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]
  • 获取第一本书和第二本书信息
info = jsonpath.jsonpath(data,"$..book[0,1]")
info = jsonpath.jsonpath(data,"$..book[:2]")
print(info)

结果:

[{'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}]
  • 过滤操作(获取含有isbn属性的书籍信息)
info = jsonpath.jsonpath(data,"$..book[?(@.isbn)]")
print(info)

结果:

[{'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}]
  • 过滤操作(获取price小于10元的书籍信息)
info = jsonpath.jsonpath(data,"$..book[?(@.price<10)]")
print(info)

结果:

[{'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}]

四、参考

https://goessner.net/articles/JsonPath/

posted @ 2020-03-23 22:13  xyztank  阅读(1673)  评论(0编辑  收藏  举报