DEV Community

TaheraFirdose
TaheraFirdose

Posted on

Payload Creation using Org.Json Library - Part 7

In this blog we will learn how to create Payload using Org.json library and will see three different flows as below

  1. Sending simple payload
  2. Sending complex payload
  3. Sending payload using Array

JSON, as you know, is nothing more than a data representation using Object and Array. As a result, we can also construct payloads using the JSONObject and JSONArray classes. To use this feature, however, you must include the org.json library in your project. To obtain the org.json library, include this dependency in your project.

Image description

Steps:

1.Create an object of the JSONObject class, in our case we create data Object and we will be assigning the values using put method.

Important Note:

It is important to note that we must first convert the object of the JSONObject class to a string before passing it to the body method.

To assign the values, we'll use the put() method. It should not be confused with a PUT Http request. Both of these are distinct from one another

2.We are going to add below json.

Image description

3.Create a class PostRequest_Simple_OrgJSON.java and add the below code.

The org.json.simple package includes a class called JSONObject. This class represents a JSON string programmatically. Take a look at the Request JSON above; you'll note that it contains numerous nodes. The JSONObject.put(String, String) method can be used to add each node. After you've added all of the nodes, use the JSONObject.toJSONString() function to acquire the String representation of JSONObject.

we will simply add the JSON String to the body of the HTTP Request and make sure that the Content-Type is json.

Image description

4.Start the Json Server and the resource URI’s will be created as per the structure of the file:

Image description

5.Run the program and verify the results in the output window:

Image description

6.Verify the result in the db.json file. Open the file in Notepad and double-check the data posted by the above program.

Image description

7.The same can also be viewed in browser for the Items.

Image description

Sending Complex Json using Org.json:

Complex json is nested json or we can say its a json within a json. Below is the example of how complex json looks like.

Image description

Steps:

1.Create an object of the JSONObject class, in our case we create data Object for main json and childObject for nested json and we will be assigning the values using put method.

Image description

2.Start the Json Server and the resource URI’s will be created as per the structure of the file

Image description

3.Run the program and verify the results in the output window:

Image description

Image description

4.Verify the result in the db.json file. Open the file in Notepad and double-check the data posted by the above program.

Image description

5.The same can also be viewed in browser for the Items.

Image description

Sending Json in Array form using Org.json:

In the below json we can see we have main json and a nested json. The nested json consist of an array.

Image description

1.Create an object of the JSONObject class, in our case we create dataObject for main json and childJson for nested json . Since topping consist of array we will create three array objects arrayOne, arrayTwo, arrayThree and we will be assigning the values using put method.

Image description

2.Start the Json Server and the resource URI’s will be created as per the structure of the file.

Image description

3.Run the program and verify the results in the output window:

Image description

Image description

4.Verify the result in the db.json file. Open the file in Notepad and double-check the data posted by the above program.

Image description

5.The same can also be viewed in browser for the Items.

Image description

Discussion (0)