How to get only keys from json object in javascript

This function should return an array of ALL the keys (i.e. the key names) in a JSON object including nested key/value pairs.

function get_all_json_keys(json_object, ret_array = []) {
    for (json_key in json_object) {
        if (typeof(json_object[json_key]) === 'object' && !Array.isArray(json_object[json_key])) {
            ret_array.push(json_key);
            get_all_json_keys(json_object[json_key], ret_array);
        } else if (Array.isArray(json_object[json_key])) {
            ret_array.push(json_key);
            first_element = json_object[json_key][0];
            if (typeof(first_element) === 'object') {
                get_all_json_keys(first_element, ret_array);
            }
        } else {
            ret_array.push(json_key);
        }
    }

    return ret_array
}

Using this function on the OP's original object

const op_object =
{
    "document":{
       "people":[
          {
             "name":[
                "Harry Potter"
             ],
             "age":[
                "18"
             ],
             "gender":[
                "Male"
             ]
          },
          {
             "name":[
                "hermione granger"
             ],
             "age":[
                "18"
             ],
             "gender":[
                "Female"
             ]
          }
       ]
    }
 }

var all_keys = [];

function get_all_json_keys(json_object, ret_array = []) {
    for (json_key in json_object) {
        if (typeof(json_object[json_key]) === 'object' && !Array.isArray(json_object[json_key])) {
            ret_array.push(json_key);
            get_all_json_keys(json_object[json_key], ret_array);
        } else if (Array.isArray(json_object[json_key])) {
            ret_array.push(json_key);
            first_element = json_object[json_key][0];
            if (typeof(first_element) === 'object') {
                get_all_json_keys(first_element, ret_array);
            }
        } else {
            ret_array.push(json_key);
        }
    }

    return ret_array
}

get_all_json_keys(op_object, all_keys);

console.log(all_keys);

should yield

[ 'document', 'people', 'name', 'age', 'gender' ]

Note: This will return a unique list of all key names.

Hello Friends 👋,

Welcome To Infinitbility! ❤️

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.

JavaScript object is created by key and value pair also is not like an array where we can use loops to get key and values.

Hence, JavaScript provide Object.keys(), Object.values(), and Object.entries() to solve the issue getting dependent on keys;

Today, we are going to use all the above three methods to retrieve keys and values both from objects.

All below tutorials work on javascript, React, React Native, Vue, Node, Deno, typescript, and all javascript frameworks.

Working with JSON String

As we know we use JSON strings to send and receive JSON to communicate between two technology.

So here, we will see how can we get key and value from json string.

To get the key and value from a JSON string you can use the JSON.parse() method it will convert the JSON string to a JSON object and you can easily get key and value.

Let’s see how we can parse a JSON string and get keys and values.

1const JSONStr = '{"a":"somestring", "b":42, "c":false}';

2const object1 = JSON.parse(JSONStr);

3

4

5console.log(object1.a)

6

7

8

9console.log(Object.keys(object1))

10

11

12

13console.log(Object.keys(object1).join())

14

15

16

17console.log(Object.values(object1))

18

19

20

21console.log(Object.values(object1).join())

22

JavaScript Object.keys()

JavaScript Object.keys() will return an array on object keys and you can use it to retrieve from the object.

1const object1 = {

2 a: 'somestring',

3 b: 42,

4 c: false

5};

6

7console.log(Object.keys(object1));

8

After getting the keys of the array you can use those on loops like the below example.

1const object1 = {

2 a: 'somestring',

3 b: 42,

4 c: false

5};

6

7for(let key of Object.keys(object1)){

8 console.log(key);

9}

10

11

12

13

JavaScript Object.values()

JavaScript Object.values() method will return an array of object values you can use on loop after getting values array.

1const object1 = {

2 a: 'somestring',

3 b: 42,

4 c: false

5};

6

7console.log(Object.values(object1));

8

after get values in the array, you have also option to use loop get value one by one.

1const object1 = {

2 a: 'somestring',

3 b: 42,

4 c: false

5};

6

7for(let value of Object.values(object1)){

8 console.log(value);

9}

10

11

12

13

JavaScript Object.entries()

JavaScript Object.entries() method will return separate array for every key value pair in object.

1const object1 = {

2 a: 'somestring',

3 b: 42

4};

5

6console.log(Object.entries(object1));

7

8

9

Now, you have the option to use a key value loop to get key and value at a time.

1const object1 = {

2 a: 'somestring',

3 b: 42

4};

5for (const [key, value] of Object.entries(object1)) {

6

7 console.log(`${key}: ${value}`);

8}

9

10

11

12

I hope it helps you, All the best 👍.

How to get key value from JSON JavaScript?

To get a JSON object's key and value in JavaScript, we can use the JSON. parse method to parse the JSON object string into a JavaScript object. Then we can get a property's value as we do with any other JavaScript object.

How to get keys from JSON object JS?

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 to get value from JSON object using key?

A JSONObject has few important methods to display the values of different types like getString() method to get the string associated with a key string, getInt() method to get the int value associated with a key, getDouble() method to get the double value associated with a key and getBoolean() method to get the boolean ...

Can JSON have only keys?

No, in JSON notation, an object is an unordered set of key/value pairs, and both the key and the value are required elements. That's perfectly valid JSON.