bharathp
bharathp

Reputation: 395

JSONAssert.assertEquals: Ignoring multiple fields when comparing

I have the following JSON structure

{
  "name": "xyz",
  "address": {
    "street": "avenida",
    "number": "41414-44141",
    "code": "33ll",
    "moreFields": "some data"
  },
  "moreFields": "some data"
}

In my JUNIT class I will have to compare two JSON files which have the above structure. However I would like to ignore fields address.number and address.code. I understand I can use below code to ignore one field, but how can I change this to adopt to my requirements?

assertEquals(json1, json2,
return new CustomComparator(JSONCompareMode.NON_EXTENSIBLE,
      Customization.customization("address.code",
        (o1, o2) -> {
          return true; 
        })
    ));

Looking at the implementation it appears the regex we provide to the customization method is modified and I am unable to comeup with the value for path parameter which can a OR condition.

Any suggestions are much appreciated

Thanks!

Upvotes: 2

Views: 7559

Answers (1)

KJTester
KJTester

Reputation: 407

Try this

CustomComparator comparator = new CustomComparator(
            JSONCompareMode.LENIENT,
            new Customization("address.nunber", (o1, o2) -> true),
            new Customization("address.code", (o1, o2) -> true));
JSONAssert.assertEquals(
        expectedJsonAsString,
        actualJsonAsString,
        comparator);

I'm not quite sure regarding Xpath. Maybe you should have to try to prefix it with **. for recurve.

Take care. Julian.

Upvotes: 6

Related Questions