All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Get JSON Data From URL In HTML

Last Updated : Mar 11, 2024

How To Get JSON Data From URL In HTML

In this article we will show you the solution of how to get JSON data from URL in HTML, 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 get JSON data from the URL using jQuery.

Step By Step Guide On How To Get JSON Data From URL In HTML :-

Let us see an example on get JSON data from a URL in HTML. Let us see the code 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 get JSON data from URL in HTML</title>
    <script src = " https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js ">
    </script>
    <style>
        h1 {
         font-size : larger ;
         font-weight : bolder ;
         color : lightgreen ;
        }
    </style>
</head>
<body>
    <h1> TALKERSCODE </h1>
    <p> how to get JSON data from URL in HTML </p>
    <script>
        var theURL = ' https://jsonplaceholder.typicode.com/comments '
                $.getJSON ( theURL , function ( data ) {
                console.log ( data )
                for( i = 0 ; i<data.length ; i = i + 1 ){
                    console.log ( data [ i ].name )
                }
        } )
        </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. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here.
  7. Create a <script> tag to add the JavaScript code to get JSON data from URL in HTML.
  8. We create a variable “ theURL “ using the var function.
  9. Into that variable “ theURL “ , we placed a URL containing the JSON data we are going to use.
  10. Using $.getJSON () to put the information into theURL to a function called function(data). The $ sign here used to access jQuery.
  11. Here , using console.log() to display the data consist in the URL.
  12. Also we can display particular data like name, email, etc. in it. Here we used a for loop to display all the Names in the JSON 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 get JSON data from URLs in HTML.

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

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪