Open In App

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:

Manual Traversal of Table Rows and Cells

In 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:

1
Output

Using 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:

1
Output

Next Article
Article Tags :

Similar Reads