【问题标题】:How to extract nested elements in JSON response using JSON extractor in JMeter如何使用 JMeter 中的 JSON 提取器提取 JSON 响应中的嵌套元素
【发布时间】:2020-09-01 23:50:40
【问题描述】:

我想根据嵌套 JSON 响应中的条件提取元素。使用下面广泛搜索的示例,我想提取由“Foo Bar”撰写的所有书籍的 storeid:

{
    "store": {
        "storeId": 1
        "book": [
            {
                "category": "reference",
                "author": [
                      {
                       "Nigel Rees",
                       "Foo Bar"
                      }
                ],
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": [
                      { 
                       "Evelyn Waugh"
                      }
                ],
                "title": "Sword of Honour",
                "price": 12.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

我尝试过使用如下表达式: $.store[?(@..author=='Foo Bar')].storeId

我还尝试查看是否有任何商店使用: $.store[*].book[?(@.author=='Foo Bar')]

但是没有返回结果...请帮忙。谢谢!

【问题讨论】:

    标签: json jmeter extractor


    【解决方案1】:

    您生成了 is not a valid JSON,因此您将无法在其上使用 JSON Extractor。

    如果您修改this good example,您甚至无法正确复制和粘贴以包含您的storeId

    {
      "store": {
        "storeId": 1,
        "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
        }
      },
      "expensive": 10
    }
    

    您将能够使用以下表达式提取storeId,其中一位作者是Nigel Rees

    $.store[?(@.book[?(@.author=="Nigel Rees")])].storeId
    

    演示:

    更多信息:

    【讨论】:

    • 感谢 Dmitri,但我认为这只会提取所有 storeId 和 book 而不会查看嵌套作者 == "Nigel Rees"...
    猜你喜欢
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多