JavaScript - Convert String To JSON

In this tutorial we will create a Convert String To JSON using Javascript. 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. So Let's do the coding...

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 - Convert String To JSON</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-3"></div>
  17.                 <div class="col-md-6">
  18.                         <div class="form-group">
  19.                                 <label>Firstname</label>
  20.                                 <input type="text" class="form-control" id="firstname"/>
  21.                         <div>
  22.                         <div class="form-group">
  23.                                 <label>Lastname</label>
  24.                                 <input type="text" class="form-control" id="lastname"/>
  25.                         <div>
  26.                         <div class="form-group">
  27.                                 <label>Address</label>
  28.                                 <input type="text" class="form-control" id="address"/>
  29.                         <div>
  30.                         <br />
  31.                         <center><button onclick="convertToJSON()" class="btn btn-default">Convert To JSON</button></center>
  32.                         <h3>Result</h3>
  33.                         <div id="result"></div>
  34.                 </div>
  35.         </div>
  36. </body>
  37. <script src="js/script.js"></script>
  38. </html>

Creating the Script

This code contains the script of the application. This code will convert the string data into an JSON database format. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. var obj = '{"member":[]}';
  2. var json = JSON.parse(obj);
  3.  
  4. function convertToJSON(){
  5.         var firstname = document.getElementById('firstname');
  6.         var lastname = document.getElementById('lastname');
  7.         var address = document.getElementById('address');
  8.        
  9.         if(firstname.value == "" || lastname.value == "" || address.value == ""){
  10.                 alert("Please complet the required field!");
  11.         }else{
  12.                 var str = '{"firstname": "'+firstname.value+'", "lastname": "'+lastname.value+'", "address": "'+address.value+'"}';
  13.                 json['member'].push(str);
  14.                 var decode = JSON.stringify(json, null, ' ');
  15.                 var rpl = decode.replace(/\\/g, '');
  16.                 document.getElementById('result').innerHTML = "<pre>"+rpl+"</pre>";
  17.                 firstname.value = "";
  18.                 lastname.value = "";
  19.                 address.value = "";
  20.         }
  21. }
There you have it we successfully created a Convert String To JSON 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!

Comments

Add new comment