cancel
Showing results for 
Search instead for 
Did you mean: 

Compare 2 JSON responses in soapUIPRO/Groovy

SOLVED
Nmenon81
Occasional Contributor

Compare 2 JSON responses in soapUIPRO/Groovy

Hi,

I have generated REST responses using a datasource which contains 5 different input test data rows that populates the REST request. This test data contains Life insurance policy number, effective date which are the 2 main variables that drive the response values. This REST service quotes the value of the insurance policy as of a ceratin effective date.

I get succussful response on Day 1 and each response has about 15 different tags. On Day 5, I want to use the same test data and re-run my test but this time I want to check  if my Day 1 and Day 5 run has generated the same set of responses for all the 5 different test data. I also want to be able to extract differences if one of the test data does not get the expected result on the Day 5 run.

 

Please help.

 

Thank you.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Nastya_Khovrina
Moderator

Re: Compare 2 JSON responses in soapUIPRO/Groovy

Hi Nmenon81,

 

The following article may give you some tips or ideas: How Do I Compare Two Dynamic JSON Responses?

 



Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

View solution in original post

7 REPLIES 7
Nastya_Khovrina
Moderator

Re: Compare 2 JSON responses in soapUIPRO/Groovy

Hi Nmenon81,

 

The following article may give you some tips or ideas: How Do I Compare Two Dynamic JSON Responses?

 



Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

View solution in original post

Olga_T
Moderator

Re: Compare 2 JSON responses in soapUIPRO/Groovy

Hi all,

 

@Nmenon81, do you have any updates for us? Does the topic that Nastya provided help? 

 


Olga Terentieva
SmartBear Assistant Community Manager

Nmenon81
Occasional Contributor

Re: Compare 2 JSON responses in soapUIPRO/Groovy

Thank you very much for this response. I will try this out.

Can I also use this for comparing my JSON response to my JDBC response?

Nastya_Khovrina
Moderator

Re: Compare 2 JSON responses in soapUIPRO/Groovy

Hi,

 

Yes, you can replace the JSON strings in the provided script with property expansions that point to the responses of a JDBC Test Step with the "Get Data" Tool. Please see the details about the Get Data functionality here: https://support.smartbear.com/readyapi/docs/testing/properties/get-data.html



Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Nmenon81
Occasional Contributor

Re: Compare 2 JSON responses in soapUIPRO/Groovy

Once again thank you for the solution. This works perfectly on JSON-JSON compare but when I set the String response  = JDBC responseasXML then I get a jackson parser error. I am unable to find out where exactly is the issue. What are some of the things I should be looking at to resolve this issue?

 

Error Message - com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'CSP001A': was expecting ('true', 'false' or 'null') at [Source: (String)"CSP001A"; line: 1, column: 15] error at line: 19 

 

 

Nastya_Khovrina
Moderator

Re: Compare 2 JSON responses in soapUIPRO/Groovy

Can you please provide the full script which you use?



Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Nmenon81
Occasional Contributor

Re: Compare 2 JSON responses in soapUIPRO/Groovy

I used the script that was provided to me as an asnwer to my older queries and I used get data to add teh JDBC resultset. The script hat was provided allowed to match to JSON responses and I was trying to match a JSON response with a JDBC resulset.  This is what I have -

 

//def String mongoResponse
def String mongoResponse = context.expand( '${GetBenefitDetail_by Ben Seq#Response}' )
def String response = context.expand( '${JDBC Request#ResponseAsXml}' )
//def String response = context.expand( '${JDBC Request#policy}' )
 


       
mapper = new ObjectMapper()
JsonNode jsonResponse = mapper.readTree(response);
JsonNode mongoDBResponse = mapper.readTree(mongoResponse);


Set<JsonPathValue> pathValues = listLeafValues(jsonResponse, '$');
Configuration configuration = Configuration.builder()
        .jsonProvider(new JacksonJsonNodeJsonProvider())
        .mappingProvider(new JacksonMappingProvider(mapper))
        .build();

pathValues.each {
    try {
        JsonPath jsonPath = JsonPath.compile(it.path);
        JsonNode result = jsonPath.read(mongoDBResponse, configuration);
        if (result != it.value) {
            log.info('Node with path ' + it.path + ' and value ' + it.value + ' cannot be found')
        }
    } catch (Exception e) {
        log.info('Node with path ' + it.path + ' and value ' + it.value + ' cannot be found')
    }
}

Set<JsonPathValue> listLeafValues(JsonNode jsonObject, String path) {
    if (jsonObject.isObject()) {
        return getValuesFrom((ObjectNode) jsonObject, path);
    } else if (jsonObject.isArray()) {
        return getValuesFrom((ArrayNode) jsonObject, path);
    } else {
    }
}

Set<JsonPathValue> getValuesFrom(ObjectNode jsonObject, String path) {
    Set<JsonPathValue> values = new LinkedHashSet<>();
    Iterator<Map.Entry<String, JsonNode>> fields = jsonObject.fields();
    while (fields.hasNext()) {
        Map.Entry<String, JsonNode> field = fields.next();
        JsonNode value = field.getValue();
        String nodePath = path + "." + field.getKey();
        addValue(values, value, nodePath);
    }
    return values;
}

Set<JsonPathValue> getValuesFrom(ArrayNode array, String nodePath) {
    Set<JsonPathValue> values = new LinkedHashSet<>();
    int index = 0;
    for (JsonNode value : array) {
        addValue(values, value, nodePath + "[" + index + "]");
        index++;
    }
    return values;
}

def addValue(Set<JsonPathValue> values, JsonNode value, String nodePath) {
    if (value.isObject()) {
        values.addAll(getValuesFrom((ObjectNode) value, nodePath));
    } else if (value.isArray()) {
        values.addAll(getValuesFrom((ArrayNode) value, nodePath));
    } else if (value.isValueNode()) {
        values.add(new JsonPathValue(nodePath, value));
    }
}

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors