0
votes

I am trying to execute below JSON code from Jmeter BeanShell Post processor but I am getting an error "Typed variable declaration : Class: JSONObject not found in namespace"

We are using JMeter 5.3 version and JSON jar files are available in the lib and lib\ext

import java.util.*;
import org.json.JSONObject;
import com.jayway.jsonpath.Criteria;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.JsonPath;


JSONObject cartItemsObj = (JSONObject) cartItems.get("C_cartItems");

log.info(cartItemsObj);

Error details:

2021-03-03 20:55:19,967 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.util.*; import com.jayway.jsonpath.Criteria; import com.jayway.json . . . '' : Typed variable declaration : Class: JSONObject not found in namespace

1
Try removing import java.util.*;user7294900

1 Answers

0
votes
  1. In order to be able to use JSONObject class you need to have org.json.json library in JMeter Classpath

  2. I fail to see where and how do you get cartItems object

  3. Since JMeter 3.1 you are supposed to use JSR223 Test Elements and Groovy language for scripting

  4. Groovy has built-in JSON support so if you want to get C_cartItems attribute value from the response (assuming that it exists on top level of the JSON you're getting from the server) it can be done as simple as:

    cartItemsObj = new groovy.json.JsonSlurper().parse(prev.getResponseData()).C_cartItems
    
    log.info(cartItemsObj)
    

    Demo:

    enter image description here

More information: