Forum Discussion

TSRAO's avatar
TSRAO
Occasional Contributor
2 years ago

How to compare 2 Json content from 2 different text files and return only the differences

Hi,

 

I found the way to save the Json response to a text file and compare both the file contents (the 2 Json responses saved from previous run and current run) by using the Groovy script. But for now, I have hardcoded the file location for both the files in the code. I am looking for a solution to remove that hard coded file path.

 

Can anyone have suggestions for me! Thanks in-advance!

 

Here is the Groovy Script what I am using,

 

import groovy.json.JsonSlurper;
import groovy.json.JsonOutput;

 

log.info("Start");

 

def previousResponse = new File('\PreviousResponse.txt').text; // Hard coded file path from my local drive, which I want to avoid
def currentResponse = new File('\CurrentResponse.txt').text; // Hard coded file path from my local drive, which I want to avoid

 

//JsonSlurper parses the text or reader content into a data structure of lists and maps
def slurperA = new JsonSlurper().parseText(previousResponse)
def slurperB = new JsonSlurper().parseText(currentResponse);

 

//Creating empty ArrayList
def listA = [];
def listB = [];

 

slurperA.each { key, value ->
loop(listA, key, value)
}

slurperB.each { key, value ->
loop(listB, key, value)
}

 

//Logs this on the Console
log.info("matches: " + listA.intersect(listB)); //logs the comman fields from both the Jsons
log.info("mismatches A: " + listA.minus(listB));
log.info("mismatches B: " + listB.minus(listA));

 

//Creating a Global property on ReadyAPI, to Set the Json Response difference to it
def diff_Current_Response = listB.minus(listA);
def jsonDiff = JsonOutput.toJson(diff_Current_Response);
def json_beauty = JsonOutput.prettyPrint(jsonDiff);
com.eviware.soapui.SoapUI.globalProperties.setPropertyValue("configDiff", json_beauty);

 

//User defined method for looping through each and every field in the Json response
def loop(list, key, value) {
if(value.class == null) {
if(value != null && !value.isEmpty()) {
value.each { k, v ->
loop(list, (key + '.' + k), v);
}
} else {
list.add((key));
}
} else if(value.class == ArrayList) {
value.each {
loop(list, key, it);
}
} else {
list.add(key + " = " + value);
}
}

 

//Groovy "Script Assertion"
def previousJsonResponse = new JsonSlurper().parseText(previousResponse);
def currentJsonResponse = new JsonSlurper().parseText(currentResponse);
assert currentJsonResponse == previousJsonResponse;

 

log.info("Done");

5 Replies

  • KarelHusa's avatar
    KarelHusa
    Champion Level 3

    TSRAO,

    what is the intended source of the JSON messages? Do you want to compare the responses of two test steps?

     

    There are different options for how to do that. You can, e.g., save the responses in ReadyAPI properties and then access the property values from the Groovy script.

     

  • TSRAO's avatar
    TSRAO
    Occasional Contributor

    Hi KarelHusa, thanks for your response.

     

    The requirement is like this,

    1. 1. Today I ran the test case(REST API) and got the response let's say A
    2. 2. Tomorrow if I run the same test case, I may get the same response or a different response if there any changes to our config file, let's say that is B

    Now, I have two responses and I need to compare both the responses A and (both A and B are Json responses), to make sure that there are no changes OR if there are any changes, what are they( I mean where exactly the changes are additions/deletions/modification to any values in the response etc.,).

     

    So, I am sending the responses to a text file, and in my Groovy script I am hard coding the two file paths(Previous response and Current responses) in order to read the content from those two files. I want to avoid hard coded file paths from my Groovy script. Of course I am also trying to see if there are any other alternatives to do that comparison and return the differences from both the responses, to make it simple.

     

    Thanks again!

    • KarelHusa's avatar
      KarelHusa
      Champion Level 3

      TSRAO,

      in such a case, you can save your reponses with the date in the filename (e.g., response-20221211.json).

       

      Then, in the compare script just refer to yesterday date, i.e. response-20221211.json.

       

      • TSRAO's avatar
        TSRAO
        Occasional Contributor

        Thanks KarelHusa!

         

        Not sure how to write the Groovy Script for generating the file with Date/TimeStamp in the file name, can you please suggest me!