How to Convert HTML Inputs to JSON Object in JavaScript

How to Convert HTML Inputs to JSON Object in JavaScript

Introduction

In this tutorial we will create a How to Convert HTML Inputs to JSON Object in JavaScript. This tutorial purpose is to teach you how to convert your html inputs into JSON object. This will tackle all the basic function that will convert your form data. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you want to convert your form data into a readable JSON object. I will give my best to provide you the easiest way of creating this program Convert form data into JSON Object. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display html form and JSON object. To create this simply copy and write it into your text editor, then save it 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">How to Convert HTML Inputs to JSON Object</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-8">
  17. <h3>JSON 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. <center><button onclick="convertToJson()" class="btn btn-primary">Submit</button></center>
  30. </div>
  31. </div>
  32. </body>
  33. <script src="script.js"></script>
  34. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically convert your html form data into JSON object when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. let member = JSON.parse('{"members":[]}');
  2.  
  3.  
  4. function displayJson(firstname, lastname, json){
  5. document.getElementById('result').innerHTML = "<pre>"+json+"</pre>";
  6. firstname.value = "";
  7. lastname.value = "";
  8. }
  9.  
  10.  
  11. function convertToJson(){
  12. let firstname = document.getElementById('firstname');
  13. let lastname = document.getElementById('lastname');
  14.  
  15. if(firstname.value == "" || lastname.value == ""){
  16. alert("Please complete the required field!");
  17. }else{
  18. let str = '{"firstname": "'+firstname.value+'", "lastname": "'+lastname.value+'"}';
  19. member['members'].push(str);
  20. let stringify = JSON.stringify(member, null, ' ');
  21. let json = stringify.replace(/\\/g, '');
  22.  
  23. displayJson(firstname, lastname, json);
  24. }
  25. }

In the code above we create a method called convertToJson(), this function will convert the html form data into JSON object. We bind first the two input into variables then create a JSON structure with the two variables in it called str. We push the structure to array object and then we use JSON.stringify() function to make turn the array into JSON object.

Output:

The How to Convert HTML Inputs to JSON Object in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Remove Element from an Array in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment