Q:

JSON Own Property Name

I need some help with JavaScript. The Problem is that i have a JSON data similar as show in follow,but the problem is that i am not able to access all the objects. With my function i am able to access ["x","y","z"], but i am not able to get others "p,q,r.k,a".Can anyone help me to get this thing fixed, please dont use Object.Keys().I dont know how to iterate inside Json Data. 

var Objectkeys = function(obj){
    a = []
    for(var prop in obj){
    if(obj.hasOwnProperty(prop)){
        a.push(prop)
        }
    }
    return a;
};
var obj = {
    x: 1,
    y: 2,
    z: {
        p: "Hello",
        q: "Master",
        r: {
            k: "Rotten",
            a: "apple"
        }
    }
};
window.onload = function () {
console.log(Objectkeys(obj));
//console.log(Object.keys(obj));
}

Thank you

A:

If grabbing the object keys is all you're after you can use a recursive function:
function getKeys(obj) {
  var arr = [];
  (function loop(obj) {
    for (var k in obj) {
      if (obj.hasOwnProperty(k)) {
        arr.push(k);
        if (typeof obj[k] === 'object') {
          loop(obj[k]);
        }
      }
    }
  }(obj));
  return arr;
}

console.log(getKeys(obj)); // ["x", "y", "z", "p", "q", "r", "k", "a"]

Demo.

