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对象的属性和数组元素

 

  1. $:根节点,也是所有jsonpath表达式的开始

  2. .:访问属性,表示获取子节点

  3. ..: 表示获取所有符合条件的内容

  4. []:访问数组元素,表示迭代器的标示(可以用于处理下标等情况)

  5. [,] :表示多个结果的选择

  6. *:通配符,匹配任何属性或数组元素,代表所有的元素节点

  7. @:当前节点

  8. ?():表示过滤操作

  9. $.property:访问JSON对象的属性值。例如,$表示JSON根对象,$.name表示根对象的name属性值

  10. $[index]:访问JSON数组的元素值。例如,$[0]表示数组的第一个元素。

  11. $.property[index]:访问JSON对象中的数组元素。例如,$.students[0]表示对象的students属性中的第一个元素。

  12. $.property[*]:访问JSON对象中的所有数组元素。例如,$.students[*]表示对象的students属性中的所有元素。

  13. $.property.*:访问JSON对象中的所有子属性。例如,$.student.*表示对象的student属性中的所有子属性。

  14. $.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)]"
  • 24
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值