Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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?

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading