17
$.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.

4
  • 7
    Stop guessing, take console.log(data); and see what's actually in the data Commented Jun 1, 2012 at 0:09
  • where do i see the logs? in the browser? Commented Jun 1, 2012 at 0:26
  • F12 (in chrome) or firebug (in ff), Console tab Commented Jun 1, 2012 at 0:34
  • hoi, btw. this will not work, because there is one bracket ) to much. After the name. All the best but thanks for the question Commented Mar 29, 2018 at 8:00

1 Answer 1

40

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.