Fernando
Fernando

Reputation: 97

Get json array value with ajax

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 :(

Upvotes: 3

Views: 5862

Answers (2)

Sumanta736
Sumanta736

Reputation: 705

Try like this:

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

Upvotes: 1

Elon Than
Elon Than

Reputation: 9775

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

Upvotes: 1

Related Questions