Error in JSON array comparison customization(sky-screamer)

jsonassert
jsonassert customization example
jsonassert ignore field
how to compare two json objects in java using jackson
expected: a json object but none found
jsoncomparemode
org json jsonobject equals
java.lang.assertionerror expected a json object got a json array

I have below two JSONs to compare,

expected json:

 [
  {
    "id": 1,
    "code": 1,
    "createdOn": null,
    "lastModifiedOn": null

  },
  {
    "id": 2,
    "code": 1,
    "createdOn": null,
    "lastModifiedOn": null
  }
]

actual json

[
  {
    "id": 1,
    "code": 1,
    "createdOn": "2019-12-31",
    "lastModifiedOn": "2019-12-31",
  },
  {
    "id": 2,
    "code": 1,
    "createdOn": "2019-12-31",
    "lastModifiedOn": "2019-12-31",
  }
]

Trying to compare by ignoring couple of nodes using below code

JSONAssert.assertEquals(actualjson, expectedjson,
new CustomComparator(JSONCompareMode.STRICT, 
    new Customization("createdOn", (o1, o2) -> {
        return true;
    }), 
    new Customization("lastModifiedOn", (o1, o2) -> {
        return true;
    })
)));

but it is failing with below assertion exception,

java.lang.AssertionError: [0].createdOn
Expected: null
     got: 2019-12-31
 ; [0].lastModifiedOn
Expected: null
     got: 2019-12-31
 ; [1].createdOn
Expected: null
     got: 2019-12-31
 ; [1].lastModifiedOn
Expected: null
     got: 2019-12-31

how can I compare array of json values with customization object by skipping createdon and lastmodifiedon nodes?

<groupId>org.assertj</groupId>
<version>2.2.1</version>

Approach 1: Parse the JSON and recreate the two object JSONs without the date properties.

Approach 2: Parse the JSON and put an assertion on each property to be compared.

Error in JSONArray comparison when Customizations are , I'm facing an issue comparing 2 json ignoring the differences for some fields (i.e date fields, like createdAt, updatedAt). So, here is json1: skyscreamer / JSONassert Error in JSONArray comparison when Customizations are specified #109. I'm trying to compare two JSON-files with such code: JSONAssert.assertEquals(expected, actual, new CustomComparator(JSONCompareMode.LENIENT, new Customization("title", (o1, o2) -> true))); And all the time I'm getting errors due to JSON has a different order in their identical objects :-

Give a try to JsonUnit it allows you to ignore values, elements or path which should help.

Issues � skyscreamer/JSONassert � GitHub, REST interfaces. - skyscreamer/JSONassert. JsonAssert assert arrays different size Error in JSONArray comparison when Customizations are specified. Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.

Since SkyScreamer has open issue noted in github I found temporary solution and thought would be helpful for others,

https://github.com/skyscreamer/JSONassert/issues/109

solution:

ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JavaTimeModule());
    List<DomainObject> actualDomainObj = mapper.readValue(actualJson, new TypeReference<List<DomainObject>>() {
    });
    List<DomainObject> expectedDomainObj = mapper.readValue(expectedJson, new TypeReference<List<DomainObject>>() {
    });
    assertDomainObjResults(actualDomainObj.get(0), expectedDomainObj.get(0));


private void assertDomainObjResults(DomainObject actual, DomainObject expected) {
    softAssertions.assertThat(actual.getId()).isEqualTo(expected.getId());
    softAssertions.assertThat(actual.getLastModifiedOn()).isEqualTo(LocalDate.now());
    softAssertions.assertThat(actual.getCreatedOn()).isEqualTo(LocalDate.now());
}

accept the answer if someone finds it useful.

org.skyscreamer.jsonassert java code examples, Best Java code snippets using org.skyscreamer.jsonassert (Showing top 20 expected, String actual, boolean strict) throws Exception { JSONAssert. actual JSONArray to compare * @param mode Defines comparison behavior Creates a new {@link Customization} instance for {@code path} and {@code comparator} . expected - expected json array actual - actual json array comparator - comparator to use Returns: result of the comparison Throws: org.json.JSONException - JSON parsing error; compareJson public static JSONCompareResult compareJson(JSONString expected, JSONString actual)

org.skyscreamer.jsonassert.JSONCompareResult.fail java code , Best Java code snippets using org.skyscreamer.jsonassert. result * @return result of comparison */ public JSONCompareResult unexpected(String field, JSONArray) { compareJSONArray(prefix, (JSONArray) expectedValue, ( JSONArray) if (customization != null) { try { if (!customization.matches(prefix, actualValue,� Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

ArrayValueMatcher (JSONassert 1.5.1-SNAPSHOT API), org.skyscreamer.jsonassert. STRICT_ORDER, customizations); ArrayValueMatcher<Object> Compares the two provided objects whether they are equal. Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows: for (JSONCompareMode c : JSONCompareMode.values()) System.out.println(c); Returns: an array containing the constants of this enum type, in the order they are declared

Uses of Class org.skyscreamer.jsonassert.JSONCompareResult , Methods in org.skyscreamer.jsonassert that return JSONCompareResult provided to the expected JSONArray, and returns the results of the comparison. JSONCompareResult. fail(String field, ValueMatcherException exception) Return true if actual value matches expected value using this Customization's comparator. The above example clearly demonstrates that even with the LENIENT comparison mode, the items in the expected array has to match the items in the real array exactly. Adding or removing, even a single element, will result in a failure. 4.3. Array Specific Operations

Comments
  • change JSONCompareMode.STRICT to JSONCompareMode.LENIENT.
  • @KunalVohra LENIENT will ignore nodes completely, adding any extra nodes in future response contract will not fail test cases with this property.