3

I have a Sample JSON response as below:

{
"id": 37,
"merchant_id": "39",
"title": "Parker Pens",
"subtitle": null,
"price": 1000,
"description": null,
"images": [],
"image_thumbs": [],
"options": [{
    "code": "color",
    "label": "Color",
    "extra_info": "",
    "values": [{
        "label": "Red",
        "value": "8"
    }, {
        "label": "Yellow",
        "value": "9"
    }, {
        "label": "Pink",
        "value": "10"
    }]
  }, {
    "code": "size",
    "label": "Size",
    "extra_info": "",
    "values": [{
        "label": "Small",
        "value": "4"
    }, {
        "label": "Medium",
        "value": "5"
    }, {
        "label": "Large",
        "value": "6"
    }]
}],
"options_available": [{
    "combination": [{
        "code": "color",
        "value": "Red"
    }, {
        "code": "size",
        "value": "Small"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Red"
    }, {
        "code": "size",
        "value": "Medium"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Red"
    }, {
        "code": "size",
        "value": "Large"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Yellow"
    }, {
        "code": "size",
        "value": "Small"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Yellow"
    }, {
        "code": "size",
        "value": "Medium"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Yellow"
    }, {
        "code": "size",
        "value": "Large"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Pink"
    }, {
        "code": "size",
        "value": "Small"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Pink"
    }, {
        "code": "size",
        "value": "Medium"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Pink"
    }, {
        "code": "size",
        "value": "Large"
    }]
}],
"custom_options": []
}

and i have my Sampler as

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

String response= prev.getResponseDataAsString();
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("options");
Integer count= jsonArray.length();
vars.put('counts', count);

But while running the script I'm getting an error: No signature of method: org.apache.jmeter.threads.JMeterVariables.put() is applicable for argument types: (java.lang.String, java.lang.Integer)

In addition to values also (Counts=2). My intention is to get the count of array in "Options" Key ( See response above)

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Siddish
  • 97
  • 3
  • 12

2 Answers2

5

vars.put doesn't support different value than regular String and you are trying to put Integer value.

Easy way out by simple convert:

vars.put("counts", Integer.toString(count));

Another option is save object as is using vars.putObject

vars.putObject("counts", count);

Also move to JSR223 Sampler, per JMeter best practices:

Since JMeter 3.1, we advise switching from BeanShell to JSR223 Test Elements

Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
1

Go for JSR223 PostProcessor and Groovy language as:

  1. Groovy performance is much better comparing to Beanshell
  2. Groovy has built-in JSON support
  3. Groovy provides a lot of enhancements on top of Java SDK

Groovy equivalent of your 7 lines of Beanshell code would be:

vars.put('counts',new groovy.json.JsonSlurper().parse(prev.getResponseData()).options.size() as String)
Dmitri T
  • 159,985
  • 5
  • 83
  • 133