Convert JSON to HTML Table with JavaScript [Closed]
Unfortunately, the question "Convert JSON to HTML Table with JavaScript" has been marked as closed. However, I can still provide an answer to this common programming task.
To convert JSON data to an HTML table using JavaScript, you can follow these steps:
1. Parse the JSON data using the `JSON.parse()` method. This will convert the JSON object to a JavaScript object.
const jsonData = '{"name": "John", "age": 30, "city": "New York"}';
const data = JSON.parse(jsonData);
2. Create an HTML table element using the `document.createElement()` method.
const table = document.createElement('table');
3. Create a header row for the table using the `insertRow()` and `insertCell()` methods.
const header = table.insertRow();
for (let key in data) {
const cell = header.insertCell();
cell.innerHTML = key;
}
4. Create a data row for the table using the `insertRow()` and `insertCell()` methods.
const row = table.insertRow();
for (let key in data) {
const cell = row.insertCell();
cell.innerHTML = data[key];
}
5. Append the table to the HTML document using the `appendChild()` method.
document.body.appendChild(table);
By following these steps, you can easily convert JSON data to an HTML table using JavaScript.
Leave a Reply
Related posts