Convert String To JSON Using JavaScript

The code we will tackle about is Convert String To JSON using JavaScript. The program will convert any string that you enter in to a readable JSON object. The trick within this code is that you need to push all the string into an array object, then use JSON.stringtify() in order to safely display the JSON object as a string. To learn more about this, just follow the steps below.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design 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">Convert String To JSON Using JavaScript</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-8">
  17. <h3>Result</h3>
  18. <div id="result"></div>
  19. </div>
  20. <div class="col-md-4">
  21. <div class="form-group">
  22. <label>Firstname</label>
  23. <input type="text" class="form-control" id="firstname"/>
  24. </div>
  25. <div class="form-group">
  26. <label>Lastname</label>
  27. <input type="text" class="form-control" id="lastname"/>
  28. </div>
  29. <div class="form-group">
  30. <label>Address</label>
  31. <input type="text" class="form-control" id="address"/>
  32. </div>
  33. <center><button onclick="convertString()" class="btn btn-primary">Convert</button></center>
  34. </div>
  35.  
  36. </div>
  37. </body>
  38. <script src="js/script.js"></script>
  39. </html>

Creating the Script

This code contains the script of the application. The code will forcefully convert the string into a JSON object. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. var member = JSON.parse('{"member":[]}');
  2.  
  3. function displayResult(firstname, lastname, address, json){
  4. document.getElementById('result').innerHTML = "<pre>"+json+"</pre>";
  5. firstname.value = "";
  6. lastname.value = "";
  7. address.value = "";
  8. }
  9.  
  10.  
  11. function convertString(){
  12. var firstname = document.getElementById('firstname');
  13. var lastname = document.getElementById('lastname');
  14. var address = document.getElementById('address');
  15.  
  16. if(firstname.value == "" || lastname.value == "" || address.value == ""){
  17. alert("Please complete the required field!");
  18. }else{
  19. var str = '{"firstname": "'+firstname.value+'", "lastname": "'+lastname.value+'", "address": "'+address.value+'"}';
  20. member['member'].push(str);
  21. var stringify = JSON.stringify(member, null, ' ');
  22. var json = stringify.replace(/\\/g, '');
  23.  
  24. displayResult(firstname, lastname, address, json);
  25. }
  26. }
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!

Add new comment