Array Comparison Strategies for Diff report

This is common situation when response is an array of JSON objects and if we invoke an API and if the API returns the response in different order each time we invoke the API then in such situation diff report will be confusing if we don't provide a unique key on which to differ the array item.

For this purpose, we have provided three strategies to make diff report more meaningful in such cases.

  1. Default
    In default strategy, the diff engine compares the array items based on properties _id or id. If both properties are not present in the array item then index based comparison will be done.

  2. Whole Array Item
    This strategy is helpful if there is only change in the order of array items, then choosing this strategy will show you the movements of array items.
  3. Keys
    This strategy is helpful if your array items are not recognised by _id or id property. In such cases, you may provide unique key property on which to compute the diff report for array response. If your response contains multiple array properties then you will need to provide comma separated properties here. If the specified keys are not found in the array items then default strategy will be applied automatically.

 

Let's see these strategies in action using some example scenarios:

Scenario 1: When array items contains the id, _id property and in the actual response, order of array items changes and content of those array items, also changes.

Suppose we have the following expected body and actual body:

Expected Body
[
    { "id":"ford", "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
    { "id":"bmw", "name":"BMW", "models":[ "320", "X3", "X5" ] },
    { "id":"fiat", "name":"Fiat", "models":[ "500", "Panda" ] }
]
Actual Body
[
    { "id":"bmw", "name":"BMW", "models":[ "320", "X3", "X5", "Z4" ] },
    { "id":"fiat", "name":"Fiat", "models":[ "500", "Panda" ] },
    { "id":"ford", "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] }
]

Now, if we apply the default strategy in the diff report. As the array item contains the id property then default strategy will provide the following meaningful diff report:

And if we apply the Whole array item strategy then the diff report will look like this. Whole array item strategy will show you the whole array item in case there is also a change in the array item content.

And keys strategy will show the same diff report as shown in the case of default strategy as the response array items contains the id property.

Scenario 2: When array items doesn't contain the id, _id property and in the actual response, only order of array items changes.

Suppose we have the following expected body and actual body:

Expected Body
[
    { "carId":"ford", "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
    { "carId":"bmw", "name":"BMW", "models":[ "320", "X3", "X5" ] },
    { "carId":"fiat", "name":"Fiat", "models":[ "500", "Panda" ] }
]
Actual Body
[
    { "carId":"bmw", "name":"BMW", "models":[ "320", "X3", "X5" ] },
    { "carId":"fiat", "name":"Fiat", "models":[ "500", "Panda" ] },
    { "carId":"ford", "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] }
]

Now, if we apply the default strategy in the diff report. As the array item doesn't contain the id or _id property then default strategy will not provide the meaningful diff report as it will compare the array items based on array index:

And if we apply the Whole array item strategy then the diff report will provide meaningful information as there is no change in the content of array items.

And now in this case keys strategy will also show you the meaningful report if we provide the unique key "carId" as input. The diff report will look like this:

Scenario 3: When array items doesn't contain the id, _id property and in the actual response, order of array items changes and content of those array items, also changes.

Suppose we have the following expected body and actual body:

Expected Body
[
    { "carId":"ford", "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
    { "carId":"bmw", "name":"BMW", "models":[ "320", "X3", "X5" ] },
    { "carId":"fiat", "name":"Fiat", "models":[ "500", "Panda" ] }
]
Actual Body
[
    { "carId":"bmw", "name":"BMW", "models":[ "320", "X3", "X5", "Z4" ] },
    { "carId":"fiat", "name":"Fiat", "models":[ "500", "Panda" ] },
    { "carId":"ford", "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] }
]

Now, if we apply the default strategy in the diff report. As the array item doesn't contain the id or _id property then default strategy will not provide the meaningful diff report as shown below:

And if we apply the Whole array item strategy then the diff report will look like this. Whole array item strategy will show you the whole array item in case there is also a change in the array item content.

And now in this case keys strategy will show you the meaningful report if we provide the unique key "carId" as input. The diff report will look like this: