jsonpath是用来解析多层嵌套的json数据。可以认为jsonpath就是json版本的xpath。它是一种信息抽取类库,是从JSON文档中抽取指定信息的工具。jsonpath对于json来说,就相当于xapth至于xml
1.安装
通过pip命令进行安装
pip install jsonpath
jsonpath表达式既可以使用点号表示法,也可以使用括号表示法。
$.store.book [0].title
和
$["store"]["book"] [0][“title"]
所表达的意思是一样的。
2.基础语法
2.1 常用操作符
| 语法 | 含义 |
|---|---|
| $ | 表示根节点 |
| @ | 当前节点 |
| .<节点名称> | 获取子节点 |
| [<节点名称1>(,<节点名称2>)] | 获取子节点,与点号不同,这里可以获取多个子节点 |
| * | 匹配所有元素节点 |
| [] | 迭代器标识,可以在里面做简单的迭代操作,如数组下标,根据内容选取值等 |
| [,] | 支持迭代器中做多选 |
| ?() | 支持过滤操作 |
| () | 支持表达式计算 |
| .. | 获取子孙节点,无论嵌套多少层,都可以获取到 |
| [<下标1>(,<下标2>)] | 表示一个或多个下标 |
| [start:end] | 表示切片语法 |
| [?(<表达式>)] | 过滤器表达式,表达式结果必须是布尔值 |
2.2 常用函数
可以在JsonPath表达式执行后进行调用,其输入值为表达式的结果
| 名称 | 描述 | 输出结果 |
|---|---|---|
| min() | 获取数值型数组的最小值 | double |
| max() | 获取数值型数组的最大值 | double |
| avg() | 获取数值型数组的平均值 | double |
| std() | 获取数值型数组的标准差 | double |
| length() | 获取数值型数组的长度 | int |
2.3 常用过滤器
| 符号 | 描述 |
|---|---|
| == | 等于 |
| != | 不等于 |
| < | 小于 |
| <= | 小于等于 |
| 大于 | |
| >= | 大于等于 |
| =~(这里是波浪号~) | 判断是否符合正则表达式 |
| in | 属于符号 |
| nin | 相当于not in |
| size | |
| empty | 判空符号 |
3.使用实例
3.1 获取基本字段信息
import jsonpath
json_data={
"error_code": 0,
"stu_info": [
{
"id": 2059,
"name": "小白",
"sex": "男",
"age": 28,
"addr": "河南省济源市北海大道32号",
"grade": "天蝎座",
"phone": "18378309272",
"gold": 10896,
"info":{
"card":434345432,
"bank_name":'中国银行'
}
},
{
"id": 2067,
"name": "小黑",
"sex": "男",
"age": 28,
"addr": "河南省济源市北海大道32号",
"grade": "天蝎座",
"phone": "12345678915",
"gold": 100
}
]
}
3.1.1 获取所有name值
names = jsonpath.jsonpath(json_data, '$..name')
print(names)
result:
['小白', '小黑']
这里的..name'整体表示从根节点开始查找,查找所有节点的name属性。获取的结果为一个列表。
3.1.2 获取所有bank_name值
names = jsonpath.jsonpath(json_data, '$..bank_name')
print(names)
result:
['中国银行']
3.1.3 获取不存在的信息
当传入的key不存在时,返回False
names = jsonpath.jsonpath(json_data, '$..score')
print(names)
result:
False
3.1.4 获取info下所有字段的值
infos = jsonpath.jsonpath(json_data, '$..info.*')
print(infos)
result:
[434345432, '中国银行']
3.2 下标和切片用法
json_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
}
},
"expensive": 10
}
3.2.1 获取前2本书的信息
infos = jsonpath.jsonpath(json_data, '$..book[0,1]')
print(infos)
result:
[{'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}]
infos = jsonpath.jsonpath(json_data, '$..book[:2]')
print(infos)
result:
[{'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}]
3.2.2 获取最后2本书的信息
infos = jsonpath.jsonpath(json_data, '$..book[-2:]')
print(infos)
result:
[{'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}]
3.3 条件筛选
json_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
}
},
"expensive": 10
}
3.3.1 获取所有具有isbn属性的书
infos = jsonpath.jsonpath(json_data, '$..book[?(@.isbn)]')
print(infos)
result:
[{'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}]
3.3.2 获取价格小于10的书籍信息
infos = jsonpath.jsonpath(json_data, '$..book[?(@.price<10)]')
print(infos)
result:
[{'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}]