jmeter-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sebb <>
Subject Re: 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 20:08:00 GMT
On 25 November 2014 at 19:43, David Luu <> wrote:
> Hello Felix,
> I had based the starting code off a web blog post on JMeter JSON with
> javascript and it used eval technique. I did consider swapping to
> JSON.parse() later after sending out the email to JMeter mailing list. I
> just switched over now for cleaner code. It didn't help though.
> Found no relevant info/errors in the jmeter.log file. I did put a try/catch
> around the problem and dumped the result to a variable to display with the
> debug sampler. It returned this particular error that isn't very helpful:
> InternalError: Java class "[B" has no public instance field or method named
> "toJSON".
> I wonder if "[B" stood for JMeter regex extractor result variable
> SOMEITEMID since the original variable name started with a B. I just
> renamed it when sending the email. Plus when I don't include this
> variable's value into the JSON object, there is no error caught (works
> fine). Other than that, the error is a bit cryptic on the specifics of
> which Java class.

This is a standard JVM error.

'[' means an array
'B' means either byte or Boolean

So the error means that the object on which you are trying to convert
to JSON is an array and which does not support the method.
You need to convert the array to something which does.

> David
> On Tue, Nov 25, 2014 at 1:01 AM, Felix Schumacher <
>> wrote:
>> 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_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
>>> 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:
>> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message