JSONPath是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,提供多种语言实现版本,包括Javascript、Python、PHP和Java。
1、JSONPath安装:
pip install jsonpath # 如果安装太慢可以使用清华源来加速安装 pip install jsonpath -i https://pypi.tuna.tsinghua.edu.cn/simple
复制
2、JSONPath语法
JSONPath语法和XPATH语法对比 JSON结构清晰,可读性高,复杂度低,非常容易匹配。
JSONPath的语法与Xpath类似,如下表所示为JSONPath与XPath语法对比。
JSONPath语法它使用点号(.)和方括号([])来访问JSON对象的属性和数组元素
$:根节点,也是所有jsonpath表达式的开始
.:访问属性,表示获取子节点
..: 表示获取所有符合条件的内容
[]:访问数组元素,表示迭代器的标示(可以用于处理下标等情况)
[,] :表示多个结果的选择
*:通配符,匹配任何属性或数组元素,代表所有的元素节点
@:当前节点
?():表示过滤操作
$.property:访问JSON对象的属性值。例如,$表示JSON根对象,$.name表示根对象的name属性值
$[index]:访问JSON数组的元素值。例如,$[0]表示数组的第一个元素。
$.property[index]:访问JSON对象中的数组元素。例如,$.students[0]表示对象的students属性中的第一个元素。
$.property[*]:访问JSON对象中的所有数组元素。例如,$.students[*]表示对象的students属性中的所有元素。
$.property.*:访问JSON对象中的所有子属性。例如,$.student.*表示对象的student属性中的所有子属性。
$.property1.property2:访问JSON对象中的嵌套属性。例如,$.student.name表示对象的student属性中的name属性值。
下面使用一个JSON文档演示JSONPath的具体使用。JSON 文档的内容如下:
import jsonpath bookJson={ "store":{ "book":[ { "category":"reference", "author":"Nigel Rees", "title":"Sayings of the Century", "price":8.95 }, { "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 } } }
复制
复制
(1)查看store下的bicycle的color属性:
checkurl = "$.store.bicycle.color" data=jsonpath.jsonpath(bookJson, checkurl) print(data)
复制
复制
['red'] Process finished with exit code 0
复制
(2)输出book节点中包含的所有对象:
checkurl = "$.store.book[*]" data=jsonpath.jsonpath(bookJson, checkurl) print(data)
复制
[ {'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95 }, {'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99 } ] Process finished with exit code 0
复制
(3)输出book节点的第一个对象:
checkurl ="$.store.book[0]" data=jsonpath.jsonpath(bookJson, checkurl) print(data)
复制
[ { 'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95 } ] Process finished with exit code 0
复制
(4)输出book节点中所有对象对应的属性title值:
checkurl ="$.store.book[*].title" data=jsonpath.jsonpath(bookJson, checkurl) print(data)
复制
['Sayings of the Century', 'The Lord of the Rings'] Process finished with exit code 0
复制
(5)输出book节点中category为fiction的所有对象:
checkurl = "$.store.book[?(@.category=='fiction')]" data=jsonpath.jsonpath(bookJson, checkurl) print(data)
复制
[ { 'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99 } ] Process finished with exit code 0
复制
(6)输出book节点中所有价格小于10的对象:
checkurl = "$.store.book[?(@.price<10)]" data=jsonpath.jsonpath(bookJson, checkurl) print(data)
复制
[ { 'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95 } ] Process finished with exit code 0
复制
(7)输出book节点中所有含有isbn的对象:
checkurl = "$.store.book[?(@.isbn)]" data=jsonpath.jsonpath(bookJson, checkurl) print(data)
复制
[ { 'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99 } ] Process finished with exit code 0
复制
3、JSONPATH的过滤器
JSONPATH还支持使用过滤器来对JSON数据进行更精细的查询和过滤。过滤器使用方括号([])中的表达式来指定要过滤的条件。下面是一些常见的过滤器:
==:等于
!=:不等于
<:小于
<=:小于或等于
>:大于
>=:大于或等于
=~:正则表达式匹配
!~:不匹配正则表达式
示例:
checkurl = "$.store.book[?(@.price<10)]"复制