How to convert JSON data to a html table using JavaScript/jQuery ? Last Updated : 19 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report To convert JSON data into an HTML table, there are multiple approaches, each of which has its own strengths. Let's walk through both approaches you mentioned, detailing how each one works.Table of ContentUsing for loopUsing JSON.stringify() MethodApproach 1: Using for loopTake the JSON Object in a variable.Call a function that first adds the column names to the < table > element. (It is looking for all columns, which is the UNION of the column names).Traverse the JSON data and match the key with the column name. Put the value of that key in the respective column.Leave the column empty if there is no value of that key.Example: This example implements the for-loop approach. HTML <!DOCTYPE HTML> <html> <head> <title> How to convert JSON data to a html table using JavaScript ? </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="constructTable('#table')"> click here </button> <br><br> <table align="center" id="table" border="1"> </table> <script> let el_up = document.getElementById("GFG_UP"); let list = [ { "col_1": "val_11", "col_3": "val_13" }, { "col_2": "val_22", "col_3": "val_23" }, { "col_1": "val_31", "col_3": "val_33" } ]; el_up.innerHTML = "Click on the button to create " + "the table from the JSON data.<br><br>" + JSON.stringify(list[0]) + "<br>" + JSON.stringify(list[1]) + "<br>" + JSON.stringify(list[2]); function constructTable(selector) { // Getting the all column names let cols = Headers(list, selector); // Traversing the JSON data for (let i = 0; i < list.length; i++) { let row = $('<tr/>'); for (let colIndex = 0; colIndex < cols.length; colIndex++) { let val = list[i][cols[colIndex]]; // If there is any key, which is matching // with the column name if (val == null) val = ""; row.append($('<td/>').html(val)); } // Adding each row to the table $(selector).append(row); } } function Headers(list, selector) { let columns = []; let header = $('<tr/>'); for (let i = 0; i < list.length; i++) { let row = list[i]; for (let k in row) { if ($.inArray(k, columns) == -1) { columns.push(k); // Creating the header header.append($('<th/>').html(k)); } } } // Appending the header to the table $(selector).append(header); return columns; } </script> </body> </html> Output: OutputApproach 2: Using JSON.stringify() methodStore the JSON object into the variable. and first put all keys in a list.Create an element <table> and a <tr> element for the header of the table.Visit the keys list and create a <th> for each value and insert it into the <tr> element created for the header.Then, for every entry in the object, create a cell and insert it to the particular row.Leave the column empty if there is no value of that key.Example: This example implements the JSON.stringify() method approach. HTML <!DOCTYPE HTML> <html> <head> <title> How to convert JSON data to a html table using JavaScript/jQuery ? </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_FUN()"> click here </button> <br><br> <table id="table" align="center" border="1px"></table> <script> let el_up = document.getElementById("GFG_UP"); let list = [ { "col_1": "val_11", "col_2": "val_12", "col_3": "val_13" }, { "col_1": "val_21", "col_2": "val_22", "col_3": "val_23" }, { "col_1": "val_31", "col_2": "val_32", "col_3": "val_33" } ]; el_up.innerHTML = "Click on the button to create the " + "table from the JSON data.<br><br>" + JSON.stringify(list[0]) + "<br>" + JSON.stringify(list[1]) + "<br>" + JSON.stringify(list[2]); function GFG_FUN() { let cols = []; for (let i = 0; i < list.length; i++) { for (let k in list[i]) { if (cols.indexOf(k) === -1) { // Push all keys to the array cols.push(k); } } } // Create a table element let table = document.createElement("table"); // Create table row tr element of a table let tr = table.insertRow(-1); for (let i = 0; i < cols.length; i++) { // Create the table header th element let theader = document.createElement("th"); theader.innerHTML = cols[i]; // Append columnName to the table row tr.appendChild(theader); } // Adding the data to the table for (let i = 0; i < list.length; i++) { // Create a new row trow = table.insertRow(-1); for (let j = 0; j < cols.length; j++) { let cell = trow.insertCell(-1); // Inserting the cell at particular place cell.innerHTML = list[i][cols[j]]; } } // Add the newly created table containing json data let el = document.getElementById("table"); el.innerHTML = ""; el.appendChild(table); } </script> </body> </html> Output: OutputBoth approaches are effective for converting JSON data to an HTML table. Depending on your needs and complexity of data, either approach can be used.JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples. Comment More infoAdvertise with us P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JSON Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of 10 min read Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel 7 min read Like