1

I am working with facebook JS SDK which returns user's information in JSON format. I know how to get the response like response.email which returns email address. But how to get an element from a nested array object? Example: user's education history may contain multiple arrays and each array will have an element such as "name" of "school". I want to get the element from the last array of an object.

This is a sample JSON I got:-

"education": [
{
  "school": {
    "id": "162285817180560",
    "name": "Jhenaidah** School"
  },
  "type": "H**hool",
  "year": {
    "id": "14404**5610606",
    "name": "2011"
  },
  "id": "855**14449421"
},
{
  "concentration": [
    {
      "id": "15158**968",
      "name": "Sof**ering"
    },
    {
      "id": "20179020**7859",
      "name": "Dig**ty"
    }
  ],
  "school": {
    "id": "10827**27428",
    "name": "Univer**g"
  },
  "type": "College",
  "id": "9885**826013"
},
{
  "concentration": [
    {
      "id": "108196**810",
      "name": "Science"
    }
  ],
  "school": {
    "id": "2772**996993",
    "name": "some COLLEGE NAME I WANT TO GET"
  },
  "type": "College",
  "year": {
    "id": "1388*****",
    "name": "2013"
  },
  "id": "8811215**16"
}]

Let's say I want to get "name": "some COLLEGE NAME I WANT TO GET" from the last array. How to do that with Javascript? I hope I could explain my problem. Thank you

Julfikar
  • 1,353
  • 2
  • 18
  • 35

4 Answers4

2

If your json above was saved to an object called json, you could access the school name "some COLLEGE NAME I WANT TO GET" with the following:

json.education[2].school.name
2

If you know where that element is, then you can just select it as already mentioned by calling

var obj = FACEBOOK_ACTION;
obj.education[2].school.name

If you want to select specifically the last element, then use something like this:

obj.education[ obj.education.length - 1 ].scool.name
lumio
  • 7,428
  • 4
  • 40
  • 56
  • Hi, thanks for your quick reply. But I have a confusion. what is FACEBOOK_ACTION here? – Julfikar Mar 11 '17 at 22:18
  • Well it is where your JSON data is stored. I just picked a random variable name where it might be stored :) Where is your JSON stored right now, then I will rewrite my answer for you – lumio Mar 11 '17 at 22:19
  • Oh thank you. My json starts like this:- { "id": "9040**1539", "name": "Mohammad Julfikar Mahmud", "email": "itsm**ail.com", "birthday": "02/10/19**", "education": [ { "school": { "id": "1622**0560", "name": "J***chool" }, – Julfikar Mar 11 '17 at 22:22
  • But how is the variable called? How do you call that JSON? – lumio Mar 11 '17 at 22:25
  • Sorry I didn't get it. This is the response that facebook returns :/ – Julfikar Mar 11 '17 at 22:27
  • I know, that is what Facebook returns. But how do you know, that facebook returns it? Where do you call it? Or are you just testing it on their SDK/Dev website? – lumio Mar 11 '17 at 22:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137858/discussion-between-lumio-and-mohammad-julfikar). – lumio Mar 11 '17 at 22:31
2

Here is a JsFiddle Example

var json = '{}' // your data;

// convert to javascript object:
var obj = JSON.parse(json);

// get last item in array:
var last = obj.education[obj.education.length - 1].school.name;
// result: some COLLEGE NAME I WANT TO GET
Matt D. Webb
  • 3,216
  • 4
  • 29
  • 51
0

Try this,

if (myData.hasOwnProperty('merchant_id')) {
    // do something here
}

where JSON myData is:

{
    amount: "10.00",
    email: "someone@example.com",
    merchant_id: "123",
    mobile_no: "9874563210",
    order_id: "123456",
    passkey: "1234"
}

This is a simple example for your understanding. In your scenario of nested objects, loop over your JSON data and use hasOwnProperty to check if key name exists.

Hamza Rashid
  • 1,329
  • 15
  • 22