美文网首页
02_JsonPath

02_JsonPath

作者: 雨点的光辉 | 来源:发表于2024-05-09 15:07 被阅读0次

    一、介绍

    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(布尔类型的值)
    2.如果表达式正确,匹配失败则返回False(布尔类型的值),匹配成功则返回一个列表list
    3.如果原数据中有多个满足匹配的value,则按原数据中的value顺序依次存到list中返回

    三、实例

    1. 实例一

    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方法

    2. 实例二

    data = [
        {
            "data": 123,
            "num": 100
         },
        {
            "data": 456,
            "num": 200
        }
    ]
    
    print(f'''$.[1]:{jsonpath.jsonpath(data, "$.[1]")}''')
    

    输出结果如下:

    $.[1]:[{'data': 456, 'num': 200}]
    

    相关文章

      网友评论

          本文标题:02_JsonPath

          本文链接:https://www.haomeiwen.com/subject/vblyxjtx.html