If you are currently using Restful APIs and you need to construct a json string response using a web project, this article will help you create json strings using javascript. This is very useful. We will use the jQuery plug-in $. toJSON to convert the data object to the json format. Use JavaScript to construct a JSON string JavaScript code: the javascript code is included here. $ ("# Form"). submit (function () {}-delete_button is the ID of the form tag. We call the value of the form input box through element. val. The Code is as follows: <script src = "jquery. min. js "> </script> <script src =" jquery. json-2.2.js> </script> <script src = "GetPostAjax. js "> </script> <script type =" text/javascript "> $ (document ). ready (function () {$ ("# form "). submit (function (e) {e. preventDefault (); var username, email, password, gender; username =$ ("# username "). val (); email = $ ("# email "). val (); password = $ ("# username "). val (); gender = $ ("# gender "). val (); if (username. length> 0 && Email. length> 0 & password. length> 0 & gender. length> 0) {// Creating Objectsvar request = new Object (); var userDetails = new Object (); var user = new Object (); var websites = new Array (); user. name = username; user. email = email; user. password = password; user. gender = gender; // Array puslif (website1.length> 0) websites. push (website1); if (website2.length> 0) websites. push (website2); if (website3.length> 0) websites. push (we Bsite3); user. websites = websites; userDetails. user = user; request. userDetails = userDetails; var jsonfy = $. toJSON (request); // Encodes special charactersvar encodedata = 'jsondata = '+ encodeURIComponent (jsonfy); // Ajax Callvar url = 'website API url'; post_data (URL, encodedata, function (data) {alert ("Success") ;}}}) ;}); </script "> HTML code: <form method = 'post' action = ''id = 'form'> Name <input type = 'text' n Ame = 'username' id = 'username'/> Email <input type = 'text' name = 'email 'id = 'email '/> Password <input type = 'text' name = 'Password' id = 'Password'/> Gender <select name = 'gender' id = 'gender'> <option value = 'male'> male </option> <option value = 'female '> female </option> </select> Websites <input type = 'text' id = 'website1'/> <input type = 'text' id = 'website2'/> <input type = 'text' id = 'website3 '/> <input type = 'submit' id ='s Ubmit '/> </form> JSON output {"userDetails": {"user": {"name": "Srinivas Tamada", "email": "srinivas@9lessons.info ", "password": "Srinivas Tamada", "gender": "male", "websites": ["www. software8.co "," www.heatpress123.net "," www.0769zzw.com "] }} JSON Encoded encode special characters. The following characters are Encoded :,/?: @ & =+ $ # Jsondata = % 7B % 22 userDetails % 22% 3A % 7B % 22 user % 22% 3A % 7B % 22 name % 22% 3A % 22 Srinivas % 20 Tamada % 22% 2C % 22 email % 22% 3A % 22 srinivas % 409lessons.info % 22% 2C % 22 password % 22% 3A % 22 Srinivas % 20 Tamada % 22% 2C % 22 gender % 22% 3A % 22 male % 22% 2C % 22 websites % 22% 3A % 5B % 22www.9lessons.info % 22% 2C % 22www. egglabs. in % 22% 2C % 22www.fglogin.com % 22% 5D % 7D % 7D % 7DGetPostAjax. js defines jquery's ajax request method. Function post_data (url, encodedata, success) {$. ajax ({type: "POST", url: url, data: encodedata, dataType: "json", restful: true, contentType: 'application/json', cache: false, timeout: 20000, async: true, beforeSend: function (data) {}, success: function (data) {success. call (this, data) ;}, error: function (data) {alert ("Error In Connecting ");}});}