0

There are 2 json, there multiple paths which is needed to be applied on both json. Then these jsons should be compared via some assert in test. The ide solution whould be:

Stirng actualJson - ....
Stirng expectedJson - ....
Stirng actualJsonFiltered - filter(actualJson, lisofJsonPath);
Stirng expectedJsonFiltered - filter(expectedJson, lisofJsonPath);

JSONAssert.assertEquals(actualJsonFiltered, expectedJsonFiltered);

What is not a solutiom: 1. Any code like that:

for(String p: lisofJsonPath) {
    assertEquals(JsonPath.read(actualJson, p), JsonPath.read(expectedJson , p);
}

It is needed to filter jsons and show/fail with all existing path. Show error that several paths are not matches. Not one by one json path matching.

  1. org.assertj.core.api.SoftAssertions - also is not a solution because it is hard to understand from message what assetons are failed.

  2. harcrest allOf/anyOf also is not a solution because it fails on first matched asseretion not combinig them.

P.S. Any suggestions for json diff visualizations are wellcome. The ideal way looks like this. (but this is in javascript)

Cherry
  • 31,309
  • 66
  • 224
  • 364

1 Answers1

0

First, accept the following:

  1. JSON is an unordered collection of key-value pairs.
  2. It is hard to compare an unordered collection of anything.
  3. If you order the key-value pairs (sorted by key) and you represent the JSON value as a String (for example: "{\"keyA\":\"blam\",\"keyB\":\"kapow\"}") you can compare the JSON values using a string comparison.

I will call the sorted form of JSON "cannonical JSON form".

Once you accept the above as true and/or real, this becomes the solution to your problem:

  1. Create a tool (perhaps a single class) that accepts JSON and outputs the cannonical JSON form (this is the non-trivial portion of this solution):
  2. Get the canonical JSON form for both of your JSON values.
  3. Compare the cannonical JSON forms using assertEquals.
DwB
  • 37,124
  • 11
  • 56
  • 82