2

I have a json which is . I just want to get specific data which is

obj['contacts']['name']

How can i get

obj['contacts']['name']

name on Contacts array

enter image description here

This is my code:

$.ajax({
  type: 'GET',
  dataType: 'json',
  url: uri,
  cache: false,
  contentType: 'application/json',
  success: function(data) {
    for (var obj in data) {
      console.log(obj['contacts']['name']);
    }
  }
});
ar em
  • 434
  • 2
  • 5
  • 18
  • `obj` is _key_. Iterate over `data.contacts` and get `name` from it. – Tushar Feb 07 '17 at 07:12
  • 2
    `obj.contacts.map(function(item) {return item.name});` – Jan Legner Feb 07 '17 at 07:13
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – JJJ Feb 07 '17 at 07:16

2 Answers2

1

Just enumerate the returned object "contacts" property:

$.ajax({
  type: 'GET',
  dataType: 'json',
  url: uri,
  cache: false,
  contentType: 'application/json',
  success: function(data) {
    data.contacts.forEach(function(contact) {
      console.log(contact.name);
    });
  }
});
wayofthefuture
  • 8,339
  • 7
  • 36
  • 53
1

In your case this is how you want get name from contacts

$.ajax({
  type: 'GET',
  dataType: 'json',
  url: uri,
  cache: false,
  contentType: 'application/json',
  success: function(data) {
    if (!data.contacts) return;
    var names = data.contacts.map(function(dt) {
      return dt.name;
    });
    console.log(names);
  }
});
Parvez Rahaman
  • 4,269
  • 3
  • 22
  • 39