IsaacLevon
IsaacLevon

Reputation: 2570

REST assured - compare two JSON objects

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?

Upvotes: 1

Views: 4638

Answers (5)

Grigory Kislin
Grigory Kislin

Reputation: 18010

  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)

Upvotes: 1

Wilfred Clement
Wilfred Clement

Reputation: 2774

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

Upvotes: 4

EEAH
EEAH

Reputation: 747

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");
}

Upvotes: 1

Eduardo Briguenti Vieira
Eduardo Briguenti Vieira

Reputation: 4589

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

Upvotes: 1

Kirill Korolyov
Kirill Korolyov

Reputation: 11

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

Upvotes: 1

Related Questions