-3

I have a set of JSON object in a text file called text.json which looks like the following:

{
  "school": [
    {
      "student": {
        "name": "John",
        "lastname": "Ghram",
        "studentId": "000111"
      }
    },
    {
       "student": {
        "name": "Harry",
        "lastname": "Smith",
        "studentId": "000112"
     }
    },
    {
       "teacher": {
       "name": "Teacher One",
       "teacherId": 1001
      }
    }
  ]
}

The following code is use to read from file

var obj = (function () {
        var json = null;
        $.ajax({
            'async': false,
            'global': true,
            'url': "text.json",
            'dataType': "json",
            'Content-type': 'application/json',
            'success': function (data) {
                obj = data;
            }
        });
        return obj;
    })();

When the obj is returned to get the name of student name I am using obj.school[0].student['name']. is there a way to store all student information as one JSON object called students and the teacher to other called teachers, so I can access the info without using the index number. For Eg: student.name.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
usrNotFound
  • 2,680
  • 3
  • 25
  • 41
  • 1
    possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Mritunjay Sep 08 '15 at 02:36
  • You have multiple students. Somehow you have to indicate which one to access. They way you claim to access the data doesn't match the data. Also, learn how asynchronous calls work. – Felix Kling Sep 08 '15 at 02:37
  • 1
    @Mritunjay: No, that has nothing to do with that. – Felix Kling Sep 08 '15 at 02:38
  • Just curious - this would be fairly easy if you are using lodash or underscore. Are you? – Mark C. Sep 08 '15 at 02:38
  • 1
    That isn't valid JSON. You need to show us real data if you want a useful answer. – user229044 Sep 08 '15 at 02:43

3 Answers3

3

No, this is exactly what arrays are for. You cannot store multiple instances of the name property on one object, that makes no sense. How would student.name possibly resolve to a single name, if student represented multiple students?

user229044
  • 232,980
  • 40
  • 330
  • 338
1

You just need to new an array to store them, you can write a function like this

function getStudents(obj)
{
    var res = new Array() ;
    var school = obj['school'] ;
    for(item in school)
    {
        for(q in school[item])
        {
            if(q=="student")//you can change it to "teacher"
            {
                res.push(school[item]) ;
            }
        }
    }
    console.log(res) ;
    return res ;//return an array(object) of students;
}
getStudents(obj) ;
Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
1

I've formatted your json and then applied a for loop to find object keys that match 'student' and then augmented the students object with the name.

If two names are the same, they'll be overwritten, so this isn't the best approach:

var obj = {
    "school": [ 
      {"student": { "name":"John",  "lastname": "Ghram", "studentId": 000111 }},  
      {"student": { "name": "Harry","lastname": "Smith", "studentId": 000112 }},
      {"teacher": { "name": "Teacher One", "teacherId": 1001 }}
    ]
};

var i, arr = obj.school, len = arr.length;
var students = Object.create(null);

for(i=0; i<len; i++) {
    if(Object.keys(arr[i])[0] === 'student') students[arr[i].student.name] = arr[i];
}

Can now use square-bracket notation:

students['John'], students['Harry'], etc
Data
  • 1,337
  • 11
  • 17