You can use any of the following methods
JsonPath :
String fileContent = FileUtils.getFileContent("test.json");
JsonPath expectedJson = new JsonPath(fileContent);
given().when().get("/testurl").then().body("", equalTo(expectedJson.getList("")));
Jackson :
String fileContent = FileUtils.getFileContent("test.json");
String def = given().when().get("/testurl").then().extract().asString();
ObjectMapper mapper = new ObjectMapper();
JsonNode expected = mapper.readTree(fileContent);
JsonNode actual = mapper.readTree(def);
Assert.assertEquals(actual,expected);
GSON :
String fileContent = FileUtils.getFileContent("test.json");
String def = given().when().get("/testurl").then().extract().asString();
JsonParser parser = new JsonParser();
JsonElement expected = parser.parse(fileContent);
JsonElement actual = parser.parse(def);
Assert.assertEquals(actual,expected);
Answer from Wilfred Clement on Stack OverflowUse RestAssured's JsonPath to parse the json file into a Map and then compare it with Hamcrest Matchers. This way the order etc didn't matter.
import static org.hamcrest.Matchers.equalTo;
import io.restassured.path.json.JsonPath;
...
JsonPath expectedJson = new JsonPath(new File("/path/to/expected.json"));
given()
...
.then()
.body("", equalTo(expectedJson.getMap("")));
Karate is exactly what you are looking for - you can perform a full equality match of a JSON payload in one step.
And for the cases where you have dynamic values (generated keys, timestamps) Karate provides a very elegant way for you to ignore (or just validate the format of) these keys.
One the primary motivations for creating Karate was to come up with a better alternative to REST-assured. You can refer to this document which can help you evaluate Karate and make a case for it in your organization: Karate vs REST-assured.
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
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
If there is an uncertainty in the order of the fields then I would suggest you to use Hamcrest Matchers
You have not posted the responses so I can only give you examples
body(containsString("Hello World"));
or you could also try something like the below
body("find { it.userId == '123' }.subject", containsInAnyOrder("MATHS", "SCIENCE"))
You can use objectMapper to achieve what you're looking for, it won't fail if the properties in your object are not in the same order, check this example:
String s1 = "{ \"employee\": { \"id\": \"1212\", \"fullName\": \"John Miles\",
\"age\": 34 } }";
String s2 = "{ \"employee\": { \"id\": \"1212\", \"age\": 34, \"fullName\": \"John
Miles\" } }";
ObjectMapper mapper = new ObjectMapper();
JsonNode s1Json = mapper.readTree(s1);
JsonNode s2Json = mapper.readTree(s2);
System.out.println(s1Json.equals(s2Json));