Dynamically Create/Retrieve/Delete JSON in JavaScript

goswami_sudipta
Mega Expert

In this discussion of JSON, I would like to show how JSON can be created dynamically in standard JavaScript. This technique could be used in SNow, having a slight modification to create JSON objects.


Below is a sample code in HTML and JavaScript to create, retrieve and delete JSON objects.

 

/**
                       var json = [ {"1" : "Sudipta"}, {"2" : "Goswami"}, {"3" : "FirstName"}, {"4" : "LastName"} ];
               **/
               //Global variable to hold the JSON object
               var g_objJSON;

               /** setJSON - Create JSON object
               * Returns - Nothing
               **/
               function setJSON() {

                       var v_aJSON = [];
                       var v_hObject = {};
                       var v_hTempHash = {};
                       
                       var v_sKey = document.getElementById("txtName").value;
                       var v_sValue = document.getElementById("txtPhone").value;
                       
                       try {
                               v_hObject[v_sKey] = v_sValue;

                               document.getElementById("txtName").value = "";
                               document.getElementById("txtPhone").value = "";

                               if (g_objJSON == undefined) {
                                       v_aJSON.push(v_hObject);
                               } else {
                                       v_hTempHash = mergeHashOb(g_objJSON[0], v_hObject);
                                       v_aJSON.push(v_hTempHash);
                               }
                               g_objJSON = v_aJSON;
                               alert("JSON created!");
                       } catch (x) {
                               alert(x.message);
                       }
               }

               /** getJSON - Retrieve all JSON objects against its KEY value
               * Returns - Nothing
               **/
               function getJSON() {
                       for (var item in g_objJSON[0]) {
                               alert("Name: " + item + "\nPhone: " +   g_objJSON[0][item]);
                       }
               }

               /** deleteJSON - Delete a JSON object against a KEY value
               * Returns - Nothing
               **/
               function deleteJSON() {
                       var v_aJSON = [];
                       var v_hNewHashObj = {};
                       var v_sName2Del = document.getElementById("txtNameDelete").value;
                       if (v_sName2Del == "") {
                               alert("Please enter a name to delete from JSON object");
                       } else {
                               for (var item in g_objJSON[0]) {
                                       if (item == v_sName2Del) {
                                               document.getElementById("txtNameDelete").value = "";
                                               alert("Name removed");
                                       } else {
                                               v_hNewHashObj[item] = g_objJSON[0][item];
                                       }
                               }
                               v_aJSON.push(v_hNewHashObj);
                               g_objJSON = v_aJSON;
                       }
               }

               /** mergeHashOb - Merge a new JSON object with the global JSON object
               * @prm_hObj - Existing Hash object
               * @prm_hObj2 - New Hash object
               * Returns - A new Hash object contains the merged Hash objects
               **/
               function mergeHashOb(prm_hObj, prm_hObj2) {
                       var v_hObj = {};
                       for (var item in prm_hObj) { 
                               v_hObj[item] = prm_hObj[item]; 
                       }
                       for (var item in prm_hObj2) { 
                               v_hObj[item] = prm_hObj2[item]; 
                       }
                       return v_hObj;
               }








 

I have also attached the .html file, which contains the whole html code.


Hope this would help!

1 REPLY 1

Community Alums
Not applicable

Nice job!