Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 28, 2023 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/1cb5bfaa3a72ca8a27ad99c57fbf9160 to your computer and use it in GitHub Desktop.
Save code-boxx/1cb5bfaa3a72ca8a27ad99c57fbf9160 to your computer and use it in GitHub Desktop.
Javascript JSON Data To HTML Table

JAVASCRIPT CREATE HTML TABLE WITH JSON DATA

https://code-boxx.com/create-tables-json-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>JSON To HTML Table</title>
</head>
<body>
<!-- (A) WE WILL GENERATE THE TABLE HERE -->
<div id="demoA"></div>
<script>
window.addEventListener("load", () => {
// (B) PARSE THE JSON STRING INTO OBJECT FIRST
var data = '{"Name":"John Doe","Email":"john@doe.com","Gender":"male"}';
data = JSON.parse(data);
// console.table(data);
// (C) GENERATE TABLE
// (C1) CREATE EMPTY TABLE
var table = document.createElement("table"), row, cellA, cellB;
document.getElementById("demoA").appendChild(table);
for (let key in data) {
// (C2) ROWS & CELLS
row = table.insertRow();
cellA = row.insertCell();
cellB = row.insertCell();
// (C3) KEY & VALUE
cellA.innerHTML = key;
cellB.innerHTML = data[key];
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JSON To HTML Table</title>
</head>
<body>
<!-- (A) WE WILL GENERATE THE TABLE HERE -->
<div id="demoB"></div>
<script>
window.addEventListener("load", () => {
// (B) PARSE THE JSON STRING INTO OBJECT FIRST
var data = '{"Name":"John Doe","Email":"john@doe.com","Gender":"male"}';
data = JSON.parse(data);
// console.table(data);
// (C) GENERATE TABLE
var table = "<table>";
for (let key in data) {
table += `<tr><td>${key}</td><td>${data[key]}</td></tr>`;
}
table += "</table>";
document.getElementById("demoB").innerHTML = table;
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Multi-Dimensional Object To Table</title>
</head>
<body>
<!-- (A) WE WILL GENERATE THE TABLE HERE -->
<div id="demoC"></div>
<script>
window.addEventListener("load", () => {
// (B) PARSE THE JSON STRING INTO OBJECT FIRST
var data = '{"Name":"John Doe","Email":"john@doe.com","Gender":"male","Colors":["red","green","blue"],"Pet":{"Name":"Roger Doe","Species":"Canis lupus familiaris"}}';
data = JSON.parse(data);
// console.table(data);
// (C) GENERATE TABLE
// (C1) CREATE EMPTY TABLE
var table = document.createElement("table"), row, cellA, cellB;
document.getElementById("demoC").appendChild(table);
for (let key in data) {
// (C2) ROWS & CELLS
row = table.insertRow();
cellA = row.insertCell();
cellB = row.insertCell();
// (C3) KEY & VALUE
cellA.innerHTML = key;
// (C3-1) COLORS FIELD
if (key=="Colors") {
cellB.innerHTML = data[key].join(", ");
}
// (C3-2) PET FIELD
else if (key=="Pet") {
cellB.innerHTML = `<div>Name: ${data[key]["Name"]}</div><div>Species: ${data[key]["Species"]}</div>`;
}
// (C3-3) OTHER FIELDS
else {
cellB.innerHTML = data[key];
}
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment