I am using this script to update values in JSON file
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.apache.commons.io.FileUtils;
try {
String filePath = "blob";
File file = new File(filePath);
String jsonContent = FileUtils.readFileToString(file, "UTF-8");
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(jsonContent);
String extractedValue1 = vars.get("CASE_TYPE_ID");
jsonObject.get("caseType").put("id",extractedValue1);
String extractedValue2 = vars.get("CASE_CONFIG_ID");
jsonObject.put("caseConfigId", extractedValue2)
FileUtils.writeStringToFile(file, jsonObject.toJSONString(), "UTF-8");
} catch (Exception e) {
log.error("Error updating JSON file: " + e.getMessage());
}
When I run the script I got this error:
In file: inline evaluation of: ``import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; imp . . . '' Encountered "FileUtils" at line 20, column 5.
How to resolve this?
There is a syntax error in your code, line 18 must end with a semicolon.
Apart from this:
Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting so consider migrating to Groovy. More information: Beanshell vs. JSR223 vs. Java For JMeter: Complete Showdown
Groovy has built-in JSON support so you don't need to use 3rd-party libraries. Example code you can use instead:
If you run your test with > 1 user they will be concurrently writing into the same file (race condition) which will result in data corruption or loss. Consider using different file names or if you cannot - use different folders, one per user.