How to create a chart from JSON data using Fetch API in JavaScript?

In this article, we are going to explore on how to create a chart after fetching the JSON data. To fetch JSON data we use fetch() method of Fetch API. We will first fetch the data and once the data is available we will feed it into the system to create a chart. The Fetch API provides a simple interface for accessing and manipulating HTTP requests and responses.


const response = fetch(resource [, init])


  • resource − This is the resource path from where the data is fetched.

  • init − It defines any extra options such as headers, body, etc.


The steps can be defined as below −

Step 1 − We will fetch the data from some remote server by calling the fetch function.

Step 1 − Once the data is available we will feed it into the system.

Step 3 − With the help of the Chart JS library we will form the chart for this.

Example #1

In the below example, we fetch the data from the remote server and then create the desired chart. The US population data is fetched from the server.

# index.html

 Live Demo

<!DOCTYPE html>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <script type="text/javascript" src=""></script>
   <script src="">
   <title>Population Chart</title>
   <h1 style="color: green;">
      Welcome To Tutorials Point
   <div style="width: 800, height: 600">
      <canvas id="bar-chart">
   async function getData() {
      const response = await fetch('');
      const data = await response.json();
      length =;
      labels = [];
      values = [];
      for (i = 0; i < length; i++) {
      new Chart(document.getElementById("bar-chart"), {
         type: 'bar',
         data: {
            labels: labels,
            datasets: [
                  label: "Population (millions)",
                  backgroundColor: ["#3a90cd",
                  data: values
         options: {
            legend: { display: false },
            title: {
               display: true,
               text: 'U.S population'


On successful execution of the above program, it will generate a bar chart the of U.S. population. You can hover over the bar to see the population for a particular year. It can also be seen in the below gif image

Updated on: 21-Apr-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started