I'm just starting to learn about APIs, JSON, and Jquery and I am stuck. How would I console log the following from my Jquery call -
name: "The Old Mill Cafe"
Here's my current code:
$(document).ready(function(){
$("#mainbutton").on("click", function() {
$.ajax({
url: "https://developers.zomato.com/api/v2.1/search?entity_id=Chicago%2C%20IL%20&entity_type=city",
headers: {
"X-Zomato-API-Key": "…"
},
method: "GET"
}).done(function(data) {
console.log(data);
});
});
});
Your console.log
is telling you that data
is an object with a property called restaurants
which is an array with a single entry. That entry is an object with a property called restaurant
which is an object with a property called name
. So:
console.log(data.restaurants[0].restaurant.name);
In this case you are getting list of restaurants which has attributes like name and so on.
To get one restaurant you have to get first element of data
data[0] this will give you first restaurant of the list.
Now you need to get the name of the first restaurant to do that
data[0].name
So to get the name of the first restaurant you have to use following
console.log(data[0].name);