当前位置:   article > 正文

JsonPath用法详解_jsonpath的用法

jsonpath的用法

JSONPath是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,提供多种语言实现版本,包括Javascript、Python、PHP和Java。

1、JSONPath安装:

  1. pip install jsonpath
  2. # 如果安装太慢可以使用清华源来加速安装
  3. 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 文档的内容如下:

  1. import jsonpath
  2. bookJson={
  3.  "store":{
  4.    "book":[
  5.     {
  6.        "category":"reference",
  7.        "author":"Nigel Rees",
  8.        "title":"Sayings of the Century",
  9.        "price":8.95
  10.     },
  11.     {
  12.        "category":"fiction",
  13.        "author":"J. R. R. Tolkien",
  14.        "title":"The Lord of the Rings",
  15.        "isbn":"0-395-19395-8",
  16.        "price":22.99
  17.     }
  18.   ],
  19.    "bicycle":{
  20.      "color":"red",
  21.      "price":19.95
  22.   }
  23. }
  24. }
 

(1)查看store下的bicycle的color属性:

  1. checkurl = "$.store.bicycle.color"
  2. data=jsonpath.jsonpath(bookJson, checkurl)
  3. print(data)
 
  1. ['red']
  2. Process finished with exit code 0

(2)输出book节点中包含的所有对象:

  1. checkurl = "$.store.book[*]"
  2. data=jsonpath.jsonpath(bookJson, checkurl)
  3. print(data)
  1. [
  2. {'category': 'reference',
  3.  'author': 'Nigel Rees',
  4.  'title': 'Sayings of the Century',
  5.  'price': 8.95
  6. },
  7. {'category': 'fiction',
  8.  'author': 'J. R. R. Tolkien',
  9.  'title': 'The Lord of the Rings',
  10.  'isbn': '0-395-19395-8',
  11.  'price': 22.99
  12. }
  13. ]
  14. Process finished with exit code 0

(3)输出book节点的第一个对象:

  1. checkurl ="$.store.book[0]"
  2. data=jsonpath.jsonpath(bookJson, checkurl)
  3. print(data)
  1. [
  2. {
  3.  'category': 'reference',
  4.  'author': 'Nigel Rees',
  5.  'title': 'Sayings of the Century',
  6.  'price': 8.95
  7. }
  8. ]
  9. Process finished with exit code 0

(4)输出book节点中所有对象对应的属性title值:

  1. checkurl ="$.store.book[*].title"
  2. data=jsonpath.jsonpath(bookJson, checkurl)
  3. print(data)
  1. ['Sayings of the Century', 'The Lord of the Rings']
  2. Process finished with exit code 0

(5)输出book节点中category为fiction的所有对象:

  1. checkurl = "$.store.book[?(@.category=='fiction')]"
  2. data=jsonpath.jsonpath(bookJson, checkurl)
  3. print(data)
  1. [
  2. {
  3.  'category': 'fiction',
  4.  'author': 'J. R. R. Tolkien',
  5.  'title': 'The Lord of the Rings',
  6.  'isbn': '0-395-19395-8',
  7.  'price': 22.99
  8. }
  9. ]
  10. Process finished with exit code 0

(6)输出book节点中所有价格小于10的对象:

  1. checkurl = "$.store.book[?(@.price<10)]"
  2. data=jsonpath.jsonpath(bookJson, checkurl)
  3. print(data)
  1. [
  2. {
  3.  'category': 'reference',
  4.  'author': 'Nigel Rees',
  5.  'title': 'Sayings of the Century',
  6.  'price': 8.95
  7. }
  8. ]
  9. Process finished with exit code 0

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

  1. checkurl = "$.store.book[?(@.isbn)]"
  2. data=jsonpath.jsonpath(bookJson, checkurl)
  3. print(data)
  1. [
  2. {
  3.  'category': 'fiction',
  4.  'author': 'J. R. R. Tolkien',
  5.  'title': 'The Lord of the Rings',
  6.  'isbn': '0-395-19395-8',
  7.  'price': 22.99
  8. }
  9. ]
  10. Process finished with exit code 0

3、JSONPATH的过滤器

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

  • ==:等于

  • !=:不等于

  • <:小于

  • <=:小于或等于

  • >:大于

  • >=:大于或等于

  • =~:正则表达式匹配

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

示例:

checkurl = "$.store.book[?(@.price<10)]"
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/775297
推荐阅读
相关标签
  

闽ICP备14008679号