How do i pass a json file in html?


JSON can very easily be translated into JavaScript.

JavaScript can be used to make HTML in your web pages.


HTML Table

Make an HTML table with data received as JSON:

Example

const dbParam = JSON.stringify({table:"customers",limit:20});
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
  myObj = JSON.parse(this.responseText);
  let text = "

"
  for (let x in myObj) {
    text += "";
  }
  text += "
" + myObj[x].name + "
"
  document.getElementById("demo").innerHTML = text;
}
xmlhttp.open("POST", "json_demo_html_table.php");
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);

Try it Yourself »


Dynamic HTML Table

Make the HTML table based on the value of a drop down menu:

Example

Try it Yourself »



HTML Drop Down List

Make an HTML drop down list with data received as JSON:

Example

const dbParam = JSON.stringify({table:"customers",limit:20});
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
  const myObj = JSON.parse(this.responseText);
  let text = ""
  document.getElementById("demo").innerHTML = text;
  }
}
xmlhttp.open("POST", "json_demo_html_table.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);

Try it Yourself »



View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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:

    How do i pass a json file in html?

    Example:

    <html lang="en">

    <head>

        <meta charset="UTF-8">

        <title>GFG User Detailstitle>

        <script src=

        script>

        <style>

            table {

                margin: 0 auto;

                font-size: large;

                border: 1px solid black;

            }

            h2 {

                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>

            <h2>GeeksForGeeksh2>

            <table id='table'>

                <tr>

                    <th>GFG UserHandleth>

                    <th>Practice Problemsth>

                    <th>Coding Scoreth>

                    <th>GFG Articlesth>

                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:

    How do i pass a json file in html?

    HTML is the foundation of webpages, is used for webpage development by structuring websites and web apps.You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.

    jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”.
    You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.


    How do I put JSON data in my HTML page?

    Approach 1:.
    Take the JSON Object in a variable..
    Call a function which first adds the column names to the < table > element. (It is looking for the all columns, which is UNION of the column names)..
    Traverse the JSON data and match key with the column name. ... .
    Leave the column empty if there is no value of that key..

    Can we use JSON in HTML?

    JSON can very easily be translated into JavaScript. JavaScript can be used to make HTML in your web pages.

    How do I display formatted JSON data in HTML?

    Use the JSON. stringify function to Display formatted JSON in HTML. If you have unformatted JSON It will output it in a formatted way. Or Use
     tag for showing code itself in HTML page and with JSON.