How to Convert HTML Table to JSON in JavaScript? Last Updated : 11 Oct, 2024 Comments Improve Suggest changes Like Article Like Report HTML tables are commonly used to present structured data on websites. In many scenarios, this tabular data needs to be converted to JSON format for processing, storage, or server communication. We will discuss different approaches to converting HTML tables to JSON using JavaScript.These are the following approaches: Table of ContentManual Traversal of Table Rows and CellsUsing a Library (jQuery)Using the HTMLTableElement APIManual Traversal of Table Rows and CellsIn this way data extraction is carried out by natural scrolling through tables rows and cells with the help of JavaScript, this data is then reorganized in JSON array form to give more control in the conversion.Example: In the example below, we will convert data given in table format into JSON format by manual traversal of table rows and cells. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title>Convert HTML Table to JSON</title> </head> <body> <h1>GeeksForGeeks</h1> <h5>Approach 1: Manual Traversal of Table Rows and Cells</h5> <table id="data-table" border="1"> <tr> <th>Name</th> <th>Age</th> <th>Country</th> </tr> <tr> <td>Pankaj Bind</td> <td>20</td> <td>India</td> </tr> <tr> <td>Sandeep Kumar</td> <td>30</td> <td>India</td> </tr> </table> <script> function tableToJson() { const table = document.getElementById("data-table"); const rows = table.rows; const headers = []; const jsonData = []; // Extract headers for (let i = 0; i < rows[0].cells.length; i++) { headers.push(rows[0].cells[i].innerText); } // Extract data for (let i = 1; i < rows.length; i++) { const rowObject = {}; const cells = rows[i].cells; for (let j = 0; j < cells.length; j++) { rowObject[headers[j]] = cells[j].innerText; } jsonData.push(rowObject); } return JSON.stringify(jsonData, null, 2); } console.log(tableToJson()); </script> </body> </html> Output:OutputUsing a Library (jQuery)Be using jQuery further reduce time wasted on navigation of the table and retrieval of the data, using .each() this technique further makes it less complicated in selecting table data and converting it into a JSON array.Example: In given below example we are going to convert data given in table format into JSON format using jQuery library. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title>Convert HTML Table to JSON (jQuery)</title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>GeeksForGeeks</h1> <h5>Approach 2: Using a Library (jQuery)</h5> <table id="data-table" border="1"> <tr> <th>Name</th> <th>Age</th> <th>Country</th> </tr> <tr> <td>Pankaj Bind</td> <td>20</td> <td>India</td> </tr> <tr> <td>Sandeep Kumar</td> <td>20</td> <td>India</td> </tr> </table> <script> function tableToJson() { const jsonData = []; const headers = []; $("#data-table tr").each(function (index) { if (index === 0) { $(this).find('th').each(function () { headers.push($(this).text()); }); } else { const rowObject = {}; $(this).find('td').each(function (i) { rowObject[headers[i]] = $(this).text(); }); jsonData.push(rowObject); } }); return JSON.stringify(jsonData, null, 2); } console.log(tableToJson()); </script> </body> </html> Output:Output Comment More infoAdvertise with us Next Article How to Convert HTML Table to JSON in JavaScript? pankajbind Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to Convert HTML to JSON in JavaScript ? Converting HTML to JSON is important for structured data extraction and integration with JavaScript applications. Here, we will learn different approaches to converting HTML to JSON in JavaScript. Below are the approaches to convert html to JSON in JavaScript: Table of Content Using html-to-json Lib 2 min read How to Convert Map to JSON in JavaScript ? In JavaScript, when working with data, you might encounter situations where you need to convert a Map object into a JSON format. This can be useful for sending data over the network, storing data in local storage, or interfacing with APIs that expect JSON data. Converting a Map to JSON means convert 3 min read How to Convert JSON to string in JavaScript ? In this article, we are going to learn the conversion of JSON to string in JavaScript. Converting JSON to a string in JavaScript means serializing a JavaScript object or data structure represented in JSON format into a textual JSON string for data storage or transmission.Several methods can be used 3 min read How to Convert CSV to JSON in JavaScript ? In this article, we will explain different ways to change Comma-Separated Values (CSV) data into JavaScript Object Notation (JSON) format, step-by-step. We'll break down each method with clear explanations and examples. There are several approaches available in JavaScript to convert CSV to JSON in J 3 min read How to Convert JSON Object to CSV in JavaScript ? JSON (JavaScript Object Notation) and CSV (Comma-Separated Values) are two widely used formats, each with its own strengths and applications. Fortunately, JavaScript provides powerful tools to facilitate the conversion process between these formats. These are the following approaches: Table of Conte 3 min read How to Convert XML to JSON in JavaScript? To convert XML to JSON in JavaScript, various methods and libraries and be used. Here, we use xml-js library that provides xml2json function to convert XML to JSON data. It takes XML data as input and gives the JSON objects as output. We can also use the DOMParser from the xmldom package to convert 2 min read How to Convert Excel to JSON in JavaScript ? Converting Excel spreadsheets to JSON format is a common requirement in various applications. JavaScript code utilizes the read-excel-file library to parse the Excel data, convert it to JSON format, and display it. Additionally, it provides functionality to download the generated JSON file. Approach 3 min read How to Convert JSON to Excel in JavaScript? It is often necessary to export or download JSON data in the form of Excel spreadsheets when developing web applications, any web developer would be able to go through this article as it provides a useful function of converting JSON files to Excel format using SheetsJS through JavaScript.These are t 4 min read How to convert JSON data to a html table using JavaScript/jQuery ? To convert JSON data into an HTML table, there are multiple approaches, each of which has its own strengths. Let's walk through both approaches you mentioned, detailing how each one works.Table of ContentUsing for loopUsing JSON.stringify() MethodApproach 1: Using for loopTake the JSON Object in a v 4 min read How to export HTML table to CSV using JavaScript ? Comma Separated Values or CSV is a type of text file where each value is delimited by a comma. CSV files are very useful for the import and export of data to other software applications.Sometimes while developing web applications, you may come into a scenario where you need to download a CSV file co 6 min read Like