I have a JSON Response like this
`{
"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.
How can I write null for message for node 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.
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.
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:
To achieve this, you need to parse that JSON Response with JSON Class in Java.
Get the JSON Library from Maven Repository and put it in JMeter Classpath
Make sure you restart JMeter
Extract full JSON Response using JSON Extractor
Put the following JAVA Code in JSR223 Sampler and select language as 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. This solved lot of my current and future problem now I don't have to call dummy samplers inside the foreach controller.
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");
You can consider using JSR223 PostProcessor and JsonSlurper combination to perform this in a single shot
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') }
That's it, once you run your test you will myfile.csv
will be generated in "bin" folder of you JMeter installation looking like:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.