简体   繁体   English

一个节点为空的Jmeter JSON Extractor

[英]Jmeter JSON Extractor with one node empty

I have a JSON Response like this 我有这样的JSON响应

`{
   "data": [
      {
         "id": "1",
          "type": "status",
         "created_time": "2010-08-02T22:27:44+0000",
         "updated_time": "2010-08-02T22:27:44+0000"
      },
      {
         "id": "2",
         "message": "JSON is much easier and better than XML",
         "created_time": "2010-08-02T25:27:44+0000",
         "updated_time": "2010-08-02T25:27:44+0000"
      },
      {
         "id": "3",
         "created_time": "2010-08-02T25:27:44+0000",
         "updated_time": "2010-08-02T25:27:44+0000"
      },
      {
         "id": "4",
         "message": "JSON is much easier and better than XML",
         "created_time": "2010-08-02T25:27:44+0000",
         "updated_time": "2010-08-02T25:27:44+0000"
      },
      {
         "id": "5",
         "created_time": "2010-08-02T25:27:44+0000",
         "updated_time": "2010-08-02T25:27:44+0000"
      },
      {
         "id": "6",
         "message": "JSON is much easier and better than XML",
         "created_time": "2010-08-02T25:27:44+0000",
         "updated_time": "2010-08-02T25:27:44+0000"
      }
   ]
}`

Now as you can see that node 1, 3 and 5 are missing the message field I am extracting the values for id, message, created_time and updated_time using json extractor with match no -1 values are extracted correctly i am them writing them in to the CSV file inside for loop using the ID_matchNr for maximum number But the problem I am facing is that since we don't have any value in node 1 but when I write it write the message value of node 2. 现在您可以看到节点1、3和5缺少消息字段,我正在使用json提取器提取id,message,created_time和updated_time的值,并且匹配的-1值均未正确提取,我正在将它们写入循环中的CSV文件使用ID_matchNr作为最大数,但是我面临的问题是,由于节点1中没有任何值,但是当我编写它时,写入节点2的消息值。

How can I write null for message for node 1, 3 & 5? 如何为节点1、3和5的消息写空值?

One of the option i thought was add a dummy sampler and use the same payload and extract the message value using the conditional json extractor based on extracted ID from the 1st sampler response. 我认为的选项之一是添加一个虚拟采样器,并使用相同的有效负载,并根据从第一个采样器响应中提取的ID,使用条件json提取器提取消息值。

One of the other option I thought was to use the foreachcontroller and use the sampler to write to file this was I may be able to use the corresponding message value of id but this will again have another beanshell sampler to write to file. 我认为的另一个选择是使用foreachcontroller并使用采样器将其写入文件,这是我可以使用相应的消息值id的原因,但这又将具有另一个beanshell采样器来写入文件。

But I was hoping to have simpler solution rather than making another sampler request 但是我希望有一个更简单的解决方案,而不是提出另一个采样器请求

This is how I want the CSV File data to look like: 这就是我希望CSV文件数据如下所示的方式:

在此处输入图片说明

To achieve this, you need to parse that JSON Response with JSON Class in Java. 为此,您需要使用Java中的JSON类解析该JSON响应。

  1. Get the JSON Library from Maven Repository and put it in JMeter Classpath 从Maven存储库获取JSON库并将其放在JMeter Classpath中

    在此处输入图片说明

  2. Make sure you restart JMeter 确保重新启动JMeter

  3. Extract full JSON Response using JSON Extractor 使用JSON提取器提取完整的JSON响应

  4. Put the following JAVA Code in JSR223 Sampler and select language as java 将以下JAVA代码放入JSR223 Sampler并选择语言作为Java

     import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; String response = vars.get("jsonOutput"); JSONObject myObject = new JSONObject(response); JSONArray data = myObject.optJSONArray("data"); log.info("ID\\tType\\t\\tMessage\\tCreated Time\\tUpdated Time"); for (int i = 0, size = data.length(); i < size; i++) { JSONObject objectInArray = data.getJSONObject(i); String id = objectInArray.optString("id"); String type = objectInArray.optString("type"); String created_time = objectInArray.optString("created_time"); String updated_time = objectInArray.optString("updated_time"); String message = objectInArray.optString("message"); if(id.isEmpty()) { id = null; } if(type.isEmpty()) { type = null; } if(created_time.isEmpty()) { created_time = null; } if(updated_time.isEmpty()) { updated_time = null; } if(message.isEmpty()) { message = null; } log.info(id + "\\t" + type + "\\t" + message + "\\t" + created_time + "\\t" + updated_time); } 

在此处输入图片说明 在此处输入图片说明

Thanks very much @SAIR this was brilliant I am not a coder but managed to workaround my way. 非常感谢@SAIR,这真是太好了,我不是编码人员,但是设法解决了我的问题。 This solved lot of my current and future problem now I don't have to call dummy samplers inside the foreach controller. 这解决了我当前和将来的许多问题,现在我不必在foreach控制器中调用虚拟采样器。

I added another line to extract nested field here's the modified code I am using 我添加了另一行来提取嵌套字段,这是我正在使用的修改后的代码

JSONObject NameobjectInArray = objectInArray.getJSONObject("Name");  -- **I used this to get the nested fields**

String FirstName = NameobjectInArray.optString("FirstName"); 字符串FirstName = NameobjectInArray.optString(“ FirstName”);

You can consider using JSR223 PostProcessor and JsonSlurper combination to perform this in a single shot 您可以考虑结合使用JSR223 PostProcessorJsonSlurper来一次完成此操作

  1. Add JSR223 PostProcessor as a child of the request which returns above JSON 将JSR223 PostProcessor添加为返回JSON之上的请求的子代
  2. Put the following code into "Script" area: 将以下代码放入“脚本”区域:

     new groovy.json.JsonSlurper().parse(prev.getResponseData()).data.each { entry -> new File('myfile.csv') << entry.id << ',' << entry.message << ',' << entry.created_time << ',' << entry.updated_time << System.getProperty('line.separator') } 
  3. That's it, once you run your test you will myfile.csv will be generated in "bin" folder of you JMeter installation looking like: 就是这样,一旦运行测试,将在JMeter安装的“ bin”文件夹中生成myfile.csv ,如下所示:

     1,null,2010-08-02T22:27:44+0000,2010-08-02T22:27:44+0000 2,JSON is much easier and better than XML,2010-08-02T25:27:44+0000,2010-08-02T25:27:44+0000 3,null,2010-08-02T25:27:44+0000,2010-08-02T25:27:44+0000 4,JSON is much easier and better than XML,2010-08-02T25:27:44+0000,2010-08-02T25:27:44+0000 5,null,2010-08-02T25:27:44+0000,2010-08-02T25:27:44+0000 6,JSON is much easier and better than XML,2010-08-02T25:27:44+0000,2010-08-02T25:27:44+0000 

More information: Apache Groovy - Why and How You Should Use It 更多信息: Apache Groovy-为什么以及如何使用它

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM