How to find the second key name in JSON using javascript
I have a JSON message
[{"Num": 10, "SubLoc": "Chamr"}, {"Num": 34, "SubLoc": "angala"}, {"Num": 4, "SubLoc": "avano"}]
How can I only find the second key value, i.e. SubLoc in the above case, using javascript or jquery? I don't want to hardcoded the value in the javascript object because the key value changes in every post.
EDIT: Below code can provide us with Key: Value data
jobs = [{"Num": 10, "SubLoc": "Chamr"}, {"Num": 34, "SubLoc": "angala"}, {"Num": 4, "SubLoc": "avano"}]
jobs.forEach(function(job) {
Object.keys(job).forEach(function(key) {
alert(key + ':' + job[key]);
});
});
I just need the data of the second key ie, "SubLoc" in this case.
+3
source to share
3 answers
Here is a jsfiddle for that
var dataJson = '[{"Num": "10", "SubLoc": "Chamr"}, {"Num": "34", "SubLoc": "angala"}, {"Num": "4", "SubLoc": "avano"}]';
var data = JSON.parse(dataJson);
for (var eachObj in data){
var keys = Object.keys(data[eachObj]);
alert(keys[1] + ' : ' + data[eachObj][keys[1]]);
}
0
source to share