jmeter-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Luu <manga...@gmail.com>
Subject JSON to string in javascript via BSF post processor, etc.? Seem to come across a problem. A bug? Any help or tips?
Date Tue, 25 Nov 2014 03:57:58 GMT
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());
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
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.

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

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message