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

3

4 Answers 4

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

3 Comments

the json I shared, was just a part of another json. how can I split it like you did in jsfiddle?
@MohammadJulfikar Create a Fork of the JsFiddle and update the JSON, then paste the link here, I will take a look! :-)
@MohammadJulfikar awesome! :-) Glad I could help.
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 Comments

that 'education' is inside an array
The syntax is similar. If your json object is the first element of an array named arr, you can access the school name like this: arr[0].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

7 Comments

Hi, thanks for your quick reply. But I have a confusion. what is FACEBOOK_ACTION here?
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
Oh thank you. My json starts like this:- { "id": "90401539", "name": "Mohammad Julfikar Mahmud", "email": "itsmail.com", "birthday": "02/10/19**", "education": [ { "school": { "id": "16220560", "name": "J*chool" },
But how is the variable called? How do you call that JSON?
Sorry I didn't get it. This is the response that facebook returns :/
|
0

Try this,

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

where JSON myData is:

{
    amount: "10.00",
    email: "[email protected]",
    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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.