JavaScript - Safely Turning a HTML String into JSON Object

In this tutorial we will create a Safely Turning a HTML String into JSON Object using JavaScript. This code will dynamically generate an JSON object from a string when user submit the input data. The code use onclick() to call a function that turn your given string into JSON object using JSON.parse in order to parse the given string into a readable JSON format. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented.

Getting started:

This is the link for the bootstrap that has been used for the layout of the calculator https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Safely Turning a HTML String into JSON Object</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-4">
  17.                         <div class="form-group">
  18.                                 <label>Firstname</label>
  19.                                 <input type="text" class="form-control" id="firstname"/>
  20.                         </div>
  21.                         <div class="form-group">
  22.                                 <label>Lastname</label>
  23.                                 <input type="text" class="form-control" id="lastname"/>
  24.                         </div>
  25.                         <center><button onclick="stringToJson()" class="btn btn-primary">Convert</button></center>
  26.                 </div>
  27.                 <div class="col-md-8">
  28.                         <h3>Result</h3>
  29.                         <div id="result"></div>
  30.                 </div>
  31.         </div>
  32. </body>
  33. <script src="js/script.js"></script>
  34. </html>

Creating the Main Function

This code contains the main function of the application. This code will turn the string data into an JSON object when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor then save it as script.js inside the js folder.
  1. var student = JSON.parse('{"student":[]}');
  2.  
  3. function stringToJson(){
  4.         var firstname = document.getElementById('firstname');
  5.         var lastname = document.getElementById('lastname');
  6.        
  7.         if(firstname.value == "" || lastname.value == ""){
  8.                 alert("Please complete the required field!");
  9.         }else{
  10.                 var str = '{"firstname": "'+firstname.value+'", "lastname": "'+lastname.value+'"}';
  11.                 student['student'].push(str);
  12.                 var stringify = JSON.stringify(student, null, ' ');
  13.                 var json = stringify.replace(/\\/g, '');
  14.                
  15.                 displayJson(firstname, lastname, json);
  16.         }
  17. }
  18.  
  19. function displayJson(firstname, lastname, json){
  20.         document.getElementById('result').innerHTML = "<pre>"+json+"</pre>";
  21.         firstname.value = "";
  22.         lastname.value = "";
  23. }
There you have it we successfully created a Safely Turning a HTML String into JSON Object using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment