0

In my jmeter test, the test receives the following json response.

{"result":"success","additional-info":"{\"external-profile\":{\"email\":\"myemail@gmail.com\",\"firstname\":\"fn\",\"lastname\":\"ln\",\"portfolio\":{\"tags-of-interest\":[],\"question-created-tags\":[{\"tag\":\"un2-new tag-empty\",\"count\":1},{\"tag\":\"un2-new tag2-empty\",\"count\":1}],\"question-answered-tags\":[]}}}"}

I want to check that the message has path additional-info.exernal-profile.portfolio and that there are keys tags-of-interest, question-created-tag,question-answered-tag

I am using json jmes path but I am getting error Assertion failure message:Invalid argument type calling "keys": expected object but was null

What am I doing wrong?

enter image description here

Manu Chadha
  • 15,555
  • 19
  • 91
  • 184

2 Answers2

0

The problem was that additional-info was a string and not an object. I used minimal-json-0.9.5 and beanshell post processor scripting to convert the strings into json objects.

Beanshell code

import com.eclipsesource.json.*;

//prev.setSuccessful(false);
try {
    String jsonString = prev.getResponseDataAsString(); //response as string
    log.info("received json string: "+jsonString);
    
    JsonObject responseAsJsonObject = JsonObject.readFrom(jsonString); //convert response string as json
    log.info("converted to object: "+responseAsJsonObject);
    
    String additionalInfoString = responseAsJsonObject.get("additional-info").asString(); //get additional info string from json object
    log.info("additional info as string: "+additionalInfoString);
    
    JsonObject additionalInfoJsonObject = JsonObject.readFrom(additionalInfoString); //convert additional info string to json
    log.info("additional info as object: "+additionalInfoJsonObject);
    
    JsonObject externalProfileJsonObject = additionalInfoJsonObject.get("external-profile").asObject(); //get external profile object
    log.info("external profile as object: "+externalProfileJsonObject);
    
    JsonObject portfolioJsonObject = externalProfileJsonObject.get("portfolio").asObject(); //get portfolio object
    log.info("portfolio as object: "+portfolioJsonObject);
    
    JsonArray tagsOfInterest = portfolioJsonObject.get("tags-of-interest").asArray();
    log.info("tags of interest: "+tagsOfInterest);
    
    JsonArray tagsCreated = portfolioJsonObject.get("question-created-tags").asArray();
    log.info("tags created: "+tagsCreated);
    
    JsonArray tagsAnswered = portfolioJsonObject.get("question-answered-tags").asArray();
    log.info("tags tagsAnswered: "+tagsAnswered);
//  prev.setSuccessful(true);
} catch (Exception e){
    log.info("error in processing beanshell script: ", e);
    prev.setSuccessful(false);
}

enter image description here

Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
0

Just go for 2 JSON JMESPath Extractors:

  1. Extract the content of additional-info attribute into a JMeter Variable from the response

    enter image description here

  2. Extract the attributes from the external-profile attribute:

    enter image description here

You can see the extracted values using Debug Sampler and View Results Tree listener combination:

enter image description here


Be aware that since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting as:

More information: Apache Groovy - Why and How You Should Use It

Dmitri T
  • 159,985
  • 5
  • 83
  • 133