apache / jmeter

Apache JMeter open-source load testing tool for analyzing and measuring the performance of a variety of services
https://jmeter.apache.org/
Apache License 2.0
8.27k stars 2.09k forks source link

Not being able to use json-smart in BeanShell #4837

Closed asfimport closed 6 years ago

asfimport commented 6 years ago

ricardogaspar2 (Bug 62604): I'm using JMeter 4.0r1823414

I'm trying to use the library json-smart that resides in lib folder of jmeter inside a BeanShell script but without success.

I'm already using JSON Extractor just to extract some JSONs, but now I need to manipulate them; that's why I I'm using BeanShell.

I'm getting the following errors:

2018-08-07 21:38:03,549 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: import net.minidev.json.JSONObject; import net.minidev.json.parser.JSONParser; . . . '' : Typed variable declaration 2018-08-07 21:38:03,549 WARN o.a.j.e.BeanShellPostProcessor: Problem in BeanShell script: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of:import net.minidev.json.JSONObject; import net.minidev.json.parser.JSONParser; . . . '' : Typed variable declaration

And this is my BeanShell Script:


import net.minidev.json.JSONObject; import net.minidev.json.parser.JSONParser;

// formats JSON variables houseDataJson; entityTypeGroupsJson // takes the variable with the JSON for entityTypeGroups and adds the prefix "entityTypeGroups":{ VARIABLE WITH JSON CODE HERE}

String SCRIPT_NAME = "[FormatJsonVariables]";

// names of the variables retrieved from JSON Extractor from get_house_data request String PKID_VAR_NAME = "pkid"; String HOUSE_DATA_VAR_NAME = "houseDataJson"; String ENTITY_TYPE_GROUPS_VAR_NAME = "entityTypeGroupsJson";

log.info(SCRIPT_NAME+ " checking id variables " + HOUSE_DATA_VAR_NAME +" and " + ENTITY_TYPE_GROUPS_VAR_NAME + " are present");

String currentPkid = vars.get(PKID_VAR_NAME); JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE);

JSONObject currentHouseDataJson = (JSONObject) parser.parse(vars.get(HOUSE_DATA_VAR_NAME)); JSONObject currentEntityTypeGroupsJson = (JSONObject) parser.parse(vars.get(ENTITY_TYPE_GROUPS_VAR_NAME));

log.info(SCRIPT_NAME + " current variable values:\n-currentPkid: " + currentPkid + "\ncurrentHouseDataJson: "+ currentHouseDataJson +"\ncurrentEntityTypeGroupsJson: " +currentEntityTypeGroupsJson)


Created attachment aca_update_answers.jmx: jmx file

Severity: major OS: Linux

asfimport commented 6 years ago

@FSchumacher (migrated from Bugzilla): Please ask user questions on the users mailing list before opening a bug.

You gave a pretty detailed description of your problem, but cut the error messaage short before the real clue was probably shown. When I tested the jmx file, it barfed on wrong input data. Be sure to give the correct JSON representation to the parser when you expect an JSON Object (instead of say a JSON Array).

Note that you could switch to groovy instead of beanshell. It is more modern and gives nicer error messages.