XhCode Online Converter Tools

JSON To HTML Table Converter

Enter json here:
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Results:
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
JSON To HTML Table

JSON to HTML Table refers to converting a JSON object into an HTML table format. HTML tables are commonly used to present data in a tabular format on web pages. Converting JSON data to an HTML table can be very useful for displaying structured information in a web application or on a webpage.

Why Convert JSON to HTML Table?
Data Presentation: JSON is often used for data exchange, while HTML is used to display data in a browser. Converting JSON to HTML helps present the data in a user-friendly way.
Interactive Websites: Displaying data from JSON in an HTML table format is common for dynamic websites, where the content is often fetched in JSON format (e.g., via APIs).
Ease of Use: HTML tables provide a simple and efficient way to display structured data, making it easier to read and interpret.
How to Convert JSON to HTML Table?
You can easily convert JSON data into an HTML table using JavaScript or a backend programming language like Python. Here's how you can do it in different ways.

1. Using JavaScript
You can convert JSON to an HTML table dynamically using JavaScript. This is especially useful when the JSON data is fetched from an API or generated dynamically on a webpage.

Example Code in JavaScript (Browser)
html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON to HTML Table</title>
</head>
<body>

<h2>JSON to HTML Table Example</h2>

<div id="table-container"></div>

<script>
// Sample JSON data
const jsonData = [
{ "id": 1, "name": "John Doe", "department": "Engineering" },
{ "id": 2, "name": "Jane Smith", "department": "HR" },
{ "id": 3, "name": "Sam Brown", "department": "Sales" }
];

// Function to convert JSON to HTML table
function jsonToTable(jsonData) {
let table = "<table border='1'><thead><tr>";

// Extract headers from the first JSON object
for (let key in jsonData[0]) {
table += `<th>${key}</th>`;
}
table += "</tr></thead><tbody>";

// Loop through the JSON data and create rows
jsonData.forEach(item => {
table += "<tr>";
for (let key in item) {
table += `<td>${item[key]}</td>`;
}
table += "</tr>";
});
table += "</tbody></table>";

return table;
}

// Insert the table into the HTML
document.getElementById("table-container").innerHTML = jsonToTable(jsonData);
</script>

</body>
</html>
Explanation:

The JavaScript function jsonToTable(jsonData) generates an HTML table by iterating over the JSON data.
It first generates the headers based on the keys of the JSON objects and then adds rows for each item in the JSON array.
The generated table is inserted into the div with the ID table-container.
Output (in HTML Table format):

html

<table border="1">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>department</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John Doe</td>
<td>Engineering</td>
</tr>
<tr>
<td>2</td>
<td>Jane Smith</td>
<td>HR</td>
</tr>
<tr>
<td>3</td>
<td>Sam Brown</td>
<td>Sales</td>
</tr>
</tbody>
</table>
2. Using Python (for backend or script)
You can use Python to generate HTML tables if you're working on the backend, or if you want to generate an HTML file from JSON data.

Python Code Example:
python

import json

# Sample JSON data
json_data = '''
[
{"id": 1, "name": "John Doe", "department": "Engineering"},
{"id": 2, "name": "Jane Smith", "department": "HR"},
{"id": 3, "name": "Sam Brown", "department": "Sales"}
]
'''

# Load JSON data
data = json.loads(json_data)

# Function to convert JSON to HTML table
def json_to_html_table(json_data):
table = "<table border='1'><thead><tr>"

# Extract headers from the first JSON object
for key in json_data[0]:
table += f"<th>{key}</th>"
table += "</tr></thead><tbody>"

# Create rows for each item
for item in json_data:
table += "<tr>"
for key in item:
table += f"<td>{item[key]}</td>"
table += "</tr>"
table += "</tbody></table>"

return table

# Convert JSON to HTML table and print the result
html_table = json_to_html_table(data)
print(html_table)

# Save HTML table to a file
with open('output.html', 'w') as file:
file.write(html_table)

print("HTML table has been created and saved as output.html.")
Explanation:

This Python script takes the JSON data, converts it to an HTML table format, and either prints it or saves it as an HTML file.
The json_to_html_table function generates an HTML table from the JSON data.
Output (in HTML Table format):

html

<table border="1">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>department</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John Doe</td>
<td>Engineering</td>
</tr>
<tr>
<td>2</td>
<td>Jane Smith</td>
<td>HR</td>
</tr>
<tr>
<td>3</td>
<td>Sam Brown</td>
<td>Sales</td>
</tr>
</tbody>
</table>
3. Using Online Tools
If you want a quick solution without writing code, you can use online tools to convert JSON to an HTML table. Some of these tools allow you to upload a JSON file or paste your JSON data, and they generate the HTML table for you.

JSON to HTML Table Converter: Websites like ConvertJSON or Code Beautify provide tools to convert JSON data into an HTML table format.
Example JSON to HTML Table Conversion
Here's a simple example with JSON data and how it can be represented as an HTML table.

JSON Input:

json

[
{ "id": 1, "name": "John Doe", "department": "Engineering" },
{ "id": 2, "name": "Jane Smith", "department": "HR" }
]
Converted HTML Table Output:

html

<table border="1">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>department</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John Doe</td>
<td>Engineering</td>
</tr>
<tr>
<td>2</td>
<td>Jane Smith</td>
<td>HR</td>
</tr>
</tbody>
</table>
In Summary:
JavaScript: Allows you to dynamically convert JSON data into an HTML table on the client-side for use in web pages.
Python: Useful for backend conversion or script-based table generation.
Online Tools: Provide quick and easy solutions for converting JSON to HTML tables without coding.
HTML Tables: Provide a clean and readable way to display structured JSON data on a web page.

TOP