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 Overflow
🌐
Makeseleniumeasy
makeseleniumeasy.com › 2021 › 02 › 19 › rest-assured-tutorial-68-compare-two-json-using-jackson-java-library
REST Assured Tutorial 68 – Compare Two JSON using Jackson – Java Library
February 19, 2021 - An abstract class JsonNode provided by Jackson API provides an abstract method called equals() which can be used to compare node objects. Equality for node objects is defined as a full (deep) value equality. This means that it is possible to compare complete JSON trees for equality by comparing ...
🌐
Makeseleniumeasy
makeseleniumeasy.com › 2021 › 05 › 14 › rest-assured-tutorial-72-how-to-compare-part-of-json-objects-and-arrays-using-jsonassert-library
REST Assured Tutorial 72 – How To Compare Part of JSON Objects and Arrays using JSONassert library
May 14, 2021 - We need to compare that the “address” object in the first JSON Object has the same values for keys as “communicationAddress” in the second JSON Object.
🌐
Google Groups
groups.google.com › g › rest-assured › c › 0NoxuFRhZtE
Re: [rest-assured] Comparing JSon file to part of a response
So is your question is essentially how to compare two JSON documents with each other? This is currently not supported by REST Assured or JsonPath.
🌐
QA Automation Expert
qaautomation.expert › 2024 › 09 › 03 › how-to-compare-json-file-with-json-response
How to compare JSON File with JSON Response – QA Automation Expert
September 3, 2024 - The ObjectMapper class from the Jackson library is used to parse the JSON content, and the `equals` method is used to compare the two JsonNode objects. ... Using the Jackson library’s ObjectMapper, read the JSON file’s content into a JsonNode ...
Find elsewhere
Top answer
1 of 1
1

What I understood from the question is to get response from an API and compare with JSON file. How do it:

 @Test
public void GetCity() {
        Response response = when().
            get(city).
        then().
            extract()
            response();

}

First, we extract the Response object which contains information like status code or response body. In this case it will be JSON. Before we extract it, let's create a POJO with JSON representation:

{
  "id": 25,
  "first_name": "Caryl",
  "last_name": "Ruberry",
  "email": "[email protected]",
  "ip_address": "222.10.201.47",
  "latitude": 11.8554828,
  "longitude": -86.2183907,
  "city": "Dolores"
}

The above JSON can be represented by below class:

public class UserEntity {
    public Long id; //id is exact name field in JSON
    @JsonProperty("first_name"); //other approach
    public String firstName;
    public String last_name;
    public String email;
    public String ip_address;
    public Long latitude;
    public Long longitude;
    public String city;
} 

Now, we can transform the JSON response body into this class like this:

 @Test
public void GetCity() {
        Response response = when().
            get(city).
        then().
            extract()
            response();
        UserEntity userEntityResponse = response.jsonPath().getObject("$", UserEntity.class);
}

The "$" means root of JSON file (the first object {}). This is how Response is translated into POJO. We can do it in a very similar matter

        Response response = when().
            get(city).
        then().
            extract()
            response();
        UserEntity userEntityResponse = response.jsonPath().getObject("$", UserEntity.class);
        UserEntity userEntityFile = JsonPath.from(new File("file path"));

Now you can easily compare them like:

assertEquals(userEntityFile.id, userEntityResponse.id);

You could also override hashCode() and equals() methods but that might be too much if you're just learning :)

🌐
YouTube
youtube.com › watch
Rest Assured | 67 | Interview question | How to compare Two json files | தமிழ் - YouTube
Support by donating:---------------------------------Google pay UPI Id: arulprasath36@okiciciName: ArulprasathEmail: arulprasath36@gmail.comFollow me on Link...
Published   November 10, 2022
🌐
Quora
quora.com › What-is-the-best-way-to-do-API-response-comparison-I-have-been-comparing-the-JSON-response-against-a-stored-JSON-response-using-REST-Assured-framework-Is-there-a-better-way
What is the best way to do API response comparison? I have been comparing the JSON response against a stored JSON response using REST Assured framework. Is there a better way? - Quora
Answer (1 of 2): As long as you are doing data driven testing, atleast your approach is effective. If not, then make your tests data driven. Also, there is a concept called as JsonSchema via which you can test the schema of the response extensively. Kindly refer to the following video for more d...
🌐
GitHub
github.com › rest-assured › rest-assured › issues › 595
Add support to compare response body to a template · Issue #595 · rest-assured/rest-assured
September 30, 2015 - I want to have a JSON file with a template of the expected response, load the file contents to a string and then in Rest assured assert that it matches the response body. This will let me verify the response body elegantly at once as a w...
Author   ivos
🌐
Pavantestingtools
pavantestingtools.com › 2024 › 11 › how-to-compare-json-file-with-json.html
SDET-QA Blog: How to Compare a JSON File with a JSON Response
REST Assured sends a GET request and retrieves the JSON response body as a string. ... JSONAssert.assertEquals compares the two JSONs.
🌐
Blogger
restservicestesting.blogspot.com › 2021 › 07 › comparing-json-responses-using-json-assert.html
Comparing JSON responses using JsonAssert Library - Lenient Mode
July 23, 2021 - In this post, we would learn about using RequestSpecBuilder in Rest Assured. You can use the builder to construct a request specification. RequestSpecBuilder is normally used when we have common parameters, Base URI, Base Path between different tests. Let's look at an example without RequestSpecBuilder. Here we are making use of Google Search API that we studied in Testing GET Requests and their Responses using Rest Assured .
🌐
CopyProgramming
copyprogramming.com › howto › how-to-compare-2-json-responses
How to compare 2 json responses - Rest assured
April 26, 2023 - String fileContent = ... ObjectMapper(); JsonNode expected = mapper.readTree(fileContent); JsonNode actual = mapper.readTree(def); Assert.assertEquals(actual,expected);...
🌐
Makeseleniumeasy
makeseleniumeasy.com › 2021 › 03 › 08 › rest-assured-tutorial-70-compare-json-objects-using-jsonassert-library
REST Assured Tutorial 70 – Compare JSON Objects using JSONassert Library
March 8, 2021 - For example – If we are going to get the same JSON response for an API every time or some parts of the response are always constant or similar or we just want to check the presence of some fields in another JSON then instead of writing some logic to assert them, we can directly compare with an existing JSON response. We have already compared two JSONs using the Jackson library here.
🌐
Stack Overflow
stackoverflow.com › questions › 75401289 › how-to-compare-two-json-fields-in-restassured-and-hamcrest
rest assured - How to compare two JSON fields in RestAssured and Hamcrest? - Stack Overflow
February 9, 2023 - I'm using RestAssured and Hamcrest to functional test our Back-end API and I'd like to know if there's any way to compare two distinct JSON fields inside the body method, or any equivalent. For exa...