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', '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', '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', 'price': 8.95
}
]
​
Process finished with exit code 0

(7)输出book节点中所有含有isbn的对象:

checkurl = "$.store.book[?(@.isbn)]"
data=jsonpath.jsonpath(bookJson, 'price': 22.99
 }
]
​
Process finished with exit code 0

3、JSONPATH的过滤器

JSONPATH还支持使用过滤器来对JSON数据进行更精细的查询和过滤。过滤器使用方括号([])中的表达式来指定要过滤的条件。下面是一些常见的过滤器:

  • ==:等于

  • !=:不等于

  • <:小于

  • <=:小于或等于

  • >:大于

  • >=:大于或等于

  • =~:正则表达式匹配

  • !~:不匹配正则表达式

示例:

checkurl = "$.store.book[?(@.price<10)]"

相关文章

文章浏览阅读2.4k次。最近要优化cesium里的热力图效果,浏览...
文章浏览阅读1.2w次,点赞3次,收藏19次。在 Python中读取 j...
文章浏览阅读1.4k次。首字母缩略词 API 代表应用程序编程接口...
文章浏览阅读802次,点赞10次,收藏10次。解决一个JSON反序列...
文章浏览阅读882次。Unity Json和Xml的序列化和反序列化_uni...