tsx81428 发表于 2024-7-18 15:42:29

JsonPath用法详解

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对象的属性和数组元素
 https://img-blog.csdnimg.cn/direct/80991684bda548448134275490d0532e.png
   
[*] $:根节点,也是所有jsonpath表达式的开始
[*] .:访问属性,表示获取子节点
[*] ..: 表示获取所有符合条件的内容
[*] []:访问数组元素,表示迭代器的标示(可以用于处置惩罚下标等情况)
[*] [,] :表示多个结果的选择
[*] *:通配符,匹配任何属性或数组元素,代表所有的元素节点
[*] @:当前节点
[*] ?():表示过滤操作
[*] $.property:访问JSON对象的属性值。例如,$表示JSON根对象,$.name表示根对象的name属性值
[*] $:访问JSON数组的元素值。例如,$表示数组的第一个元素。
[*] $.property:访问JSON对象中的数组元素。例如,$.students表示对象的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"
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)]"
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: JsonPath用法详解