How do I use d3.json() ? Data Visualization Projects - Visualize Data with a Bar Chart

Tell us what’s happening:

how do I use d3.json() ?

Problem

// The problem is on the next line or something.
let data = d3.json("https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json", function(d) {
    return d
})
console.log(data)

The line with console.log(data) always outputs {} instead of the data given.
Can someone tell me how to fix this?

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36

Challenge Information:

Data Visualization Projects - Visualize Data with a Bar Chart

Hello @khaledkanan!

Since the d3.json method is a Promise, you should call it with await. Also in your case, you can omit the second parameter.

const someData = await d3.json("example.json");

If you forget to put await, your program doesn’t wait until the data is fetched. So it returns an empty object.

Happy coding!

‘await’ is only allowed within async functions
How do I avoid this error?

make it an async function!

how ? characters to exceed the character limit

One of the projects in the JavaScript certification teaches asyncronous programming

where? ccvfdhnfvhgfshsdfh

You can look at the projects in the JavaScript certification, find the one with the right name and start it

It doesn’t work:


async function dataGet() {
  let data = await d3.json("https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json")
  return data
}
console.log(dataGet())

Returns: {}

P.S. I did not look at that reply you just sent.

Ilenia offered you some very helpful advice. Checking out a project dealing with promises and asynchronous programming when you’re stumped about them is a solid strategy. You get to learn what you need to before getting back to the current project.

Here’s a hint. The dataGet method is returning a promise which hasn’t been fulfilled yet. Hence why nothing is printing in the console.

P.S For the record, please don’t create another thread while actively ignoring helpful advice one of our staff members. I can understand being stumped. I can understand being confused and bumping the thread. Your response was very rude and I don’t appreciate it.

P.S I did not KNOW that reply existed before I replied.

And how do I fix that?

you need to await that too