How to Convert HTML Table to JSON Array with jQuery

How to Convert HTML Table to JSON Array with jQuery

Introduction

In this tutorial we will create a How to Convert HTML Table to JSON Array. This tutorial purpose is to teach how to convert html table into JSON data. This will cover all the important parts from creating a table and adding the converting script. I will provide a sample program to show the actual coding of this tutorial. So Let's do the coding.

What is jQuery?

The purpose of jQuery is to make it much easier to use JavaScript on your working website projects. It will takes a lot of common tasks that require many lines of code to complete your function, and jQuery wraps them all into a methods that you can call with a single line of code. In short jQuery simplifies a lot of the complicated things from JavaScript, like AJAX calls and DOM manipulation.

Before we get started:

First you have to download the jQuery plugin.

Here is the link for the jQuery that i used in this tutorial https://jquery.com/.

Lastly, 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 an interface that will show you the actual table with data included. 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"/>
  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://www.sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="container-fluid">
  13. <div class="row">
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <div id="result">
  17. <div class="table-responsive">
  18. <table class="table table-bordered">
  19. <tr class="info">
  20. <th>Firstname</th>
  21. <th>Lastname</th>
  22. <th>Age</th>
  23. </tr>
  24. </thead>
  25. <tr>
  26. <td>John</td>
  27. <td>Smith</td>
  28. <td>25</td>
  29. </tr>
  30. <tr>
  31. <td>Claire</td>
  32. <td>Temple</td>
  33. <td>18</td>
  34. </tr>
  35. <tr>
  36. <td>Edward</td>
  37. <td>Elric</td>
  38. <td>15</td>
  39. </tr>
  40. <tr>
  41. <td>Alphonse</td>
  42. <td>Elric</td>
  43. <td>13</td>
  44. </tr>
  45. </tbody>
  46. </table>
  47. </div>
  48. </div>
  49. <button id="convert">Convert</button> <button id="reset">Reset</button>
  50. </div>
  51. </div>
  52. </div>
  53. </body>
  54. <script src="js/jquery-3.2.1.min.js"></script>
  55. <script src="js/script.js"></script>
  56. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will compress and convert all the table data into JSON array object. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. $(document).ready(function(){
  2. $("#convert").click(function(){
  3. var myTable = { myTable: [] };
  4.  
  5. var $th = $('table th');
  6. $('table tbody tr').each(function(i, tr){
  7. var obj = {}, $tds = $(tr).find('td');
  8. $th.each(function(index, th){
  9. obj[$(th).text()] = $tds.eq(index).text();
  10. });
  11. myTable.myTable.push(obj);
  12. });
  13.  
  14. $("#result").html("<pre>"+JSON.stringify(myTable, null, 2));
  15.  
  16. });
  17.  
  18. $("#reset").click(function(){
  19. window.location = "index.html";
  20. });
  21. });

In the given code we first call the basic jQuery ready event to signal that the DOM of the page is now ready to be use. After we are all set up we then bind the button by targeting the ID and use the click function in order to trigger any button event.

To convert the table into json array object we must first create a global array variable to store the html data. Afterwards we then use the jQuery each function, this function will loop through the code base on the given condition. We now then use find function to track what data will be inserted and store it using push function into the objects

Output:

The How to Convert HTML Table to JSON Array with jQuery source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Convert HTML Table to JSON Array with jQuery. 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

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
1 + 17 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.