JsonPath Use In Rest Assured

 Reading and Modifying JSON:

  • Start by reading a JSON file that contains the initial data.
  • Use a JSON library, such as JsonPath, to modify specific values within the JSON data.
  • Update fields as required before sending the modified JSON data in the REST API request.

Sending the POST Request:

  • Use an HTTP client, like HttpClient or RestAssured, to send the POST request with the modified JSON data.
  • Validate the response to ensure the API behaves as expected.

  • introduction:

    • Mention that you will explain how you handled modifying JSON data and sending it in a POST request for a REST API integration test.
  • Reading the JSON File:

    • Explain that you start by reading the JSON file into a string.
    • Use Java's Files class to read the file content: String jsonContent = new String(Files.readAllBytes(Paths.get(jsonFilePath)));.
  • Modifying JSON Using JsonPath:

    • Use the JsonPath library to parse and modify the JSON content.
    • Parse the JSON content: JSONObject jsonObject = JsonPath.parse(jsonContent).json();.
    • Modify specific fields, for example, changing the name field to "ben": JsonPath.parse(jsonObject).set("$.name", "ben");.
  • Converting JSON Object Back to String:

    • Convert the modified JSON object back to a string for sending in the POST request: String updatedJsonContent = jsonObject.toJSONString();.
  • Sending the POST Request:

    • Use an HTTP client library like RestAssured to send the POST request with the updated JSON content.
    • Set the request headers and body: given().header("Content-Type", "application/json").body(updatedJsonContent).
    • Send the POST request and validate the response: .when().post("http://yourapiendpoint.com/api").then().statusCode(200);.
  • Conclusion:

    • Summarize the process: reading the JSON file, modifying it using JsonPath, and sending the modified JSON data in a POST request.
  • In my integration test for a REST API, I needed to send a POST request with a JSON payload that had dynamic values. Here’s how I approached it:

    1. **Reading the JSON File**: 
       I started by reading the JSON file into a string using Java's `Files` class. This allowed me to work with the JSON data programmatically.

    2. **Modifying the JSON Data**: 
       I used the `JsonPath` library to parse the JSON string into a JSON object. With JsonPath, I was able to modify specific fields in the JSON object. For example, I changed the `name` field to "ben" using the expression `JsonPath.parse(jsonObject).set("$.name", "ben");`.

    3. **Converting JSON Object Back to String**:
       After making the necessary modifications, I converted the JSON object back to a string to prepare it for the HTTP request.

    4. **Sending the POST Request**:
       To send the POST request, I used the RestAssured library. I set the content type to JSON, included the modified JSON string in the body of the request, and sent it to the API endpoint. I also validated the response status code to ensure the request was successful.

    This approach allowed me to dynamically modify the JSON payload before sending it in a POST request, ensuring that the API received the correct data for each test case.




    Comments