1

How to structure Multiple array of JSON in the beanshell sampler

for example i need to pass N number of articles to a loop , so i have created a for loop to fetch the articles. here i have mentioned 3 articles as an example. but i need to fetch N number of articles in a loop.

The output should be like :

"itemLines": {
"itemLine": [
    {
        "bundleParentId": "",
        "id": "1",
        "itemType": "ART",
        "itemNo": "1234",
    },
    {
        "bundleParentId": "",
        "id": "2",
        "itemType": "ART",
        "itemNo": "2021",
    },{
        "bundleParentId": "",
        "id": "3",
        "itemType": "ART",
        "itemNo": "2023",
    }
]

}

My code in the beanshell smpler is : For example here i have mentioned in the array list with 3 article numbers.

public void createJsonStructure() {

try
{
    JSONObject rootObject = new JSONObject();
    JSONArray articleArr = new JSONArray();
    String[] article_list = {"00258882", "70234185", "00258882"};
    log.info(article_list.length);  
    for (i=0;i<=article_list.length;i++)
    {
    JSONObject article_list= new JSONObject();
    article_list.put("id", "i+1");
    article_list.put("itemNo",article_list[i]);
    article_list.put("requiredQty", "1");
    articleArr.put(article_list);
    }
   log.info(articleArr);        
    rootObject.put("itemLines", articleArr);
    log.info("rootObject is"+rootObject.toString(4));
    props.put("JsonObjectoutput", rootObject.toString(4));        
   }
catch (Exception ex)
{
    ex.printStackTrace();
    log.info("notes");
}

}

I could see the output is not retrieved in the jmeter logs . Here output should be printed in the logs , but i could see output is not printed.

Raksha
  • 11
  • 1
  • Isn't it a duplicate of https://stackoverflow.com/questions/56001955/how-to-structure-multiple-object-and-array-of-json-in-java-in-beanshell-and-revi – Abhishek Aggarwal May 06 '19 at 14:10

0 Answers0