1
votes

Suppose the GET request returns me some JSON object:

{
 "a": 1, "b": 2, "c": 3, "date": "current_date"
}

And I have in my hand a similar object, which I'd like to check it's identical for the the keys "a", "b", "c" and ignore the "date" key.

How can I do that?

5
Compare it using a method's object in which you pass the other object without checking date field. - O.Badr
What do you mean by "using a method's object"? - yaseco

5 Answers

3
votes

I have been using JsonUnit and it really helps

String json1 = "{\r\n" + "  \"a\": 1,\r\n" + "  \"b\": 2,\r\n" + "  \"c\": 3,\r\n"
            + " \"date\": \"30-07-2020\"\r\n" + "}";

String json2 = "{\r\n" + "  \"a\": 1,\r\n" + "  \"b\": 2,\r\n" + "  \"c\": 3,\r\n"
            + " \"date\": \"31-07-2020\"\r\n" + "}";

assertThatJson(json1).whenIgnoringPaths("date").isEqualTo(json2);

Static Import :

import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;

Dependency :

    <dependency>
        <groupId>net.javacrumbs.json-unit</groupId>
        <artifactId>json-unit-assertj</artifactId>
        <version>2.18.1</version>
        <scope>test</scope>
    </dependency>

You will have to add json.org or Jackson 1.x or Jackson 2.x or Johnzon or Gson to the classpath as well

I use Jackson

1
votes

You can transform it to the Json object and delete the unwanted key. Follow the link for details: Remove key from a Json inside a JsonObject

1
votes

I found out that rest-assured has some interesting functionalities.

You could do:

@Test
public void test() {
   get("/xxxx").then().statusCode(200).assertThat()
  .body("a", equalTo(1)); 
}

More info here

1
votes

You can transform the JSON into a JS object, then compare each property of that object if that property has a key not equal to "date"

In the code below is comparing obj1 to obj2 and ignoring the date property. It prints "identical" if they are both the same and "not identical" if they are not (ignoring the date property)

var obj1 = JSON.parse ('{ "a":"1","b":"2", "c":"3", "date":"current_date"}' );

var obj2 = JSON.parse ('{ "a":"1","b":"2", "c":"3", "date":"another_date"}' );

let s1 = Object.keys(obj1).length; // length of obj1
let s2 = Object.keys(obj2).length; // length of obj2

let identical = true ;

for ( let i = 0 ; i < s1 ; i ++ ){
  if (i >= s2) {
    identical = false ;
    break ;
  }
  
  let current_key = Object.keys(obj1)[i];
  let current_value = obj1[current_key];
  
  if (current_key.localeCompare("date") != 0){
     if (current_value.localeCompare(obj2[current_key]) != 0){
       identical = false ;
       break;
     }
  }
 
}

if (identical){
  console.log ("Identical");
}else {
  console.log ("Not identical");
}
1
votes
  1. Use AssertJ ignoring fields in the comparison
    For than it's needed to deserialize json from response (for Spring Test: MvcResult.getResponse().getContentAsString()) to Object. It is possible to deserialize json response with REST Assured Content-Type based Deserialization
  2. Use unstrict compare with JSONassert.
    In order to not creating json by hand, it is possible serialize expected object with Jackson ObjectMapper. For fields exclusion:
Map<String, Object> map = objectMapper.convertValue(obj, new TypeReference<Map<String, Object>>() {});
map.remove(...);
objectMapper.writeValueAsString(obj)