XhCode Online Converter Tools

HTML Table To JSON Converter

Enter html here:
Results:
HTML Table To JSON

Converting an HTML table to JSON (JavaScript Object Notation) allows you to take data from a table in a webpage and represent it in a structured format that's easy to work with programmatically. JSON is commonly used for data exchange, especially in web development and APIs.

Why Convert HTML Table to JSON?
Data Manipulation: JSON is more flexible and easier to work with programmatically compared to HTML tables.
Web Development: If you're building dynamic web applications that rely on JSON data, converting HTML tables to JSON can be an essential step.
Data Exchange: JSON is a lightweight format for transferring data, and many APIs accept JSON format for input and output.
How Does HTML Table to JSON Conversion Work?
When you convert an HTML table to JSON:

Each row in the table will become an object in the JSON array.
Each column header becomes a key for the corresponding data in that row.
The table data (in <td> elements) becomes the values associated with the respective keys.
Example HTML Table:
html

<table id="data-table">
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
<tr>
<td>John</td>
<td>28</td>
<td>USA</td>
</tr>
<tr>
<td>Jane</td>
<td>22</td>
<td>Canada</td>
</tr>
<tr>
<td>Tom</td>
<td>30</td>
<td>UK</td>
</tr>
</table>
Converted JSON Output:
json

[
{
"Name": "John",
"Age": 28,
"Country": "USA"
},
{
"Name": "Jane",
"Age": 22,
"Country": "Canada"
},
{
"Name": "Tom",
"Age": 30,
"Country": "UK"
}
]
Methods to Convert HTML Table to JSON
1. Using JavaScript (For Quick Conversion on a Webpage):
If the HTML table is already on a webpage, you can easily use JavaScript to convert the table to JSON format.

JavaScript Code:
Here's a simple JavaScript solution to convert an HTML table to JSON:

html

<!DOCTYPE html>
<html>
<head>
<title>HTML Table to JSON</title>
</head>
<body>
<table id="data-table">
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
<tr>
<td>John</td>
<td>28</td>
<td>USA</td>
</tr>
<tr>
<td>Jane</td>
<td>22</td>
<td>Canada</td>
</tr>
<tr>
<td>Tom</td>
<td>30</td>
<td>UK</td>
</tr>
</table>
<button onclick="downloadJSON()">Download JSON</button>

<script>
function tableToJson(table) {
var rows = table.rows;
var jsonArray = [];

for (var i = 1; i < rows.length; i++) { // Start from 1 to skip the header row
var row = rows[i];
var rowData = {};

for (var j = 0; j < row.cells.length; j++) {
var key = rows[0].cells[j].textContent; // Use header row as keys
var value = row.cells[j].textContent;
rowData[key] = value;
}
jsonArray.push(rowData);
}
return jsonArray;
}

function downloadJSON() {
var table = document.getElementById("data-table");
var jsonData = tableToJson(table);
var jsonFile = new Blob([JSON.stringify(jsonData, null, 2)], {type: "application/json"});
var a = document.createElement("a");
a.href = URL.createObjectURL(jsonFile);
a.download = "table_data.json";
a.click();
}
</script>
</body>
</html>
How it Works:
The function tableToJson() converts the table into an array of JSON objects. Each object represents a row of the table, where the keys are the header names and the values are the corresponding cell data.
The downloadJSON() function allows you to download the JSON data as a .json file.
Steps:
Copy the code into an HTML file.
Open the file in your browser.
Click the "Download JSON" button, and it will download the table data in JSON format.
2. Using Online Tools:
If you don't want to write JavaScript code, you can use online tools to convert HTML tables to JSON quickly:

TableConvert:

Paste your HTML code (or upload an HTML file) containing the table.
It will automatically convert the HTML table into a JSON array.
You can download the resulting JSON or copy it to your clipboard.
ConvertCSV:

Paste the HTML table or upload an HTML file.
The tool will convert it into JSON format that you can download.
JSON Formatter & Validator:

This tool will also convert HTML tables to JSON, and it formats the output nicely.
Steps:
Copy the HTML table or upload the file.
The tool will generate the JSON output.
Download or copy the result.
3. Using Python (For Automated Conversion):
If you have an HTML file with multiple tables and want to automate the process of extracting them to JSON, you can use Python with BeautifulSoup and pandas.

Python Script:
python

import pandas as pd
from bs4 import BeautifulSoup

def html_table_to_json(html_file, json_file):
with open(html_file, 'r') as file:
soup = BeautifulSoup(file, 'html.parser')
tables = soup.find_all('table')

# Assuming only one table is in the HTML file
df = pd.read_html(str(tables[0]))[0] # Converts the first table to DataFrame

# Convert DataFrame to JSON
df.to_json(json_file, orient='records', lines=False)
print(f"JSON file saved as {json_file}")

# Example usage
html_table_to_json('your_file.html', 'output.json')
Explanation:
BeautifulSoup is used to parse the HTML file and extract the table.
pandas.read_html() converts the table into a DataFrame.
The DataFrame is then converted into a JSON file using to_json().
Steps:
Install required libraries: pip install pandas beautifulsoup4 lxml.
Run the script to read the HTML file and convert the table to a JSON file.
4. Manually Writing JSON:
For very small HTML tables, you can manually write the corresponding JSON. This is not ideal for large datasets but can be useful for simple tables.

For example, the following HTML table:

html

<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
<tr>
<td>John</td>
<td>28</td>
<td>USA</td>
</tr>
</table>
Would be manually written as:

json

[
{
"Name": "John",
"Age": 28,
"Country": "USA"
}
]
Summary of Methods:
JavaScript (Browser Solution): Use a custom JavaScript function to extract table data and convert it to JSON. Ideal for use directly on a webpage.
Online Tools: Use websites like TableConvert and ConvertCSV to quickly convert HTML tables to JSON without coding.
Python: Automate the conversion of HTML tables to JSON using BeautifulSoup and pandas for more complex or bulk conversions.
Manual Conversion: For small datasets, you can manually convert HTML table rows and headers into JSON.