Skip to content

Instantly share code, notes, and snippets.

@asahasrabuddhe
Created March 12, 2019 10:28
Show Gist options
  • Save asahasrabuddhe/ac40b53778f0aba46d0aab9976de7638 to your computer and use it in GitHub Desktop.
Save asahasrabuddhe/ac40b53778f0aba46d0aab9976de7638 to your computer and use it in GitHub Desktop.
Compare and find out differences between two JSON files
// add the following lines inside the dependencies section of your build.gradle file and sync!
// https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
// https://mvnrepository.com/artifact/com.google.guava/guava
compile group: 'com.google.guava', name: 'guava', version: '27.1-jre'
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
public class JSONComparer {
private static void compareJSON(String leftJSON, String rightJSON) {
Gson gson = new Gson();
Type type = new TypeToken<Map<String, Object>>() {
}.getType();
Map<String, Object> leftMap = gson.fromJson(leftJSON, type);
Map<String, Object> rightMap = gson.fromJson(rightJSON, type);
MapDifference<String, Object> difference = Maps.difference(leftMap, rightMap);
System.out.println("Files are " + (difference.areEqual() ? "Identical" : "Distinct"));
if (!difference.areEqual()) {
if (difference.entriesOnlyOnLeft().size() > 0) {
System.out.println("Entries only on the left\n--------------------------");
difference.entriesOnlyOnLeft().forEach((key, value) -> System.out.println(key + ": " + value));
}
if (difference.entriesOnlyOnRight().size() > 0) {
System.out.println("\n\nEntries only on the right\n--------------------------");
difference.entriesOnlyOnRight().forEach((key, value) -> System.out.println(key + ": " + value));
}
if (difference.entriesDiffering().size() > 0) {
System.out.println("\n\nEntries differing\n--------------------------");
difference.entriesDiffering().forEach((key, value) -> System.out.println(key + ": " + value));
}
}
}
private static String readFile(String path, Charset encoding)
throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment