jsonpath模块
从json格式中提取数据,通过字符串格式的提取表达式。 安装:
pip install jsonpath
| XPath | JSONPath | Description |
|---|---|---|
| / | $ | 表示根元素 |
| . | @ | 当前元素 |
| / | . or [] | 子元素 |
| .. | n/a | 父元素 |
| // | .. | 递归下降,JSONPath是从E4X借鉴的。 |
| * | * | 通配符,表示所有的元素 |
| @ | n/a | 属性访问字符 |
| [] | [] | 子元素操作符 |
| | | [,] | 连接操作符在XPath 结果合并其它结点集合。JSONP允许name或者数组索引。 |
| n/a | [start:end:step] | 数组分割操作从ES4借鉴。 |
| [] | ?() | 应用过滤表示式 |
| n/a | () | 脚本表达式,使用在脚本引擎下面。 |
| () | n/a | Xpath分组 |
- []在xpath表达式总是从前面的路径来操作数组,索引是从1开始。
- 使用JOSNPath的[]操作符操作一个对象或者数组,索引是从0开始。
data = {
"store": {
"book": [
{"category": "参考",
"author": "Nigel Rees",
"title": "世纪风俗",
"price": 8.95
},
{"category": "小说",
"author": "Evelyn Waugh",
"title": "荣誉剑",
"price": 12.99
},
{"category": "小说",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{"category": "小说",
"author": "JRR Tolkien",
"title": "指环王",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
data = {
"store": {
"book": [
{"category": "参考",
"author": "Nigel Rees",
"title": "世纪风俗",
"price": 8.95
},
{"category": "小说",
"author": "Evelyn Waugh",
"title": "荣誉剑",
"price": 12.99
},
{"category": "小说",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{"category": "小说",
"author": "JRR Tolkien",
"title": "指环王",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
from jsonpath import jsonpath
# 提取成功后返回一个列表,如果提取不到返回False
jsonpath(data, '$.store.book.0.title') # 第一本书的名称
['世纪风俗']
jsonpath(data, '$.store.book.*.author') # 所有书的作者
jsonpath(data, '$.store.book.[*].author')
['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'JRR Tolkien']
jsonpath(data, '$.store..price') # store所有物品的价格
[8.95, 12.99, 8.99, 22.99, 19.95]
jsonpath(data, '$..book[1]') # 第二本书
[{'category': '小说', 'author': 'Evelyn Waugh', 'title': '荣誉剑', 'price': 12.99}]
jsonpath(data, '$..book[(@.length-1)]') # 最后一本书,不支持负索引
[{'category': '小说',
'author': 'JRR Tolkien',
'title': '指环王',
'isbn': '0-395-19395-8',
'price': 22.99}]
jsonpath(data, '$..book[-1:]') # 支持切片,最后一本书
[{'category': '小说',
'author': 'JRR Tolkien',
'title': '指环王',
'isbn': '0-395-19395-8',
'price': 22.99}]
jsonpath(data, '$..book[0,2]') # 枚举索引
[{'category': '参考', 'author': 'Nigel Rees', 'title': '世纪风俗', 'price': 8.95},
{'category': '小说',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99}]
jsonpath(data, '$..book[?(@.isbn)]') # 过滤所有isbn的书
[{'category': '小说',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99},
{'category': '小说',
'author': 'JRR Tolkien',
'title': '指环王',
'isbn': '0-395-19395-8',
'price': 22.99}]
jsonpath(data, '$..book[?(@.price<10)]')
[{'category': '参考', 'author': 'Nigel Rees', 'title': '世纪风俗', 'price': 8.95},
{'category': '小说',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99}]
2. 提取函数封装
功能分析:
- 根据jsonpath表达式动态提取现有结果中的数据
- 动态绑定数据到指定的对象