2

I need to compare two json strings by ignore some fields

I am currently using JSONAssert from org.SkyScreamer to do the comparison but does not ignore any attributes.

Json 1 :

{
  "contributions": [
    [
      {
        "order" : 1,
        "contributorId" : "1980"
      }
    ]
  ]
}

Json 2 :

{
  "contributions": [
    [
      {
        "order": 1,
        "contributorId" : "5789"
      }
    ]
  ]
}
ArrayValueMatcher<Object> arrValMatch1 = new ArrayValueMatcher<>(new CustomComparator(
                JSONCompareMode.NON_EXTENSIBLE,
                new Customization("contributions[0][*].contributorId",(o1, o2) -> true)));

        Customization arrayValueMatchCustomization = new Customization("contributions", arrValMatch1);
        CustomComparator customArrayValueComparator = new CustomComparator(
                JSONCompareMode.NON_EXTENSIBLE,
                arrayValueMatchCustomization);
       assertEquals(subJson1, json2, customArrayValueComparator);

I expect the above scenario should be passed. But its failing with

Exception in thread "main" java.lang.AssertionError: contributions[0][contributorId=1980]
Expected: a JSON object
     but none found
 ; contributions[0][contributorId=5789]
Unexpected: a JSON object
Rekha k
  • 21
  • 3

1 Answers1

2

Use *.contributorId instead of contributions[0][*].contributorId

Pentium1080Ti
  • 1,040
  • 1
  • 7
  • 16