top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Crafting JSON Strings with JSON Objects for REST API Requests using TestNg

Understanding JSONObject:

JSONObject represents an unordered collection of name/value pairs. Externally, it appears as a string enclosed in curly braces with colons separating names and values, and commas separating pairs. Internally, its an object equipped with methods like GET and OPT for value retrieval by name, and PUT for value addition or replacement .Values can be of various types including Boolean,JSONArray,JSONObject,Number or String.


The GET method retrieves a value if found, otherwise, it throws an exception. In contrast, the OPT method returns a default value instead of an exception, making it handy for optional value retrieval. The PUT methods facilitate value addition or replacement in the object.

For example, consider the following code snippet:


JSONObject myString = new JSONObject();

myString.put("Name","Canada");

System.out.println("my country name is "+myString.toString);


This concise code snippet elegantly outputs {"Name" : "Canada"}, showcasing the simplicity and power of JSONObject in crafting structured data representations.



Creating JSON Strings using JSONObject for API Request Payload:


To Begin, we need to include the following dependencies in our POM.XML


<dependency>

      <groupId>org.json</groupId>

     <artifactId>json</artifactId>

     <version>20240205</version>

</dependency>


Step 1:


With the dependencies successfully incorporated, the next step is to define a JSON String format that the sample API accepts.This entails crafting a structured JSON object representing the desired payload format, exemplified by the following JSON Structure:


{
    "name": "morpheus",
    "job": "leader"
}

Step 2:


The next step is to create a class with a method designed to send the post request.Inside this method, create an object for JSONObject class, which helps us to create JSON String to follow a specific pattern required by the API.

Using a method PUT from the JSONObject class, add the value by defining a key for it. This allows us to dynamically generate the data which we want to send as part of our API request.




Following these steps carefully and using the features provided by the org.json.library, developers can efficiently create JSON strings for their REST API Requests.





Executing POST Requests with Multiple JSON Strings from a JSON File as Request Payloads using TestNG Framework


Step 1 : Creating a JSON FIle with Multiple JSON Strings.


Firstly, we need to create a JSON file containing multiple JSON Strings. Each JSON String represents data that we want to send as part of our API Requests. My JSON File looks like,




Step 2: Using DataProviders to Run Tests for Multiple Inputs.


Now, we want to run our test multiple times, each time with different inputs from the JSON file we created. We'll use DataProviders in TestNG to achieve this. DataProviders allow us to pass parameters into our test methods.

First, let's set up our DataProvider. We'll create a method that reads data from the JSON file and returns it in a format that TestNG can use:



This method reads the JSON file, extracts the data, and returns it as a 2D array where each row represents a set of parameters for our test method.


Step 3 : Writing the Test Method


Now, let's write our test method that will be executed multiple times with different inputs from JSON file:




In this method, we're creating a JSONObject using the parameters passed from the DataProvider. We then make a POST request using RestAssured with the JSON object as the request body.


Step 4: Executing the Test


Now, when we execute our test method, it will run multiple times, each time with different inputs from the JSON file. This allows us to test our API with various data sets, ensuring comprehensive test coverage.



This approach demonstrates how to leverage JSONObject and DataProviders in TestNG to efficiently handle multiple JSON strings from a JSON file and perform POST requests with different payloads in a systematic and organized manner.


Conclusion:

I hope this blog will clear your doubts regarding JSONObject and how to create JSONString using JSONObject.

Happy Learning!!

18 views0 comments
bottom of page