Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert Json to Java Object in Jmeter

Our Requirement is we need to convert the Json response into Java object. May i know how to do it in Jmeter? Can we use Beanshell?

Here is my sample response

{"items":[],"indicators":
[{"name":"location","type":"country","title":"Country"},{"name":"total","type":"number","title":"Entities Total"}]

We are able to store into Csv or json format but how to convert this into Java object? Can any help us on this?

like image 408
Kavinkumar Muthu Avatar asked Mar 12 '26 01:03

Kavinkumar Muthu


1 Answers

You can use BeanShell for processing this JSON. You can use json-smart that comes as part of JMeter 3.3 (I don't know about previous versions). Here you have example code to process your json and obtain "type" from the 1st element of array "indicators":

import net.minidev.json.parser.JSONParser;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONArray;

JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);

String jsonString = prev.getResponseDataAsString();
JSONObject jsonObj = (JSONObject) p.parse(jsonString);
JSONArray arr = (JSONArray) jsonObj.get("indicators");

JSONObject indicator = (JSONObject) arr.get(0);
String typeString = (String) indicator.get("type");

log.info("TYPE:" + typeString);

vars.put("TYPEVAR", typeString);

BTW, the json you wrote is missing a final '}'.

like image 178
rodolk Avatar answered Mar 13 '26 14:03

rodolk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!