The task is to fetch data from the given JSON file and convert data into an HTML table.
Approach: We have a JSON file containing data in the form of an array of objects. In our code, we are using jQuery to complete our task. The jQuery code uses getJSON() method to fetch the data from the file’s location using an AJAX HTTP GET request. It takes two arguments. One is the location of the JSON file and the other is the function containing the JSON data. The each() function is used to iterate through all the objects in the array. It also takes two arguments. One is the data and the other one is the function containing the index and the element. An empty string is used to construct rows that contain the data from the JSON objects. The append() method is used to append the string containing rows in the table.
JSON file:

Example:
<html lang="en"> <head> <meta charset="UTF-8"> <title>GFG User Details</title> <!-- INCLUDING JQUERY--> <script src= </script> <!-- CSS FOR STYLING THE PAGE --> <style> table { margin: 0 auto; font-size: large; border: 1px solid black; } h1 { text-align: center; color: #006600; font-size: xx-large; font-family: 'Gill Sans', 'Gill Sans MT', ' Calibri', 'Trebuchet MS', 'sans-serif'; } td { background-color: #E4F5D4; border: 1px solid black; } th, td { font-weight: bold; border: 1px solid black; padding: 10px; text-align: center; } td { font-weight: lighter; } </style> </head> <body> <section> <h1>GeeksForGeeks</h1> <!-- TABLE CONSTRUCTION--> <table id='table'> <!-- HEADING FORMATION --> <tr> <th>GFG UserHandle</th> <th>Practice Problems</th> <th>Coding Score</th> <th>GFG Articles</th> </tr> <script> $(document).ready(function () { // FETCHING DATA FROM JSON FILE $.getJSON("gfgdetails.json", function (data) { var student = ''; // ITERATING THROUGH OBJECTS $.each(data, function (key, value) { //CONSTRUCTION OF ROWS HAVING // DATA FROM JSON OBJECT student += '<tr>'; student += '<td>' + value.GFGUserName + '</td>'; student += '<td>' + value.NoOfProblems + '</td>'; student += '<td>' + value.TotalScore + '</td>'; student += '<td>' + value.Articles + '</td>'; student += '</tr>'; }); //INSERTING ROWS INTO TABLE $('#table').append(student); }); }); </script> </section> </body> </html> |
Output:

Recommended Posts:
- How to fetch data from localserver database and display on HTML table using PHP ?
- How to convert JSON data to a html table using JavaScript/jQuery ?
- How to append data in JSON file through HTML form using PHP ?
- How to convert JSON string to array of JSON objects using JavaScript ?
- How to add table row in a table using jQuery?
- How to remove table row from table using jQuery ?
- How to send data of HTML form directly to JSON file?
- How to load data from JSON into a Bootstrap Table?
- JSON | modify an array value of a JSON object
- What is the difference between display: inline and display: inline-block in CSS?
- PHP program to fetch data from localhost server database using XAMPP
- AngularJS | fetch data from API using HttpClient
- How to fetch data from Database in PHP PDO using loop ?
- How to create a dynamic JSON file by fetching data from localserver database ?
- How to Use the JavaScript Fetch API to Get Data?
- How to read and write JSON file using Node.js ?
- How to import data from .CSV file into MySQL table using Node.js ?
- Get and Post method using Fetch API
- Simple GET and POST request using Fetch API method by making custom HTTP library
- How to get file input by selected file name without path using jQuery ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


