简体   繁体   English

如何在 JMeter 中使用 JSON 提取器提取 JSON 响应中的嵌套元素

[英]How to extract nested elements in JSON response using JSON extractor in JMeter

I would like to extract an element based on a condition inside a nested JSON response.我想根据嵌套 JSON 响应中的条件提取一个元素。 Using the widely searched example below, I would like to extract the storeid of all books authored by "Foo Bar":使用下面广泛搜索的示例,我想提取“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
}

I have tried using expressions like: $.store[?(@..author=='Foo Bar')].storeId我尝试使用如下表达式: $.store[?(@..author=='Foo Bar')].storeId

I have also tried to see if there are any store returned at all using: $.store[*].book[?(@.author=='Foo Bar')]我还尝试使用以下方法查看是否有任何商店返回: $.store[*].book[?(@.author=='Foo Bar')]

But no result returned... Please help.但没有返回结果......请帮助。 Thanks!谢谢!

What you produced is not a valid JSON so you won't be able to use JSON Extractor on it.您生成的不是有效的 JSON ,因此您将无法在其上使用 JSON Extractor。

If you amend this good example which you cannot even properly copy and paste to include your storeId :如果你修改这个你甚至不能正确复制和粘贴以包含你的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
}

You will be able to extract the storeId where one of the authors is Nigel Rees using the following expression:您将能够使用以下表达式提取其中一位作者是Nigel ReesstoreId

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

Demo:演示:

在此处输入图像描述

More information:更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 JSON 提取器提取 JSON 响应中的嵌套元素 - How to extract nested elements in JSON response using JSON extractor 如何使用 Json Extractor 从 Jmeter 中嵌套的 Json 中提取数据 - How to extract data from nested Json in Jmeter using Json Extractor 如何使用正则表达式提取器在jmeter中提取json响应数据? - how to extract json response data in jmeter using regular expression extractor? 如何使用 Jmeter 中的 JSON JMESpasth Extractor 从 JSON 响应中提取所有 id? - How to extract all id's from a JSON response using JSON JMESPasth Extractor in Jmeter? 如何使用正则表达式提取器在jmeter响应数据中提取JSON值 - How the JSON value extract process in jmeter response data using regular expression extractor 使用 jMeter 中的 JSON Extractor 从 JSON 中提取嵌套的 JSON 内容 - Extract nested JSON content from JSON with use of JSON Extractor in jMeter JMeter: JSON 提取器 - 使用多个条件提取 - JMeter: JSON Extractor - Extract using multiple conditions 如何在Jmeter中使用JSON提取器提取列表中的特定值 - How to Extract specific value in list using JSON extractor in Jmeter 如何使用正则表达式提取器提取jmeter中的Json值? - How to extract the Json values in jmeter using regular expression extractor? 使用JSON提取器从响应中提取值 - Using JSON extractor to extract values from the response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM