02_JsonPath

一、介绍

  1. JsonPath 是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,提供多种语言实现版本,包括 Javascript、Python、PHP 和 Java
  2. 和jmeter中的JSON提取器很像

二、使用

1. JsonPath语法

语法 实例 描述
$ $.property 根节点,也是所有jsonpath表达式的开始,获取子节点的"property"key值
. $.property 获取子节点的"property"key值
.. $..property 获取子节点及以下所有子节点的"property"key值
[] $.property[0] 获取子节点的"property"列表中第1个值
[, ] $.property[0, 1] 获取子节点的"property"列表中第1和第2个值
[start:end:step] $.property[1:5:2] 按照固定间隔,获取子节点的"property"列表中指定位置的值,即第1和第3个值
* $.property[*] 获取子节点的"property"列表中所有值
@ $.data[?(@.age)]、$.data[(@.length-1)] 一般跟?()() 一起使用。表示获取data列表中所有值中带"age"key的值、获取data列表中第x个值,x等于data列表总长度-1
?() $.data[?(@.age)] 表示过滤操作。一般跟@ 一起使用。表示获取data列表中所有值中带"age"key的值
$.property.* 获取"property"key值中所有的值
$.property1.property2 获取"property1"key值中的"property2"key值

1.1 语法中的过滤操作

过滤操所对应符号 实例 描述
== $..book[?(@.price==10)] 等于
!= $..book[?(@.price!=10)] 不等于
< $..book[?(@.price<10)] 小于
<= $..book[?(@.price<=10)] 小于或等于
> $..book[?(@.price>10)] 大于
>= $..book[?(@.price>=10)] 大于或等于

注意:多个条件过滤使用&&连接,比如$..book[?(@.category=="fiction"&&@.price<10)]

2. 安装

需要安装JsonPath库才能使用提取功能

pip install jsonpath

3. 使用

在使用 JsonPath 的时候一般是使用它里面的jsonpath函数,即jsonpath.jsonpath()

jsonpath.jsonpath(obj, expr)
  • obj:JSONPath 表达式操作对象
    注意:操作对象是Python中的字典数据类型(dict)
  • expr:JSONPath 表达式,用于指定要提取的值的路径

注意:
1.如果表达式错误,则匹配结果会返回False(布尔类型的值)。如果表达式正确,则匹配结果会返回一个列表list
2.如果返回的列表中有多个value,则按匹配顺序依次存到list中返回。如果返回的列表中没有值,说明匹配失败

三、实例

data = { "store": {
    "book": [ 
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      { "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { "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
    }
  }
}

匹配表达式如下:

JSONPath 表达式 含义
$.store.book[*].author 商店中所有书籍的作者
$..author 所有作者
$.store.* 商店中所有东西
$.store..price 商店中所有东西的价格
$..book[2] 第三本书
$..book[(@.length-1)] 最后一本书
$..book[-1:] 最后一本书
$..book[0,1] 前两本书
$..book[:2] 前两本书
$..book[0::2] 从第1本书开始获取,每隔2本即第2次获取的是第3本书,依次类推
$..book[?(@.isbn)] 过滤所有带isbnkey的书籍
$..book[?(@.price<10)] 过滤所有价格小于10的书籍
$..book[?(@.category=="fiction")] 过滤所有categoryfiction的书籍
$..book[?(@.category=="fiction"&&@.price<10)] 过滤所有categoryfiction且价格少于10的书籍
$..* 获取所有数据
print(f"$.store.book[*].author:{jsonpath.jsonpath(data, '$.store.book[*].author')}")
print(f"$.store.*:{jsonpath.jsonpath(data, '$.store.*')}")
print(f"$.store..price:{jsonpath.jsonpath(data, '$.store..price')}")
print(f"$..book[2]:{jsonpath.jsonpath(data, '$..book[2]')}")
print(f"$..book[(@.length-1)]:{jsonpath.jsonpath(data, '$..book[(@.length-1)]')}")
print(f"$..book[-1:]:{jsonpath.jsonpath(data, '$..book[-1:]')}")
print(f"$..book[0,1]:{jsonpath.jsonpath(data, '$..book[0,1]')}")
print(f"$..book[:2]:{jsonpath.jsonpath(data, '$..book[:2]')}")
print(f"$..book[0::2]:{jsonpath.jsonpath(data, '$..book[0::2]')}")
print(f"$..book[?(@.isbn)]:{jsonpath.jsonpath(data, '$..book[?(@.isbn)]')}")
print(f"$..book[?(@.price<10)]:{jsonpath.jsonpath(data, '$..book[?(@.price<10)]')}")
print(f'''$..book[?(@.category=="fiction")]:{jsonpath.jsonpath(data, '$..book[?(@.category=="fiction")]')}''')
print(f'''$..book[?(@.category=="fiction"&&@.price<10)]:{jsonpath.jsonpath(data, '$..book[?(@.category=="fiction"&&@.price<10)]')}''')
print(f"$..*:{jsonpath.jsonpath(data, '$..*')}")

输出结果如下:

$.store.book[*].author:['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien']
$.store.*:[[{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}, {'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}, {'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}], {'color': 'red', 'price': 19.95}]
$.store..price:[8.95, 12.99, 8.99, 22.99, 19.95]
$..book[2]:[{'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}]
$..book[(@.length-1)]:[{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]
$..book[-1:]:[{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]
$..book[0,1]:[{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}]
$..book[:2]:[{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}]
$..book[0::2]:[{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}]
$..book[?(@.isbn)]:[{'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}, {'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]
$..book[?(@.price<10)]:[{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}]
$..book[?(@.category=="fiction")]:[{'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}, {'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}, {'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]
$..book[?(@.category=="fiction"&&@.price<10)]:[{'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}]
$..*:[{'book': [{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}, {'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}, {'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}}, [{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}, {'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}, {'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}], {'color': 'red', 'price': 19.95}, {'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}, {'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}, {'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}, 'reference', 'Nigel Rees', 'Sayings of the Century', 8.95, 'fiction', 'Evelyn Waugh', 'Sword of Honour', 12.99, 'fiction', 'Herman Melville', 'Moby Dick', '0-553-21311-3', 8.99, 'fiction', 'J. R. R. Tolkien', 'The Lord of the Rings', '0-395-19395-8', 22.99, 'red', 19.95]

注意:在java中的jsonpath库支持.length方法,但是python的jsonpath库是不支持.length方法

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335