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");