Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all key in JSON object (javascript)

{"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?

like image 986
Hikaru Shindo Avatar asked Nov 13 '14 05:11

Hikaru Shindo


People also ask

How do I get the key of a JSON object?

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.

How do I find the key name of an object?

Use object. keys(objectName) method to get access to all the keys of object.


2 Answers

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)) 
like image 89
Jeeva Avatar answered Sep 23 '22 23:09

Jeeva


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"]

like image 33
Rahul G Nair Avatar answered Sep 22 '22 23:09

Rahul G Nair