Alex Rivera | Logout

Javascript: Getting all existing keys in a JSON array

Asked 2012-03-19T08:43:16.687
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

Edit
Report

1 Answer

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).

answered 2012-03-19T09:56:24.570

Your Answer