Home > Net >  How to print a dataset from a url using d3.json() javascript?
How to print a dataset from a url using d3.json() javascript?

Time:12-29

I'm trying to print a dataset in the console with this code

d3.json("data.json", function(data) {
    console.log(data);
});

I'm using replit and data.json is one of the files. I downloaded it from this url: https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json

I called the d3js library in the head tag like that:

<script src="https://d3js.org/d3.v6.min.js"></script>

It's not printing though. No error messages are shown. It just does not print. What is wrong?

CodePudding user response:

The signature of d3.json() has changed since d3 v5 or later. Please use Promise style event handling now. Here is an example:

<html>
    <head><meta charset="UTF-8"></head>
    <body>
        <script src="https://d3js.org/d3.v6.min.js"></script>
        <script>
            let url = "https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json";
            d3.json(url).then(data => {
                console.log("got data", data);
            }).catch(error => {
                console.log("error fetching url", url);
            });
        </script>
    </body>
</html>
  • Related