XhCode Online Converter Tools

HTML To JSON Converter

Enter html here:
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Results:
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
HTML To JSON

Converting an HTML table to JSON (JavaScript Object Notation) means transforming the data in a structured table format into a lightweight data-interchange format that can be easily parsed and used in many programming environments. JSON is particularly useful for web applications and APIs, as it's easy to read and write for both humans and machines.

Example HTML Table:
Here's an example of an HTML table we might want to convert into JSON:

html

<table>
<tr><th>Name</th><th>Age</th><th>City</th></tr>
<tr><td>Alice</td><td>24</td><td>New York</td></tr>
<tr><td>Bob</td><td>27</td><td>Los Angeles</td></tr>
<tr><td>Charlie</td><td>22</td><td>Chicago</td></tr>
</table>
Expected JSON Output:
The JSON representation of the above HTML table would look like this:

json

[
{
"Name": "Alice",
"Age": 24,
"City": "New York"
},
{
"Name": "Bob",
"Age": 27,
"City": "Los Angeles"
},
{
"Name": "Charlie",
"Age": 22,
"City": "Chicago"
}
]
Methods to Convert HTML to JSON
1. Using Python (Automated Method)
You can use Python and libraries like BeautifulSoup for HTML parsing and json for converting the data into JSON format.

Python Code Example:
python

import json
from bs4 import BeautifulSoup

# Example HTML content
html_content = """
<table>
<tr><th>Name</th><th>Age</th><th>City</th></tr>
<tr><td>Alice</td><td>24</td><td>New York</td></tr>
<tr><td>Bob</td><td>27</td><td>Los Angeles</td></tr>
<tr><td>Charlie</td><td>22</td><td>Chicago</td></tr>
</table>
"""

# Parse the HTML
soup = BeautifulSoup(html_content, 'html.parser')
table = soup.find('table')

# Prepare the list to hold the JSON data
data = []

# Extract headers (first row)
headers = [header.text.strip() for header in table.find_all('th')]

# Extract rows (subsequent rows)
for row in table.find_all('tr')[1:]: # Skip the header row
cols = row.find_all('td')
if len(cols) > 0: # Skip empty rows
row_data = {headers[i]: cols[i].text.strip() for i in range(len(cols))}
data.append(row_data)

# Convert to JSON format and print it
json_data = json.dumps(data, indent=2)
print(json_data)
Output (JSON):
json

[
{
"Name": "Alice",
"Age": "24",
"City": "New York"
},
{
"Name": "Bob",
"Age": "27",
"City": "Los Angeles"
},
{
"Name": "Charlie",
"Age": "22",
"City": "Chicago"
}
]
Explanation of the Code:
BeautifulSoup: Parses the HTML content to extract the table data.
Headers Extraction: The first row of the table (<th>) is used to get the headers.
Rows Extraction: The remaining rows (<td>) are iterated over to extract the data.
Dictionary: For each row, a dictionary is created where the keys are the headers and the values are the corresponding cell data.
json.dumps(): This function converts the list of dictionaries into a nicely formatted JSON string.
2. Using Online Tools
If you prefer not to write code, you can use online tools to convert HTML tables to JSON. These tools allow you to paste the HTML or upload a file, and they will convert the table data into JSON format for you.

Some online tools for HTML to JSON conversion:

HTML Table to JSON Converter: Allows you to paste HTML table data and get the corresponding JSON output.
Code Beautify: Offers an HTML Table to JSON tool.
3. Using Excel (Indirect Method)
Excel doesn't have a direct export to JSON, but you can manually extract the data and save it as JSON with a few steps:

Copy the HTML table and paste it into Excel.
Use Excel's Power Query to process the data if needed.
Save the Excel file as CSV.
Use an online converter or a custom script (in Python or JavaScript) to convert the CSV file into JSON format.
Conclusion
Converting an HTML table to JSON can be easily done with Python using libraries like BeautifulSoup for HTML parsing and json for output. Alternatively, online tools provide a fast and simple solution for those who prefer not to code. JSON is a widely-used format that allows for easy integration with web applications and APIs.

TOP