15

I have a JSON array like below:

var jsonArray = [{"k1":"v1"},{"k2":"v2"},{"k3":"v3"},{"k4":"v4"},{"k5":"v5"}]

I don't know which keys does exists in this array. I want to get all the existing key from the array.

It should be possible something like this:

for(i=0;i<jsonArray.lenght;i++){
  // something like-  key = jsonArray[i].key
  // alert(key);
}

Please tell me the method or way to get all keys existing in Json array.

Regards

Gaurav Gandhi
  • 3,041
  • 2
  • 27
  • 40
S Singh
  • 1,403
  • 9
  • 31
  • 47

5 Answers5

19

Why don't you use a

var jsonObject = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4","k5":"v5"}

instead of your

var jsonArray = [{"k1":"v1"},{"k2":"v2"},{"k3":"v3"},{"k4":"v4"},{"k5":"v5"}]

? Then the solution would be so simple: Object.keys(jsonObject).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • When I try your solution Object.keys on a jsonObj I get an error message TypeError: object.keys() is not a funtion. – pranshus Jun 27 '13 at 14:58
  • `Object` is uppercase. Also, the function is not available in outdated browsers, you will have to [shim it there](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Compatibility) – Bergi Jun 27 '13 at 15:02
  • Thanks. It was not due to the case. It was because I was using IE 7. – pranshus Jul 01 '13 at 09:39
11

Try this:

var L = jsonArray.length;
for (var i = 0; i < L; i++) {
    var obj = jsonArray[i];
    for (var j in obj) {
        alert(j);
    }
}

I've also made some modifications of your current code (like length caching).

freakish
  • 54,167
  • 9
  • 132
  • 169
5

Loop through the object properties, and select the first "real" one (which given your data schema should be the only real one).

var jsonArray = [{"k1":"v1"},{"k2":"v2"},{"k3":"v3"},{"k4":"v4"},{"k5":"v5"}]

for (var i = 0; i < jsonArray.length; i++) {
    for (var prop in jsonArray[i]) {
        if (jsonArray[i].hasOwnProperty(prop)) {
            var key = prop;
            break;
        }
    }
    alert(key);
}

See How to loop through items in a js object? for an explanation of why it's important to use hasOwnProperty here.

Community
  • 1
  • 1
Ben Lee
  • 52,489
  • 13
  • 125
  • 145
4

Try this:

jsonArray.reduce(function(keys, element){ 
    for (key in element) {
       keys.push(key);
    } 
    return keys; 
},[]);

This should also work for multiple keys in the array objects.

If you're supporting old browsers that don't have reduce and map, then consider using a shim.

Sudhir Jonathan
  • 16,998
  • 13
  • 66
  • 90
1
var id = { "object": "page", "entry": [{ "id": "1588811284674233", "time": 1511177084837, "messaging": [{ "sender": { "id": "1393377930761248" }, "recipient": { "id": "1588811284674233" }, "timestamp": 1511177084553, "message": { "mid": "mid.$cAAX_9pLcfu1mCnGmiVf2Sxd2erI2", "seq": 1882, "text": "a" } }] }] };

    function getKey(obj, data) {
//@author dvdieukhtn@gmail.com
      var data = data || [];
      if (obj) {
        var keys = Object.keys(obj);
        for (var pos in keys) {
          console.log();
          data.push(keys[pos]);
          if ((obj[keys[pos]].constructor === Array)) {
            for (var i = 0; i < obj[keys[pos]].length; i++) {
              getKey(obj[keys[pos]][i], data);
            }
          }
          else if (obj[keys[pos]].constructor === Object) {
            getKey(obj[keys[pos]], data);
          }
        }
        return data;
      }
    }
    console.log(getKey(id));
ghost
  • 11
  • 1