Hello David,

Am 25.11.2014 04:57, schrieb David Luu:
I was wondering if anyone has dealt with JSON data and the need to
stringify it at some point and doing this in JMeter, say with BSF sampler
for javascript?

Any tips on that would be appreciated.

I gave it a try and noticed that JMeter, at least as of version 2.9
r1437961 that I'm using, seems to support JSON.stringify(), or using it
doesn't cause any errors.

However, in one situation, it fails to work. I can't share the full test
plan but here's a snippet around the issue:

//after HTTP sampler, we process JSON response in BSF post processor in
javascript
eval('var jsonResponse = ' + prev.getResponseDataAsString());
if you are using the JSON object (below with stringify) anyway, why not use
 var jsonResponse = JSON.parse(prev.getResponseDataAsString());
instead of eval(...)?

jsonResponse.cacheOnly = false;
jsonResponse.someItemId = vars.get("SOMEITEMID");
//...some other stuff dealing with updating JSON object member values
vars.put("testVar", jsonResponse.someItemId);
vars.put("JSON_OBJ_AS_STR", JSON.stringify(jsonResponse));

I'm reusing JSON_OBJ_AS_STR or can be a new JMeter variable.

If I set JSON object member someItemId, then the stringify fails (without
any complain from JMeter other than test failure at some point). Using
Have you looked in jmeter.log?

debug sampler after this, I notice that JSON_OBJ_AS_STR isn't updated as expected (using old value) or the new variable isn't defined/set, although testVar is defined correctly. If I omit defining new member "someItemId"
and setting it's value, then the stringify works fine. But I need both
things together.
You could try to enclose the javascript code in a try/catch block and log
the possibly catched exceptions.

HTH
 Felix

SOMEITEMID is actually a JMeter variable set by a Regular Expression
Extractor that is set to match a single match group in parenthesis in the
regex.

SOMEITEMID =14179242
SOMEITEMID_g=1
SOMEITEMID_g0=someText someMoreText" id="14179242"
SOMEITEMID_g1=14179242

I also tried using SOMEITEMID_g1 instead of base variable, didn't make a
difference.

Is the problem to do with regular expression matched variable and
JSON.stringify used together?

I guess I could look for alternate javascript solution, or worst case swap
to try doing it in Java like

http://theworkaholic.blogspot.com/2012/05/json-in-jmeter.html

it's just that javascript is simpler to deal with if I can. I don't suppose
a newer version of Jmeter fixes this issue...

I could look into JSONPath related route, but as I'm dealing with multiple
updates to JSON object data, it seems easier to do in code whether
javascript, Java, etc.

Thanks for reading. Any feedback appreciated,
David

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to