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 <[email protected]> wrote:
On 25 November 2014 at 19:43, David Luu <[email protected]> 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 <
> [email protected]> 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: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.5" jmeter="2.10 r1533061">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Testplan"
enabled="true">
<stringProp name="TestPlan.comments"></stringProp>
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<elementProp name="TestPlan.user_defined_variables"
elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments"
testname="Benutzer definierte Variablen" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup"
testname="Thread-Gruppe" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller"
elementType="LoopController" guiclass="LoopControlPanel"
testclass="LoopController" testname="Schleifen-Controller (Loop Controller)"
enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">1</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">1</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<longProp name="ThreadGroup.start_time">1416473075000</longProp>
<longProp name="ThreadGroup.end_time">1416473075000</longProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
</ThreadGroup>
<hashTree>
<JavaSampler guiclass="JavaTestSamplerGui" testclass="JavaSampler"
testname="hello world json" enabled="true">
<elementProp name="arguments" elementType="Arguments"
guiclass="ArgumentsPanel" testclass="Arguments" enabled="true">
<collectionProp name="Arguments.arguments">
<elementProp name="Sleep_Time" elementType="Argument">
<stringProp name="Argument.name">Sleep_Time</stringProp>
<stringProp name="Argument.value">100</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="Sleep_Mask" elementType="Argument">
<stringProp name="Argument.name">Sleep_Mask</stringProp>
<stringProp name="Argument.value">0xFF</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="Label" elementType="Argument">
<stringProp name="Argument.name">Label</stringProp>
<stringProp name="Argument.value"></stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="ResponseCode" elementType="Argument">
<stringProp name="Argument.name">ResponseCode</stringProp>
<stringProp name="Argument.value"></stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="ResponseMessage" elementType="Argument">
<stringProp name="Argument.name">ResponseMessage</stringProp>
<stringProp name="Argument.value"></stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="Status" elementType="Argument">
<stringProp name="Argument.name">Status</stringProp>
<stringProp name="Argument.value">OK</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="SamplerData" elementType="Argument">
<stringProp name="Argument.name">SamplerData</stringProp>
<stringProp name="Argument.value"></stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="ResultData" elementType="Argument">
<stringProp name="Argument.name">ResultData</stringProp>
<stringProp name="Argument.value">{"hello":
"world"}</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
</collectionProp>
</elementProp>
<stringProp
name="classname">org.apache.jmeter.protocol.java.test.JavaTest</stringProp>
</JavaSampler>
<hashTree>
<BSFPostProcessor guiclass="TestBeanGUI" testclass="BSFPostProcessor"
testname="BSF PostProcessor" enabled="true">
<stringProp name="filename"></stringProp>
<stringProp name="parameters"></stringProp>
<stringProp name="script">var json =
JSON.parse(prev.getResponseDataAsString());
vars.putObject("json", json);
vars.put("json_string_without_regex",
JSON.stringify(json));</stringProp>
<stringProp name="scriptLanguage">javascript</stringProp>
</BSFPostProcessor>
<hashTree/>
</hashTree>
<JavaSampler guiclass="JavaTestSamplerGui" testclass="JavaSampler"
testname="extract 42 and add to json" enabled="true">
<elementProp name="arguments" elementType="Arguments"
guiclass="ArgumentsPanel" testclass="Arguments" enabled="true">
<collectionProp name="Arguments.arguments">
<elementProp name="Sleep_Time" elementType="Argument">
<stringProp name="Argument.name">Sleep_Time</stringProp>
<stringProp name="Argument.value">100</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="Sleep_Mask" elementType="Argument">
<stringProp name="Argument.name">Sleep_Mask</stringProp>
<stringProp name="Argument.value">0xFF</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="Label" elementType="Argument">
<stringProp name="Argument.name">Label</stringProp>
<stringProp name="Argument.value"></stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="ResponseCode" elementType="Argument">
<stringProp name="Argument.name">ResponseCode</stringProp>
<stringProp name="Argument.value"></stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="ResponseMessage" elementType="Argument">
<stringProp name="Argument.name">ResponseMessage</stringProp>
<stringProp name="Argument.value"></stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="Status" elementType="Argument">
<stringProp name="Argument.name">Status</stringProp>
<stringProp name="Argument.value">OK</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="SamplerData" elementType="Argument">
<stringProp name="Argument.name">SamplerData</stringProp>
<stringProp name="Argument.value"></stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="ResultData" elementType="Argument">
<stringProp name="Argument.name">ResultData</stringProp>
<stringProp name="Argument.value">~42~2~3~4~</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
</collectionProp>
</elementProp>
<stringProp
name="classname">org.apache.jmeter.protocol.java.test.JavaTest</stringProp>
</JavaSampler>
<hashTree>
<RegexExtractor guiclass="RegexExtractorGui"
testclass="RegexExtractor" testname="Regular Expression Extractor"
enabled="true">
<stringProp name="RegexExtractor.useHeaders">false</stringProp>
<stringProp name="RegexExtractor.refname">digit</stringProp>
<stringProp name="RegexExtractor.regex">(\d+)</stringProp>
<stringProp name="RegexExtractor.template">$1$</stringProp>
<stringProp name="RegexExtractor.default">NOT_FOUND</stringProp>
<stringProp name="RegexExtractor.match_number">1</stringProp>
</RegexExtractor>
<hashTree/>
<BSFPostProcessor guiclass="TestBeanGUI" testclass="BSFPostProcessor"
testname="BSF PostProcessor" enabled="true">
<stringProp name="scriptLanguage">javascript</stringProp>
<stringProp name="parameters"></stringProp>
<stringProp name="filename"></stringProp>
<stringProp name="script">try {
var json = vars.getObject("json");
var digit = vars.get("digit");
log.info(digit);
log.info("digit: " + digit);
log.info("json-object:" + json);
json['id'] = "" + digit;
log.info("json-object:" + json.id);
vars.put("json_string", JSON.stringify(json));
log.info("json_string: " + vars.get("json_string"));
} catch (e) {
vars.put("json_string", "OH NOES");
log.error("Error adding id from regex:" + e);
}
</stringProp>
</BSFPostProcessor>
<hashTree/>
</hashTree>
<DebugSampler guiclass="TestBeanGUI" testclass="DebugSampler"
testname="Debug Sampler" enabled="true">
<boolProp name="displayJMeterProperties">false</boolProp>
<boolProp name="displayJMeterVariables">true</boolProp>
<boolProp name="displaySystemProperties">false</boolProp>
</DebugSampler>
<hashTree/>
<ResultCollector guiclass="ViewResultsFullVisualizer"
testclass="ResultCollector" testname="View Results Tree" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<name>saveConfig</name>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>false</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]