All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Display JSON Data In HTML Using Ajax

Last Updated : Mar 11, 2024

How To Display JSON Data In HTML Using Ajax

In this article we will show you the solution of how to display JSON data in HTML using Ajax, let us understand JSON first. The JSON stands for JavaScript Object Notation.

It is a lightweight data-exchanging format. It is used basically with API. JSON is language-independent and supports all kinds of frameworks and languages.

In this tutorial, we will display JSON data from the URL using AJAX or asynchronous JavaScript and XML. Using AJAX we can read or send data to a web server.

Step By Step Guide On How To Display JSON Data In HTML Using Ajax :-

Let’s see an example first.

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> how to display JSON data in HTML using ajax </title>
    <style>
        h1 {
         font-size : larger ;
         font-weight : bolder ;
         color : rgb(113, 221, 113) ;
         text-align : center ;
        }
        h3 {
            text-align : center ;
            font-family : 'Lucida Sans' , 'Lucida Sans Regular' , 'Lucida Grande' , 'Lucida Sans Unicode' , Geneva , Verdana , sans-serif ;
        }
table, tr,td,th{
        border: 1px solid black;
        }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
    <h1> TALKERSCODE </h1>
    <h3> how to display JSON data in HTML using ajax </h3>
    <table id="table" >
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
        </tr>
        <tbody id="myTable"></tbody>
    </table>
    <script>
        var myArray = []
        $.ajax({
            method:'GET',
            url:'https://reqres.in/api/users',
            success:function(response){
                myArray = response.data
                buildTable(myArray)
                console.log(myArray)
            }
        })
        function buildTable(data){
            var table = document.getElementById('myTable')
            for (var i = 0; i < data.length; i++){
                var row = `<tr>
                                <td>${data[i].first_name}</td>
                                <td>${data[i].last_name}</td>
                                <td>${data[i].email}</td>
                          </tr>`
                table.innerHTML += row
            }
        }
    </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page, create a <script> tag with a URL that is used to access the library of jQuery. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Into the <head> tag add a <script> tag with external link or using ajax.
  6. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  7. <h1> tag used to add heading here.
  8. Create a <table> with the id ‘table’, using the <th> tag to add the heading of the JSON file. Then create a <tbody> tag with the id ‘myTable’.
  9. Created the <style> tag to add CSS to the HTML page.
  10. To add JavaScript Create a <script> tag.
  11. Creating a variable array named ‘myArray’
  12. By $.ajax, mention the method ‘get’ and URL to the JSON file. Calling the buildTable() function into it and using console.log() to display the array.
  13. Now creating the buildTable(data) function. Creating a variable table of document.getElementById() by the id 'myTable’
  14. Create a for loop to print the data.

Conclusion :-

At last, here in conclusion, here we can say that with the help of this article, we are able to know how to display JSON data in HTML using ajax.

I hope this article on how to display JSON data in HTML using Ajax helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪