2

I have got a task to iterate through the complex json file that contains json array. I could not access the array object from the json file.

I need to access the particularly the class-name object from the json file.

classdetail.json

[ [ {   "student" : [ 
     {
     "name" : "AAaa",
     "class-name" : "A",
     "grade-label" : "AA"   }, 
     {
     "name" : "AAbb",
     "class-name" : "A",
     "grade-label" : "AB"   }, 
     {
     "name" : "AAcc",
     "class-name" : "A",
     "grade-label" : "AB"   }, 
     {
     "name" : "AAdd",
     "class-name" : "B",
     "grade-label" : "AA"   } ],  
      "Average" : 2.5 },

      {   
     "student" : [ 
      {
     "name" : "BBaa",
     "class-name" : "B",
     "grade-label" : "AB"   }, 
      {
     "name" : "BBbb",
     "class-name" : "B",
     "grade-label" : "AA"   }, 
      {
     "name" : "BBcc",
     "class-name" : "B",
     "grade-label" : "AA"   }, 
      {
     "name" : "BBdd",
     "class-name" : "B",
     "grade-label" : "AA"   } ],   
      "Average" : 2.5 } ] ]

iterate.js

var fs = require('fs');
var express = require('express');
var http = require('http');
var publicApis;
var item;
var subItem;


classmem = JSON.parse(fs.readFileSync("classdetail.json", "utf8"));



for (item in classmem) {
  for (subItem in classmem[item]) {
     console.log(classmem[item][subItem]);
  }
}
3
  • 2
    What's the error you're getting? Commented Nov 2, 2015 at 13:35
  • Everything works fine. classmem[item][subItem].student is a student array. Commented Nov 2, 2015 at 13:42
  • im not getting any error but i cant able access the particular json object Commented Nov 3, 2015 at 6:32

3 Answers 3

2
for (item in classmem) {
  for (subItem in classmem[item]) {
     var student = classmem[item][subItem].student;
     for (row in student) {
       console.log(student[row]['class-name']);
     }
  }
}

But read about Array.forEach.

Sign up to request clarification or add additional context in comments.

2 Comments

If i change the array name student-data-details instead of student im getting error as "data not defined" ** el.student-data-details.forEach(function(e, i, a)**
You cannot use a minus sign in variable name. Now JS solve this as (el.student) - (data) - (details.forEach....)
0

for...in iterates over object properties in arbitrary order. It might not be what you want to use for an array, which stores items in a well-defined order. (though it should work in this case)

Try Array.forEach():

// iterate over `classmem`
classmem.forEach(function(element, index, array) {
  // iterate over classmem[index], which is an array too
  element.forEach(function(el, idx, arr) {
    // classmem[index][idx] contains objects with a `.student` property
    el.student.forEach(function(e, i, a) {
      console.log(e["name"], e["class-name"], e["grade-label"]);
    });
  });
});

1 Comment

If i change the array name student-data-details instead of student im getting error as "data not defined" ** el.student-data-details.forEach(function(e, i, a)**
0

first check the value is array then access to the "class-name" value

for (item in classmem) {
    for (subItem in classmem[item]) {
        **if (typeof classmem[item][subItem] === 'object') {
            classmem[item][subItem].forEach(function (val, ind) {
                console.log(val['class-name']);
            });
        }**
    }
}

1 Comment

im getting this error "TypeError: Object #<Object> has no method 'forEach'"

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.