I'm extracting data from a food recipe api and the response is JSON.
{
"results": [
{
"id": 147605,
"title": "Mac and Cheese",
"image": "https://spoonacular.com/recipeImages/147605-312x231.png",
"imageType": "png"
},
{
"id": 137592,
"title": "Mac and Cheese",
"image": "https://spoonacular.com/recipeImages/137592-312x231.png",
"imageType": "png"
},
{
"id": 760335,
"title": "Mac and Cheese",
"image": "https://spoonacular.com/recipeImages/760335-312x231.jpg",
"imageType": "jpg"
},
{
"id": 1047165,
"title": "Mac and Cheese",
"image": "https://spoonacular.com/recipeImages/1047165-312x231.jpg",
"imageType": "jpg"
}
],
"offset": 0,
"number": 4,
"totalResults": 1171
}
I want to extract the title, id, and image. I did:
request(url, (error, response) => {
console.log(response.body.results["id"])
console.log(response.body.results["title"])
console.log(response.body.results["image"])
})
I will want to store them in variables and do other stuff with it, but for now I just want to print it out to the terminal.
I'm getting undefined for all the logs. I'm not sure if I should be doing some sort of loop, or something of that nature.
I should also add, when I do:
console.log(response.body.results) // outputs undefined
console.log(response.body) // outputs the JSON response
So this must mean I'm accessing the results array incorrectly.
Thanks for the help!