JSONPath 介绍
XPath for JSON:JSONPath主要是一种路径表达式语法,它可以选择JSON结构的一部分,就像XPath表达式选择XML文档的节点一样
JSONPath 安装
pip install jsonpath
JSONPath 表达式
JSONPath表达式引用JSON结构的方式与XPath表达式与XML文档结合使用的方式相同
在JSONPath 中,
- $ 表示根对象/元素
- @ 表示当前对象/元素
- 通配符,表示任意对象
- . 或 [] 表示子对象
- .. 表示后代元素,相当于xpath 中的//
- ?(boolean expr) 表示过滤表达式,例如:$.store.book[?(@.price < 10)].title 表示价格小于10的书籍标题
- (
) 脚本表达式,被括号引起来使用,例如 $.store.book[(@.length-1)].title 表示最后一本书籍的标题 - [start:end:step] 表示切片
JSONPath 表达式与 XPATH 表达式的对应关系
XPath | JSONPath | Description |
---|---|---|
/ | $ | 表示根对象/元素 |
. | @ | 表示当前对象/元素 |
/ | . or [] | 子对象 |
// | .. | 表示后代元素 |
* | * | 通配符,表示任意对象 |
[] | [] | 下标运算符 |
| | [,] | 联合运算符 |
JSONPath 示例
import jsonpath
data_json = { "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
}
}
}
获取所有书籍的作者
jsonpath.jsonpath(data_json, '$.store.book[*].author')
out:
['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien']
获取所有的作者
jsonpath.jsonpath(data_json, '$..author')
out:
['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien']
获取store元素的所有子元素列表
jsonpath.jsonpath(data_json, '$.store.*')
out:
[[{'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}]
获取所有的价格
jsonpath.jsonpath(data_json, '$.store..price')
out:
[8.95, 12.99, 8.99, 22.99, 19.95]
获取第3本书
jsonpath.jsonpath(data_json, '$..book[2]')
out:
[{'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}]
获取最后一本书
jsonpath.jsonpath(data_json, '$..book[(@.length-1)]')
或
jsonpath.jsonpath(data_json, '$..book[-1:]')
out:
[{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]
获取前两本书
jsonpath.jsonpath(data_json, '$..book[0,1]')
或
jsonpath.jsonpath(data_json, '$..book[:2]')
out:
[{'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有值的书籍
jsonpath.jsonpath(data_json, '$..book[?(@.isbn)]')
out:
[{'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}]
过滤出价格低于10的书籍
jsonpath.jsonpath(data_json, '$..book[?(@.price<10)]')
out:
[{'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}]
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。