JavaScript - Safely Turning a HTML String into JSON Object

Submitted by: 
Visitors have accessed this post 151 times.

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

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="<a href="">Sourcecodester</a>
  10. " rel="nofollow">">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Safely Turning a HTML String into JSON Object</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  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. <center><button onclick="stringToJson()" class="btn btn-primary">Convert</button></center>
  27. </div>
  28. <div class="col-md-8">
  29. <h3>Result</h3>
  30. <div id="result"></div>
  31. </div>
  32. </div>
  33. </body>
  34. <script src="js/script.js"></script>
  35. </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":[]}');
  3. function stringToJson(){
  4. var firstname = document.getElementById('firstname');
  5. var lastname = document.getElementById('lastname');
  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, '');
  15. displayJson(firstname, lastname, json);
  16. }
  17. }
  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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.