0

My JSON

[
 {
   "container":         
  [
    null,null,null,null,null
  ],

   "type":
  [
  "text","text","text","text","text"
  ],

   "role":
  [
    null,null,null,null,null
  ],

  "subtype":
  [
   null,null,null,null,null
  ],

  "maxlength":
  [
   null,null,ull,null,null

  ]

 }
]

My Code

for(i=0; i<jsonString.length; i++){
        container = jsonString[i];
        console.log(container);
        for(j in container){
            console.log(container[j]);
        }
    }

Thia isn't giving me the desired result. I know there a lot of these type of question, but I have applied several of them but to no avail.Thanks for the help.

iamklaus
  • 3,720
  • 2
  • 12
  • 21

2 Answers2

0

You need this instead:

var keys = Object.keys(jsonString[0]);
keys.forEach(key => {
  container = jsonString[0][key];
  console.log(container);
  container.forEach(j => {
    console.log(j);
  });
});

Edit:

Backwards compatible syntax:

var keys = Object.keys(jsonString[0]);
for (var i = 0; i < keys.length; i++) {
  container = jsonString[0][keys[i]];
  console.log(container);
  for (var j = 0; j < container.length; j++) {
    console.log(container[j]);
  }
}
0

Try this.... If you just want to print the values of each level of the given format JSON.

 yourJSON.forEach(function(e)
    { 
      container = Object.keys(e)
      console.log(container)
      container.forEach(function(key){
        containerValue = e[key]
        console.log(containerValue)
        containerValue.forEach(function(value){
          console.log(value)
        })
      })
    })
Parmatma
  • 191
  • 10