ChuChuwi
ChuChuwi

Reputation: 1050

How to store JSON recieved from REST api service into a variable using Javascript

I am trying to get a JSON from https://api.nal.usda.gov/fdc/v1/foods/search?api_key=DEMO_KEY&query=ketchup and do something with it by storing it into a variable. I used Node and Express in my work. Can anyone please tell me how to. Very much appreciated, Thanks.

Upvotes: 0

Views: 528

Answers (1)

Treintaytres
Treintaytres

Reputation: 31

I asume that your server is working and fetching data correctly. Then you can use something like that:

async function getData() {
  return fetch('https://api.nal.usda.gov/fdc/v1/foods/search?api_key=DEMO_KEY&query=ketchup')
    .then(response => response.json())
}

getData()
  .then(data => console.log(data))

Like zinkn say here

Upvotes: 1

Related Questions