Does anybody know how to compare two JSON files in java and show their differences? I have tried using guava by following this example: How to compare JSON documents and return the differences with Jackson or Gson?
Do note that the naming of arrays in the JSON like "institution", "people" and "details" are not fixed.
The results of this attempt produce these:
ObjectMapper mapper = new ObjectMapper();
TypeReference<HashMap<String, Object>> type =
new TypeReference<HashMap<String, Object>>() {};
Map<String, Object> leftMap = mapper.readValue(leftJson, type);
Map<String, Object> rightMap = mapper.readValue(rightJson, type);
MapDifference<String, Object> difference = Maps.difference(leftMap, rightMap);
System.out.println(difference.entriesDiffering());
Output
{institution=([{school=TP}], [{school=MIT}]), details=([{duration=4, course=Humanities, description=Students in Computer Sci}], [{duration=4, course=Computer Sci, description=Students in Computer Sci}]), people=([{name=Bob, age=32}, {name=Amanda, age=16}], [{name=Bob, age=32}, {name=Samantha, age=16}, {name=Dylan, age=20}])}
Left JSON
{
"package": {
"institution": [
{
"school": "TP"
}
],
"people": [
{
"age": 32,
"name": "Bob"
},
{
"age": 16,
"name": "Amanda"
}
],
"details": [
{
"course": "Humanities",
"duration": 4,
"description": "Students in Computer Sci"
}
]
}
}
Right JSON
{
"package": {
"institution": [
{
"school": "MIT"
}
],
"people": [
{
"age": 32,
"name": "Bob"
},
{
"age": 16,
"name": "Samantha"
},
{
"age": 20,
"name": "Dylan"
}
],
"details": [
{
"course": "Computer Sci",
"duration": 4,
"description": "Students in Computer Sci"
}
]
}
}
Expected Output
Differences
institution = {"school": "MIT"}
people = {"age": 20, "name": "Dylan"}, {"age": 16, "name": "Samantha"}
details = {"course": "Computer Sci"}