jsonpath is a simple method to extract part of a given json document. When we do interface testing, the current main popular data structure is json. When encountering complex json formats, use jsonpath to extract data
When we use jsonpath, we first need to install jsonpath
pip install jsonpath
Compared with JSONPath syntax and XPATH syntax, JSON has a clear structure, high readability, low complexity, and is very easy to match. The syntax of JSONPath is similar to that of XPath. The following table shows the syntax comparison between JSONPath and XPath.
XPATH | JSON | |
/ | $ | Represents the root element |
. | @ | current element |
/ | .or[] | child element |
.. | n/a | parent element |
// | .. | Recursive descent, JSONPATH borrowed from EX4 |
* | * | Wildcard means all elements |
@ | n/a | attribute access character |
[] | [] | Child element access character |
| | [,] | The concatenation operator combines XPath results and sets them with other nodes, JSON allows name or array indexing |
n/a | [start:end:step] | Array separator borrowed from ES4 |
[] | ?() | apply filter expression |
n/a | () | Script expression, used below the script |
() | n/a | XPath grouping |
Use the JSON document to demonstrate the role of JSONPATH:
The content of the JSON file is as follows:
{"store": {
"book": [
{"category": "玄幻",
"author": "唐家三少",
"title": "斗罗大陆",
"price": 8.95
},
{"category": "历史",
"author": "愤怒的香蕉",
"title": "赘婿",
"price": 12.99
},
{"category": "玄幻",
"author": "爱潜水的乌贼",
"title": "诡秘之主",
"isbn": "0-553-21311-3",
"price": 8.99
},
{"category": "仙侠",
"author": "祭酒",
"title": "地煞七十二变",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"author": "大司马",
"color": "red",
"price": 19.95
}
}
}
Preparation before inquiry:
#导入包
import jsonpath
import json
#对json文件进行反序列化
obj = json.load(open('073_尚硅谷_爬虫_解析_jsonpath.json','r',encoding='utf-8'))
1: Query the authors of all bookstores
#书店所有书的作者
author_list = jsonpath.jsonpath(obj,'$.store.book[*].author')
print(author_list)
2: All authors (since there are multiple indicators in the file, we can query the information in multiple indicators)
author_list= jsonpath.jsonpath(obj,'$..author')
print(author_list)
3: All elements below the store
tag_list =jsonpath.jsonpath(obj,'$.store.*')
print(tag_list)
4: All the prices in the store
price_list =jsonpath.jsonpath(obj,'$.store..price')
print(price_list)
5: The third book
book = jsonpath.jsonpath(obj,'$..book[2]')
print(book)
6: The Last Book
book = jsonpath.jsonpath(obj,'$..book[(@.length-1)]')
print(book)
7: The first two books
#book_list = jsonpath.jsonpath(obj,'$..book[0,1]')
book_list = jsonpath.jsonpath(obj,'$..book[:2]')
print(book_list)
8: Filter out all books containing isbn
#条件过滤需要在()前加一个?
book_list = jsonpath.jsonpath(obj,'$..book[?(@.isbn)]')
print(book_list)
9: That book is more than 10 yuan
book_list = jsonpath.jsonpath(obj,'$..book[?(@.price>10)]')
print(book_list)