luca
luca

Reputation: 3328

Use of JSONObject and JSONArray in Jmeter

I have a problem with the construction of a Json for Amazon Kinesis. This json has to have this format:

{
    "Records": [
        {
            "Data": "XzxkYXRhPl8x",
            "PartitionKey": "partitionKey1"
        },
        {
            "Data": "f1PxFQo92Afh",
            "PartitionKey": "partitionKey2"
        },
        {
            "Data": "Gi4sEdd08HypA",
            "PartitionKey": "partitionKey3"
        }
    ],
    "StreamName": "exampleStreamName"
}

I use an BeanShell Sampler to create the json as a buffer:

import org.json.JSONArray;
import org.json.JSONObject;


//Dichiarazione variabili
int timestampValue=(${startTime}+${i}+1);
float current_powerValue=${current_power_1}+${__Random(0,10)};
String idValue=${__threadNum}+"_"+"5";
JSONObject part = new JSONObject();


//Create JSON

part.put("timestamp",timestampValue);
part.put("parent","${__threadNum}");
part.put("id",idValue);
part.put("state","on");
part.put("today_kwh",65);
part.put("current_power",current_powerValue);
part.put("today_on_time",0);
part.put("on_for",0);
part.put("today_standby_time",0);

//ADD json to array
if(${i}%(${bufferSize}*${sample}-1)==0 && ${i}!=0 || ${i}==${totalNumber}-${endOfDb}){
    //Add to json variable the last json created
    vars.put("json",vars.get("json")+part.toString());
    //Make an JSONObject by json variable of jmeter
    JSONObject tempArray= new JSONObject(vars.get("json"));
    log.info(tempArray.toString());
    //Add tempArray into JSONArray so that it adds square brackets
    JSONArray records= new JSONArray();
    records.put(tempArray);
    //Add the second field streamName
    JSONObject kinesis = new JSONObject();
    kinesis.put("records",records);
    kinesis.put("streamName","kinesis");
    //save into jsonBuffer
    vars.put("jsonBuffer",kinesis.toString());
    //restart json variable
    vars.put("json","");    
}
else{
    //add new json into variable so to store it.
    vars.put("json", vars.get("json")+part.toString()+",");
}

I use json variable in jmeter to save the json for each iteraction and when the "i" variable respect the if clause then I start to create the json structure. So I add the last json to the jmeter variable, then I create a JSONObject to store this json but when i do this it store only one json (because it is an object). Unfortunately if I store in a JSONArray it add "" because read the variable json as a string. The best solution would be use only JSONObject and JSONArray but how I use the same object for all the iteractions(in jmeter i can't use JSONArray) This is my jmx enter image description here

Upvotes: 2

Views: 4801

Answers (1)

Matteo
Matteo

Reputation: 1953

You could trt with this snippet:

if(${i}%(${bufferSize}*${sample}-1)==0 && ${i}!=0 || ${i}==${totalNumber}-${endOfDb}){
 vars.put("json",vars.get("json")+part.toString());
 JSONArray records= new JSONArray("["+vars.get("json")+"]");
 log.info(records.toString());
 //records.put(tempArray);
 JSONObject kinesis = new JSONObject();
 kinesis.put("records",records);
 kinesis.put("streamName","kinesis");
 vars.put("jsonBuffer",kinesis.toString());
 vars.put("json",""); 
}

Upvotes: 1

Related Questions