Load JSON data to Table using Jquery

Load JSON data to table using Jquery

Demo

See the Pen bLjwzy by Vinothkumar Rajendran (@fedtutorial) on CodePen.0

Note
For demo purpose I am using external api from www.mocky.io

JSON


//service.json
{
    "Cities": ["LUSAKA", "HARARE"],
    "Countries": ["ZAMBIA", "ZIMBABWE"]
}

Script


<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>WCF Client</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
    <table id="location" border='1'>
        <tr>
            <th>Countries</th>
            <th>Cities</th>
        </tr>
    </table>
    <script>
        var service = 'service.json';

        $(document).ready(function() {

            jQuery.support.cors = true;

            $.ajax({
                type: "GET",
                // url: service + '/service/',
                url: service,
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache: false,
                success: function(data) {

                    var trHTML = '';

                    $.each(data.Countries, function(i, item) {

                        trHTML += '<tr><td>' + data.Countries[i] + '</td><td>' + data.Cities[i] + '</td></tr>';
                    });

                    $('#location').append(trHTML);

                },

                error: function(msg) {

                    alert(msg.responseText);
                }
            });
        })
    </script>
</body>
</html>

Note

If you are working in local environment, please use some local server (XAMPP, WAMP, Apache) to run this code. Because of JSON local access it is not working directly without server.

Continue Reading

Load JSON data to Table using Javascript

  
    <!-- Loading JSON data here -->  
    <div id="showData"></div>
  

  <script>
    //function CreateTableFromJSON() {
    var myBooks = [{
        "Book ID": "101",
        "Book Name": "Fire of Wings",
        "Category": "Biography",
        "Price": "100"
    }, {
        "Book ID": "102",
        "Book Name": "Horrypotter",
        "Category": "Thriller",
        "Price": "1000"
    }, {
        "Book ID": "103",
        "Book Name": "Revolution 2020",
        "Category": "Romance",
        "Price": "150"
    }]

    // EXTRACT VALUE FOR HTML HEADER. 
    // ('Book ID', 'Book Name', 'Category' and 'Price')
    var col = [];
    for (var i = 0; i < myBooks.length; i++) {
        for (var key in myBooks[i]) {
            if (col.indexOf(key) === -1) {
                col.push(key);
            }
        }
    }
    // CREATE DYNAMIC TABLE.
    var table = document.createElement("table");
    // CREATE HTML TABLE HEADER ROW USING THE EXTRACTED HEADERS ABOVE.

    var tr = table.insertRow(-1); // TABLE ROW.

    for (var i = 0; i < col.length; i++) {
        var th = document.createElement("th"); // TABLE HEADER.
        th.innerHTML = col[i];
        tr.appendChild(th);
    }

    // ADD JSON DATA TO THE TABLE AS ROWS.
    for (var i = 0; i < myBooks.length; i++) {
        tr = table.insertRow(-1);
        for (var j = 0; j < col.length; j++) {
            var tabCell = tr.insertCell(-1);
            tabCell.innerHTML = myBooks[i][col[j]];
        }
    }

    // FINALLY ADD THE NEWLY CREATED TABLE WITH JSON DATA TO A CONTAINER.
    var divContainer = document.getElementById("showData");
    divContainer.innerHTML = "";
    divContainer.appendChild(table);
    //}
  </script>

Demo

See the Pen Load JSON data to table using Javascript by Vinothkumar Rajendran (@fedtutorial) on CodePen.0

Continue Reading