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']);
    }
  }
});
3
  • obj is key. Iterate over data.contacts and get name from it. Commented Feb 7, 2017 at 7:12
  • 2
    obj.contacts.map(function(item) {return item.name}); Commented Feb 7, 2017 at 7:13
  • Possible duplicate of Access / process (nested) objects, arrays or JSON Commented Feb 7, 2017 at 7:16

2 Answers 2

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);
  }
});
Sign up to request clarification or add additional context in comments.

Comments

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);
    });
  }
});

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.