JSON Get all Keys from an Object

JSON Get all Keys from the Object

In the following example, we will get all keys of the first object in an array (data).

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <body> <h1>JSON Get all keys from the Object</h1> <button onclick="myFunction()">Click Me</button> <script> var data = [ { "name": "Mike", "age": 36, "married": true } ] function myFunction(){ console.log(Object.keys(data[0])) } </script> </body> </html>

Syntax

Object.key(objName)

JSON Get all Keys of objects in an Array

The following example has a complex JSON structure. In the real world web application, you quite often want to deal with such kind of data.

In the following example, we will get a list of friends of 'Mike'.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <body> <h1>JSON Get all keys from the Object</h1> <p>Get a list of friends of 'Mike'.</p> <button onclick="myFunction()">Click Me</button> <script> var data = [ { "name": "Mike", "age": 36, "friends": [{ "jade": { "gender": "female", "age": 25 }, "antony": { "gender": "male", "age": 35 } }] } ] function myFunction(){ console.log(Object.keys(data[0].friends[0])) } </script> </body> </html>

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author