🌐
EncodedNA
encodedna.com › javascript › populate-json-data-to-html-table-using-javascript.htm
Dynamically Convert JSON to HTML Table Using JavaScript
In the markup section, I have a button to call a JavaScript function, which will extract the JSON data from the array, create a &lttable> with header and rows dynamically and finally populate the data in it. I also have DIV element that will serve as a container for our table.
🌐
ASPSnippets
aspsnippets.com › Articles › 1064 › Populate-Display-JSON-data-in-HTML-Table-using-JavaScript
Populate Display JSON data in HTML Table using JavaScript
February 26, 2019 - explained how to populate (display) JSON data in HTML Table using JavaScript. The HTML Table will be dynamically created by looping through the JSON array elements on Button click.
🌐
SitePoint
sitepoint.com › blog › javascript › make dynamic tables in seconds from any json data
Make Dynamic Tables in Seconds from Any JSON Data — SitePoint
November 13, 2024 - Converting JSON data to an HTML table using JavaScript involves parsing the JSON data and dynamically creating HTML table rows and cells. You can use the JSON.parse() method to convert the JSON data into a JavaScript object.
🌐
YouTube
youtube.com › watch
Display JSON Data in HTML Table Using JavaScript - YouTube
In this video, we’ll explore how to fetch data from a JSON file and dynamically display it in a table using JavaScript and the fetch method.⚡ Let me quick ex...
Published: April 9, 2022
🌐
ServiceNow Community
servicenow.com › community › developer-forum › how-to-show-data-from-json-within-html-table-of-content-block › m-p › 2564967
Solved: Re: How to show data from JSON within HTML table o... - ServiceNow Community
May 18, 2023 - <?xml version="1.0" encoding="utf-8" ... "Shaun", "Policy Management": "Rhyms" }; // Function to populate the HTML table with JSON data function populateTable() { var table = document.querySelector('.table-bordered'); var tableBody = table.querySelector('tbody'); // Loop through ...
🌐
{#}Codebrary
codebrary.com › 2018 › 04 › create-html-table-dynamically-using-json-data-in-javascript.html
Create HTML Table Dynamically using JSON Data in JavaScript - {#}Codebrary
See the Pen JSON Data To HTML Table by Sartaj Husain (@sartaj-husain) on CodePen. Using JavaScript we have just created a common function that accepts two parameters as following: 1st Parameter "jsonData" for JSON Data. It is a variable that holds JSON data. 2nd Parameter "elementToBind" inside ...
🌐
Sling Academy
slingacademy.com › article › javascript-displaying-json-data-as-a-table-in-html
JavaScript: Displaying JSON data as a table in HTML - Sling Academy
February 4, 2024 - In this example, we’re fetching the JSON data from a specified path, parsing it as JSON, and then passing it to a function createTable that will generate our HTML table. Next, we’ll write the createTable function that creates an HTML table dynamically with the fetched data:
Find elsewhere
🌐
YouTube
youtube.com › watch
Create Dynamic Table from JSON Data using JavaScript | Dynamically Display Data with JavaScript - YouTube
#javascript #dynamically How to Create Table Dynamically from JSON data in JavaScript. We will fetch data and then display that data dynamically in a table w...
Published: February 4, 2023
🌐
Stack Overflow
stackoverflow.com › questions › 31962105 › how-to-display-json-data-in-html-table-using-only-java-script-and-rows-should-be › 32006748
javascript - how to display json data in html table using only java script and rows should be added and removed dynamically based on json data - Stack Overflow
Thanks.. i have done it with only java script and json. ill try with ajax ... Glad you did .. happy coding ... <HTML> <HEAD> <TITLE></TITLE> <style type="text/css"> body { font-family: Arial; font-size: 10pt; } table { border: 1px solid #ccc; border-collapse: collapse; } table th { background-color: #F7F7F7; color: #333; font-weight: bold; } table th, table td { padding: 5px; border-color: #ccc; } </style> <script type="text/javascript" src="file.json"></script> <SCRIPT language="javascript"> var i=0; function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table
Top answer
1 of 3
1

There were two static field column in your table billtype and average which needed to be appended before looping the json data

 var outletCount = 0; //global variable to get the no of outlets
var data = [{
    "outlet": "JAYANAGAR",
    "cancelled": 126544,
    "duplicate": 1
  },
  {
    "outlet": "MALLESHWARAM",
    "cancelled": 31826,
    "duplicate": 31
  },
  {
    "outlet": "KOLAR",
    "cancelled": 10374,
    "duplicate": 10
  },
  {
    "outlet": "New Test",
    "cancelled": 154,
    "duplicate": 20
  }
];

let formatData = function(data) { //outlets is unique thats why formating it to loop forward in my code
  let outlets = [];
  data.forEach(element => {
    if (outlets.indexOf(element.outlet) == -1) {
      outlets.push(element.outlet);
    }
  });
  outletCount = outlets.length //calculating outlet count

  return {
    data: data,
    outlets: outlets,

  };
};

let renderTable = function(data) {
  outlets = data.outlets;
  data = data.data;
  let tbl = document.getElementById("tblOlSalesSummary");
  let table = document.createElement("table");
  let thead = document.createElement("thead");
  let headerRow = document.createElement("tr");
  let th = document.createElement("th");
  th.innerHTML = "Bill Type"; //header
  th.classList.add("text-center");
  headerRow.appendChild(th);
  th = document.createElement("th");
  th.innerHTML = "Average"; //header
  th.classList.add("text-center");
  headerRow.appendChild(th);
  outlets.forEach(element => {
    th = document.createElement("th");
    th.innerHTML = element; //this one is populating outlet as header
    th.classList.add("text-center");

    headerRow.appendChild(th);

  });

  thead.appendChild(headerRow);
  table.appendChild(thead);

  let tbody = document.createElement("tbody"); // from here onwards i don't know what to do

  let row = document.createElement("tr");

  let total = 0;

  // static field insertion for Cancelled bill
  let el = 'Cancelled bill';
  td = document.createElement("td");
  td.innerHTML = el.toLocaleString('en-in');
  td.classList.add("text-right");
  row.appendChild(td);
  // Logic start to find the average cancelled amount 
  var total_cancel =0;
  total_can_count = 0;
  outlets.forEach(outlet => { 
    data.forEach(d => {
      if (d.outlet == outlet) {
        total_cancel += parseInt(d.cancelled);
        total_can_count++;

      }
    });
  });

  let el_avg = ( total_cancel / (total_can_count) );
  td = document.createElement("td");
  td.innerHTML = el_avg.toLocaleString('en-in');
  td.classList.add("text-right");
  row.appendChild(td);
  // Logic End to find the average cancelled amount 

  outlets.forEach(outlet => { 
    let el = 0;
    data.forEach(d => {
      if (d.outlet == outlet) {
        total += parseInt(d.cancelled);
        el = d.cancelled;
      }
    });
    td = document.createElement("td");
    td.innerHTML = el.toLocaleString('en-in');
    td.classList.add("text-right");
    row.appendChild(td);
  });

  
  /* console.log("row is : " , row.children ) */

  tbody.appendChild(row);

  let row_duplicate = document.createElement("tr");

  let total_dup = 0;
  // static field insertion for duplicate bill
  let el_2 = 'Duplicate bill';
  td = document.createElement("td");
  td.innerHTML = el_2.toLocaleString('en-in');
  td.classList.add("text-right");
  row_duplicate.appendChild(td);

  // Logic start to find the Duplicate average  
  total_dup_count = 0;
  outlets.forEach(outlet => { 
    data.forEach(d => {
      if (d.outlet == outlet) {
        total_dup += parseInt(d.duplicate);
        total_dup_count++;
      }
    });
  });

  let el_avg_2 = ( total_dup / (total_dup_count) );
  td = document.createElement("td");
  td.innerHTML = el_avg_2.toLocaleString('en-in');
  td.classList.add("text-right");
  row_duplicate.appendChild(td);

  // Logic End to find the Duplicate average  

  outlets.forEach(outlet => { //i am trying to loop through outlets but getting somthing else
    let el = 0;
    data.forEach(d => {
      if (d.outlet == outlet) {
        total += parseInt(d.duplicate);
        el = d.duplicate;
      }
    });
    td = document.createElement("td");
    td.innerHTML = el.toLocaleString('en-in');
    td.classList.add("text-right");
    row_duplicate.appendChild(td);
  });


  /* console.log("row is : " , row.children ) */

  tbody.appendChild(row);
  tbody.appendChild(row_duplicate);

  table.appendChild(tbody);
  tbl.innerHTML = "";
  tbl.appendChild(table);
  table.classList.add("table");
  table.classList.add("table-striped");
  table.classList.add("table-bordered");
  table.classList.add("table-hover");
}
let formatedData = formatData(data);
renderTable(formatedData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<div align="center" class="table table-responsive">
  <table id="tblOlSalesSummary"></table>
</div>

2 of 3
1

I've written the code in this JSFiddle. Take a look. It uses JQuery.

HTML:

<table id="dataTable">

</table>

JavaScript:

var data = [{
    "outlet": "JAYANAGAR",
    "cancelled": 126544,
    "duplicate": 1
  },
  {
    "outlet": "MALLESHWARAM",
    "cancelled": 31826,
    "duplicate": 31
  },
  {
    "outlet": "KOLAR",
    "cancelled": 10374,
    "duplicate": 10
  }
];


function draw() {
    var avgCancelled = 0;
    var avgDuplicate = 0;
    var totalCancelled = 0;
    var totalDuplicate = 0;
    for (var i = 0; i < data.length; i++) {
        totalCancelled += data[i].cancelled;
        totalDuplicate += data[i].duplicate;
    }
    avgCancelled = totalCancelled / data.length;
    avgDuplicate = totalDuplicate / data.length;
    drawTableHead()
    drawTableRows(avgCancelled, avgDuplicate)
}

function drawTableHead() {
    var row = $("<tr />");
    $("#dataTable").append(row);
    row.append("<th>" + "BILLTYPE"  + "</th>")
    row.append("<th>" + "AVERAGE"  + "</th>")
    for (var i = 0; i < data.length; i++) {
        row.append("<th>" + data[i].outlet  + "</th>")
    }
}

function drawTableRows(avgCancelled, avgDuplicate) {
    var firstRow = $("<tr />");
    $("#dataTable").append(firstRow);
    firstRow.append("<td>" + "CANCELLED BILL"  + "</td>")
    firstRow.append("<td>" + avgCancelled  + "</td>")
    for (var i = 0; i < data.length; i++) {
        firstRow.append("<td>" + data[i].cancelled  + "</td>")
    }
    var secondRow = $("<tr />");
    $("#dataTable").append(secondRow);
    secondRow.append("<td>" + "DUPLICATE BILL"  + "</td>")
    secondRow.append("<td>" + avgDuplicate  + "</td>")
    for (var i = 0; i < data.length; i++) {
        secondRow.append("<td>" + data[i].duplicate  + "</td>")
    }
}


draw();
🌐
wpDataTables
wpdatatables.com › home › blog › how to convert json to an html table
How to Convert JSON to an HTML Table
May 20, 2026 - Learn how to convert JSON to an HTML table using JavaScript and jQuery. Turn your data into interactive, dynamic HTML tables easily.
🌐
YouTube
youtube.com › watch
json data to html table using javascript - YouTube
#json #javascript #htmltableJSON Data To HTML Table Using Javascript You can convert json data to html table using javascript by first parsing the JSON into ...
Published: August 15, 2023
🌐
Digitalfox-tutorials
digitalfox-tutorials.com › tutorial.php
Display JSON data in HTML table using JavaScript - Digital fox
The .json() method returns also a promise, so we have to use another .then() method to catch our data (in our case the products). That is what we do in line 5. The products argument inside the function is holding a javascript array of products. ... In line 6 we are targeting the table-body ...
Top answer
1 of 3
1

This is an example on how to accept an array of arbitrary objects and just render them on a table by inspecting all of their properties.

Maybe this is an answer trying to address a problem bigger than what was strictly asked anyway it could be interesting to inspect this option.

The main issue with this solution is that, since there's no guarantee on the fact that all objects in the array will be consistent with their properties, the very first step is navigating the whole array collecting a set of unique property names found and use it to build the table header.

Such array of unique property names will be used also later to build the table rows. Since the list of objects doesn't hold the order of the property names, they will be listed randomly in the output table.

The way header and rows get built from the propertyNames and data goes like this:

const header = buildTableHeader(uniquePropertyNames);
const rows = buildTableBody(uniquePropertyNames, data);

The order of items in the uniquePropertyNames will rule how they will be rendered on the output table.

The entry point is just:

renderObjectsToTable(data, '#target');

Where '#target' is the selector to fetch the target table to render the data inside.

Edit: I corrected a mistake I did using the variable data inside a function and I added the demo option to pass a list of html elements to the rendering procedure. Just to draw a bigger picture.

const data = [
  {
    "name": "value1",
    "email": "[email protected]"
  },
  {
    "id": "123",
    "name": "value2",
  },
  {
    "name": "value3",
    "email": "[email protected]",
    "messsage": "lorem ipsum"
  },
  {
    "name": "value4",
    "email": "[email protected]"
  }
];

renderObjectsToTable(data, '#target');
  
function renderObjectsToTable(objects, target){

  const uniquePropertyNames = [];
  objects.forEach(obj => {
    for (const prop in obj) {
      if(!uniquePropertyNames.includes(prop))
        uniquePropertyNames.push(prop);
    }
  });

  const header = buildTableHeader(uniquePropertyNames);
  const rows = buildTableBody(uniquePropertyNames, objects);

  const table = document.querySelector(target);
  table.querySelector('thead').append(header);
  table.querySelector('tbody').append(...rows);
}


function buildTableHeader(propertyNames){
  const headerRow = document.createElement('tr');
  propertyNames.forEach( prop => {
    const headerCell = document.createElement('th');
    headerCell.innerText = prop;
    headerRow.append(headerCell);
  });
  return headerRow;
}

function buildTableBody(propertyNames, objects){
  const tableRows = [];
  objects.forEach(obj => {
    debugger;
    const currentRow = document.createElement('tr');
    propertyNames.forEach(prop => {
      const tableCell = document.createElement('td');
      debugger;
      const value = (obj[prop] === undefined) ? '' : obj[prop];
      tableCell.innerText = value;
      currentRow.append(tableCell);
    });
    tableRows.push(currentRow);
  });
  return tableRows;
}

function goWild(){
  document.querySelector('#target thead').innerHTML = '';
  document.querySelector('#target tbody').innerHTML = '';
  const nodes = document.getElementById('wildList').childNodes;
  renderObjectsToTable(nodes, '#target');
}
#target{
  border-collapse: collapse;
}

#target th,
#target td
{
  border: solid 1px;
  padding: .2em;
}

.hidden{
  display: none;
}

#gowild{
  padding: .5em 2em;
  margin-top: 1em;
  cursor: pointer;
}
<table id="target">
  <thead></thead>
  <tbody></tbody>
</table>

<button id="gowild" onclick="goWild();">GO WILD</button>

<ul id="wildList" class="hidden">
  <li data-wild="very">Wild List Item #1</li>
  <li data-wild="toomuch">Wild List Item #2</li>
</ul>

2 of 3
0

const data = [{
    "name": "value1",
    "email": "[email protected]"
  },
  {
    "id": "123",
    "name": "value2",
  },
  {
    "name": "value3",
    "email": "[email protected]",
    "messsage": "lorem ipsum"
  },
  {
    "name": "value4",
    "email": "[email protected]"
  }
];

const tableBody = document.querySelector("tbody");
data.forEach(d => {
  tableBody.innerHTML += `<tr>
                    <td>${d.id || ""}</td>
                    <td>${d.name || ""}</td>
                    <td>${d.email || ""}</td>
                    <td>${d.messsage || ""}</td>
                </tr>`;
})
table {
  border-collapse: collapse;
}

table :where(tr, th, td) {
  border: 1px solid gray;
}
<table>
  <thead>
    <tr>
      <th>id</th>
      <th>name</th>
      <th>email</th>
      <th>message</th>
    </tr>
  </thead>
  <tbody></tbody>
</table>

🌐
W3Schools
w3schools.com › js › js_json_html.asp
JavaScript JSON HTML
JSON objects and arrays can contain ... Use each property name to move through the nested object. The fetch() method can load JSON from a file or server. The parsed data can then be displayed in HTML....