3

i started to learn json and ajax and i have this json file:

{
  "data": {
    "children": [{
      "data": {
        "domain": "some.url.com"
      },
      {
        "data": {
          "domain": "another.url.com"
        }
      }
    }]
  }
}

How i get the first domain from this file?

I try:

$(document).ready(function(){
  $.getJSON("https://myurl.com/api.json", function(data){
    console.log(data.children[0].data.domain);
});
});

and it didn't work :(

1
  • 1
    Tipp: open devtools and you 'console.log(data)' to display the data.. Commented Sep 16, 2015 at 2:25

2 Answers 2

1

Look closely on your data and your code.

You've got data variable, then data object with children array so you can access it like that

data.data.children[0].data.domain
Sign up to request clarification or add additional context in comments.

Comments

1

Try like this:

$(document).ready(function(){
   $.getJSON("https://myurl.com/api.json", function(data){
   console.log(data.data.children[0].data.domain);
   });
 });

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.