{"document": {"people":[ {"name":["Harry Potter"],"age":["18"],"gender":["Male"]}, {"name":["hermione granger"],"age":["18"],"gender":["Female"]}, ]} }
From this JSON example, I would like to get the keys such as name
, age
, gender
for each people.
How to do this?
To get key and value from json object in javascript, you can use Object. keys() , Object. values() , for Object. entries() method the methods helps you to get both key and value from json object.
Use object. keys(objectName) method to get access to all the keys of object.
I use Object.keys
which is built into JavaScript Object, it will return an array of keys from given object MDN Reference
var obj = {name: "Jeeva", age: "22", gender: "Male"} console.log(Object.keys(obj))
Try this
var s = {name: "raul", age: "22", gender: "Male"} var keys = []; for(var k in s) keys.push(k);
Here keys array will return your keys ["name", "age", "gender"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With