Say I have JSON data as below:
var db=[
{"name":"John Doe","age":25},
{"name":"John Doe 2","age":21}
]
I know how to retrieve data from the JSON object using js/jquery. What I need is to get the value of the labels, i.e. I want to retrieve the "name" and the "age" from the object. I don't actually want to retrieve the values "John Doe" and 25 (I can already do that), instead I want the labels.
Say the JSON object has about 5 data with 10 fields each. I need to display the JSON in table form dynamically which I can already do. Since the JSON may be any set of values, I won't know what label to put in the <th></th>
cells of the table. So I need a way to get the labels.
Using a pseudo code:
var db=[
{"name":"John Doe","age":25},
{"name":"John Doe 2","age":21}
]
for i in db{
for j in db[i]{
console.log(db[i][j].label+":"db[i][j])
}
}
//db[i][j].label doesn't really work
The output should be:
name: John Doe
age: 25
name: John Doe
age: 21
Is there a JavaScript or jQuery function to do so or some other method to retrieve the required data?