16
$.getJSON('http://23.21.128.153:3000/api/v1/holidays', function(data){
        alert("this: " + data.holiday[0].name);
 });

I'm trying to access the "name" attribute of the first element of my JSON response but without success, can anyone tell me what I'm doing wrong.

Community
  • 1
  • 1
Luis D Urraca
  • 2,024
  • 4
  • 24
  • 46

1 Answers1

38

Try this:

data[0].holiday.name

The data looks like this:

[
  {
    "holiday":{
      "id":1,
      "date":"2012-05-01",
      "name":"Dia del trabajo",
      "description":"",
      "country_id":1,
      "moved_date":"2012-04-30"
    }
  },
  {
    "holiday":{...}
  },
...]

So, you need to select the first element from the main array (data[0]), then get its holiday property (data[0].holiday), and then get its name property.

cambraca
  • 27,014
  • 16
  • 68
  • 99