jmeter-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Deepak Shetty <shet...@gmail.com>
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 Wed, 26 Nov 2014 20:54:31 GMT
>Using the regex extracted value directly will be set as a byte-array,
which is no object and thus has no toString method.
>jsonResponse.someItemId = "" + vars.get("SOMEITEMID");
No the regex does return a java.lang.String  (which you can verify by doing
a getClass().getName())
I believe whats happening is vars.get("SOMEITEMID") is a java.lang.String
(represented within Rhino ) and the left hand side of the assignment
expects a javascript string (and for some reason it isnt working
automatically)
Concatenating to a javascript string forces the end result to be a Rhino
String and you could achieve the same thing using
jsonResponse.someItemId = String(vars.get("SOMEITEMID"));


On Wed, Nov 26, 2014 at 12:15 AM, Felix Schumacher <
felix.schumacher@internetallee.de> wrote:

> Am 26.11.2014 03:07, schrieb David Luu:
>
>  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.
>>>
>>
>> The involved code snippets as Deepak requested are:
>>
>> //these 2 lines together in this order will cause the 2nd line to fail
>> with
>> the mentioned standard JVM error
>> jsonResponse.someItemId = vars.get("SOMEITEMID");
>> vars.put("JSON_OBJ_AS_STR", JSON.stringify(jsonResponse));
>>
>> //doing this (after 1st line above) however has no problem, debug sampler
>> shows the assignment to testVar is fine (see below)
>> vars.put("testVar", jsonResponse.someItemId);
>>
>> so appending SOMEITEMID into JSON object in the form of a new
>> member/property of object and then pulling it back out to another var is
>> no
>> problem. Just a problem when you try to stringify the whole JSON with this
>> included member/property.
>>
>> and if not clarified earlier, SOMEITEMID is a regular expression extractor
>> (post processor) result variable. Defined as follows:
>>
>> reference name: SOMEITEMID
>> regular expression: someText someMoreText".+id="(\d)+"
>> template: $1$
>> match #: 1
>> default value: notFound
>>
>> with actual match results/var generated as follows as shown in the debug
>> sampler
>>
>> SOMEITEMID =14179242
>> SOMEITEMID_g=1
>> SOMEITEMID_g0=someText someMoreText" id="14179242"
>> SOMEITEMID_g1=14179242
>> testVar = 14179242
>>
>> so my question then is, how do we force/cast SOMEITEMID or SOMEITEMID_g1
>> as
>> a string and not an array within javascript? It would seem that
>> calling vars.get("SOMEITEMID")
>> still keeps a representation of the data as an array, although when stored
>> to another variable it is ok as string but not when you try to do
>> JSON.stringify() with it as part of the data. I speculate this is the
>> issue
>> because omitting SOMEITEMID in the JSON object, the stringify operation is
>> successful.
>>
>> So to summarize in a nutshell, it seems like w/o special handling, regular
>> expression extractor result variable value can't be added to a JSON object
>> in javascript BSF postprocessor/etc to then be stringified.
>>
> I get the same result with a simple test script. Using the regex extracted
> value
> directly will be set as a byte-array, which is no object and thus has no
> toString
> method. (I have attached the simple test case, hopefully it gets through).
>
> You can workaround this by adding an empty string to the regex extracted
> value:
>
> jsonResponse.someItemId = "" + vars.get("SOMEITEMID");
>
> That helped my test case.
>
> Regards
>  Felix
>
>
>> I wonder then whether one would run into similar problem in Java via
>> Beanshell, etc. I will look into that next.
>>
>>
>> On Tue, Nov 25, 2014 at 12:08 PM, sebb <sebbaz@gmail.com> wrote:
>>
>>  On 25 November 2014 at 19:43, David Luu <mangaroo@gmail.com> 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 <
>>> > felix.schumacher@internetallee.de> 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_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: user-unsubscribe@jmeter.apache.org
>>> >> For additional commands, e-mail: user-help@jmeter.apache.org
>>> >>
>>> >>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
>>> For additional commands, e-mail: user-help@jmeter.apache.org
>>>
>>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
> For additional commands, e-mail: user-help@jmeter.apache.org
>

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