V
V
vaskadogana2016-12-06 00:36:48
JavaScript
vaskadogana, 2016-12-06 00:36:48

How to count number of keys in json?

Json with multiple nesting levels.

names = JSON.parse(ch, function(key, value){
  var b = 0;
  if(key.length > 0){
    b = b+1;
  }
  console.log(b);
});

With this approach, in the console (chrome) you can see how many times the key length has been compared. Is it possible to extract this value somehow?
Question two)
how to jump to the third level with such a campaign
for(i in names){
    if( typeof names[i] === 'object'){
      names = names[i];
      len = len + Object.keys(names).length;
      console.log("len temp", len);	
    }
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2016-12-06
@miraage

var jsonString = "{... your json string ...}";
console.log(jsonString.match(/[^\\]":/g).length);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question