0

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"}
spn161
  • 47
  • 1
  • 4
  • The given output is correct. If you want custom behaviour/output then you need to write custom code, or rather than doing a top-level difference call you need to further process the inner JSON elements rather than comparing top level elements. You might also find the answers here helpful: [How to get the difference between two maps Java?](https://stackoverflow.com/questions/38015282/how-to-get-the-difference-between-two-maps-java) – sorifiend Jul 27 '22 at 04:40
  • How do I manipulate the output of the entriesDiffering to display only differences of one map instead of both? – spn161 Jul 27 '22 at 07:01

1 Answers1

0

https://github.com/octomix/josson

https://mvnrepository.com/artifact/com.octomix.josson/josson

This method subtract left JSON from right JSON. It compares the whole object inside an array, instead of comparing individual object value inside an array.

implementation 'com.octomix.josson:josson:1.3.21'

------------------------------------------------


Jossons jossons = new Jossons();
jossons.putDataset("left", Josson.fromJsonString("{\n" +
        "\"package\": {\n" +
        "  \"institution\": [\n" +
        "      {\n" +
        "        \"school\": \"TP\"\n" +
        "      }\n" +
        "    ],\n" +
        "    \"people\": [\n" +
        "      {\n" +
        "        \"age\": 32,\n" +
        "        \"name\": \"Bob\"\n" +
        "      },\n" +
        "      {\n" +
        "        \"age\": 16,\n" +
        "        \"name\": \"Amanda\"\n" +
        "      }\n" +
        "    ],\n" +
        "    \"details\": [\n" +
        "      {\n" +
        "        \"course\": \"Humanities\",\n" +
        "        \"duration\": 4,\n" +
        "        \"description\": \"Students in Computer Sci\"\n" +
        "      }\n" +
        "    ]\n" +
        "  }\n" +
        "}"));
jossons.putDataset("right", Josson.fromJsonString("{\n" +
        "\"package\": {\n" +
        "  \"institution\": [\n" +
        "      {\n" +
        "        \"school\": \"MIT\"\n" +
        "      }\n" +
        "    ],\n" +
        "    \"people\": [\n" +
        "      {\n" +
        "        \"age\": 32,\n" +
        "        \"name\": \"Bob\"\n" +
        "      },\n" +
        "      {\n" +
        "        \"age\": 16,\n" +
        "        \"name\": \"Samantha\"\n" +
        "      },\n" +
        "      {\n" +
        "        \"age\": 20,\n" +
        "        \"name\": \"Dylan\"\n" +
        "      }\n" +
        "    ],\n" +
        "    \"details\": [\n" +
        "      {\n" +
        "        \"course\": \"Computer Sci\",\n" +
        "        \"duration\": 4,\n" +
        "        \"description\": \"Students in Computer Sci\"\n" +
        "      }\n" +
        "    ]\n" +
        "  }\n" +
        "}"));
JsonNode result = jossons.evaluateQuery("left->package >-> right->package");
System.out.println(result == null ? null : result.toPrettyString());

Output

{
  "institution" : [ {
    "school" : "MIT"
  } ],
  "people" : [ {
    "age" : 16,
    "name" : "Samantha"
  }, {
    "age" : 20,
    "name" : "Dylan"
  } ],
  "details" : [ {
    "course" : "Computer Sci",
    "duration" : 4,
    "description" : "Students in Computer Sci"
  } ]
}
Raymond Choi
  • 1,065
  • 2
  • 7
  • 8