Payload Creation using Org.Json Library - Part 7
Published: 2021-10-16 14:19:01

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9d1hxeaec4uxqu9txea2.png)

###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u6l5fygxb8z8zy4qv07k.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xyzxdkusi5rx0bv69vb7.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztcys4r2mb2ig5jbiui2.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7y7v6cnv0wheb93j239g.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n4aifbwf0r6mgnlaz6bi.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y5908m4kyrlhkk7psoyx.png)

##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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ecrxn285vjk6i0zsvde2.png)

###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jmxx1wtrybpnw0zatxw.png)
 
2.Start the Json Server and the resource URI’s will be created as per the structure of the file
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4u6e4hempkxce7k5ys7a.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhbsa8xsiwwseezkoear.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/84smw6ydsj0rhzr8ra27.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wehurg2z5fqt75hcbon0.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u37gusicdk5qeyr0on5j.png)

##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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5b3e7vethj7m3lg836u.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0lk5qqrqbrofde5fsdro.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3krwi9eou47ug02o41m.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ak88f4z8qr2fw3adrmqz.png)
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nac490y6qcu2si298kwo.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nv62h2ox6ithrlxtqpjz.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/87tw0x2vzv42nwu2jlr8.png)