简体   繁体   English

如何在 jmeter 中使用 JSON Extractor 从数组中提取值?

[英]How to extract values out of a array using JSON Extractor in jmeter?

I want to extract below json and use values accordingly.我想提取下面的 json 并相应地使用值。

I/p JSON:- I/p JSON:-

{
  "status": "Success",
  "message": "User created successfully",
  "id": [
    131188,
    131191
  ]
}

Here I want values of id field.这里我想要 id 字段的值。 I used JSON Extractor and gave expression as $.id which gives me [131188,131191] in a variable.我使用了 JSON Extractor 并给出了 $.id 表达式,它给了我 [131188,131191] 一个变量。 Now I want to use individual values out of this array ie 131188 and 131191. Any Idea how to do it?现在我想使用这个数组中的单个值,即 131188 和 131191。知道怎么做吗?

Update : I don't want to use 2 JSON Extractors.更新:我不想使用 2 个 JSON 提取器。

Just add [*] to your JSON path expression as below只需将 [*] 添加到您的 JSON 路径表达式中,如下所示

$.id[*]

This will create a jmeter variable for each value.Note that you should use -1 in the match numbers field.这将为每个值创建一个 jmeter 变量。请注意,您应该在匹配数字字段中使用-1

You could use a json extractor and a "JSR223 PostProcessor" with groovy language.您可以使用带有 groovy 语言的 json 提取器和“JSR223 PostProcessor”。 An example:一个例子:

   import groovy.json.JsonSlurper

   //String jsonString = vars.get("jsonFromExtractor")

   String jsonString = '''
   {
     "status": "Success",
     "message": "User created successfully",
     "id": [
       131188,
       131191
     ]
   }
   '''
   log.info("jsonString:" + jsonString)

   def json = new JsonSlurper().parseText( jsonString )

   String idValue1 = json.get("id").get(0)
   String idValue2 = json.get("id").get(1)
   log.info("idValue1:" + idValue1)
   log.info("idValue2:" + idValue2)

I hope this helps我希望这有帮助

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

相关问题 如何使用 Json Extractor 从 Jmeter 中嵌套的 Json 中提取数据 - How to extract data from nested Json in Jmeter using Json Extractor 如何在 Jmeter 中不使用 foreach controller 从数组中提取值(通过 JSON 提取器获得) - How to extract a value from an array (obtained through JSON extractor) without foreach controller in Jmeter JMeter JSON 提取器使用 -1 值 foreach controller 与不一致的数组 - JMeter JSON Extractor using -1 value for foreach controller with inconsistent array 如何解析Jmeter中Json的数组值 - How to parse array values from Json in Jmeter 使用 BeanShell 和 JSON 提取器或任何其他方式从 JSON 响应中检索和对数组值执行排序? - Retrieve and perform sorting on Array values from the JSON response using BeanShell and JSON extractor or any other way? 如何使用JMeter计算JSON数组的长度? - how to calculate length of JSON array using JMeter? 如何从数组中“提取”某些值? - How to “extract” certain values out of an array? JMeter:如何使用jsonpath计算数组中的JSON对象 - JMeter: How to count JSON objects in an Array using jsonpath 如何使用 json 数组从 JSON 文档中提取不同的值? - How to use json array to extract different values from JSON document? 给定一个json数组,如何使用jq按键提取键值列表? - Given a json array, how do I extract a list of key values by key, using jq?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM