I am trying to compare two dynamic json data and if they are not equal then i am printing the differences. For this i am using
Type mapType = new TypeToken<Map<String, Object>>(){}.getType();
Map<String,Object> firstMap = g.fromJson(jsonElement1, mapType);
Map<String, Object> secondMap = g.fromJson(jsonElement2, mapType);
System.out.println(Maps.difference(firstMap, secondMap));
I need to display the difference in the console. This is the java code in which i am trying to display the differences of the json
public class Tester {
public static ArrayList<Object> ls1 = new ArrayList<Object>();
public static ArrayList<Object> ls2 = new ArrayList<Object>();
public static void main(String[] args) throws Exception {
JsonParser parser = new JsonParser();
try{
Gson g = new Gson();
JsonElement jsonElement1 = parser
.parse(new FileReader("D:\\file1.json"));
JsonElement jsonElement2 = parser
.parse(new FileReader("D:\\file2.json"));
System.out.println("Is the two JSON File Same: "+compareJson(jsonElement1, jsonElement2));
if(!compareJson(jsonElement1, jsonElement2)){
Type mapType = new TypeToken<Map<String, Object>>(){}.getType();
Map<String,Object> firstMap = g.fromJson(jsonElement1, mapType);
Map<String, Object> secondMap = g.fromJson(jsonElement2, mapType);
System.out.println(Maps.difference(firstMap, secondMap));
}
else{
System.out.println("The Two JSON Are SAME!!!!!!!!!!!!!!!");
}
}catch(Exception e1){
e1.printStackTrace();
}
}
public static boolean compareJson(JsonElement json1, JsonElement json2) {
boolean isEqual = true;
// Check whether both jsonElement are not null
if (json1 != null && json2 != null) {
// Check whether both jsonElement are objects
if (json1.isJsonObject() && json2.isJsonObject()) {
Set<Entry<String, JsonElement>> ens1 = ((JsonObject) json1).entrySet();
Set<Entry<String, JsonElement>> ens2 = ((JsonObject) json2).entrySet();
JsonObject json2obj = (JsonObject) json2;
if (ens1 != null && ens2 != null && (ens2.size() == ens1.size())) {
// Iterate JSON Elements with Key values
for (Entry<String, JsonElement> en : ens1) {
isEqual = isEqual && compareJson(en.getValue(),json2obj.get(en.getKey()));
}
} else {
return false;
}
}
// Check whether both jsonElement are arrays
else if (json1.isJsonArray() && json2.isJsonArray()) {
JsonArray jarr1 = json1.getAsJsonArray();
JsonArray jarr2 = json2.getAsJsonArray();
if (jarr1.size() != jarr2.size()) {
return false;
} else {
int i = 0;
// Iterate JSON Array to JSON Elements
for (JsonElement je : jarr1) {
isEqual = isEqual && compareJson(je, jarr2.get(i));
i++;
}
if (isEqual) {
Object[] o1 = ls1.toArray();
Object[] o2 = ls2.toArray();
isEqual = Arrays.deepEquals(o1, o2);
}
}
}
// Check whether both jsonElement are null
else if (json1.isJsonNull() && json2.isJsonNull()) {
return true;
}
// Check whether both jsonElement are primitives
else if (json1.isJsonPrimitive() && json2.isJsonPrimitive()) {
ls1.add(json1);
ls2.add(json2);
}
} else if (json1 == null && json2 == null) {
return true;
} else {
return false;
}
return isEqual;
}
}
the object array
if (isEqual) {
Object[] o1 = ls1.toArray();
Object[] o2 = ls2.toArray();
isEqual = Arrays.deepEquals(o1, o2);
}
the object array o1 and o2 are null dont know why it is null.
These are the two sample json i am using for testing json1 {"Company List":["Compnay: Paypal","Compnay: eBay","Compnay: Google"]}
json2 {"Company List":["Compnay: Viswesh","Compnay: Anand","Compnay: Anand"]}
in both the json the value for company is different so the code has to return false but the code return true