JsonPath

一、簡介

JSONPath表達(dá)式與XPath表達(dá)式相似,它們通常與XML文檔結(jié)合使用。由于JSON結(jié)構(gòu)通常是匿名的,并且不一定具有“根成員對象”,因此JSONPath假定分配給外部級別對象的抽象名稱$。在測試工作中,通常利用jsonpath解析json數(shù)據(jù),并進(jìn)行斷言。

二、JsonPath與XPath語法對比

XPath JSONPath Description
/ $ 根節(jié)點(diǎn)
. @ 當(dāng)前節(jié)點(diǎn)
/ . or [] 子節(jié)點(diǎn)
.. n/a 父節(jié)點(diǎn)
// .. 子孫節(jié)點(diǎn)
* * 匹配所有元素節(jié)點(diǎn)
@ n/a 屬性節(jié)點(diǎn), JSON沒有屬性
[] [] 迭代器標(biāo)示
| [,] 支持迭代器中做多選
[] ?() 過濾操作表達(dá)式
n/a () script 表達(dá)式
() 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 獲取書店所有商品價(jià)格信息
//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)

結(jié)果:

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

結(jié)果:

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

結(jié)果

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

結(jié)果:

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

結(jié)果:

[{'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)

結(jié)果:

[{'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)

結(jié)果:

[{'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)

結(jié)果:

[{'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)

結(jié)果:

[{'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/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容