Open In App

Check if a key exists inside a JSON object - JavaScript

Last Updated : 21 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Below are some methods to check if a key exists inside a JSON object:

1. Using hasOwnProperty() Method

JavaScript hasOwnProperty() Method returns a boolean denoting whether the object has the defined property as its own property (as opposed to inheriting it).

Syntax

obj.hasOwnProperty(prop);
JavaScript
let obj = {
    prop_1: "val_1",
    prop_2: "val_2",
    prop_3: "val_3",
    prop_4: "val_4",
};
function gfg_Run() {
    ans = "";
    let prop = 'prop_1';
    if (obj.hasOwnProperty(prop)) {
        ans = "let 'obj' has " + prop + " property";
    } else {
        ans = "let 'obj' has not " + prop + " property";
    }
    console.log(ans);
}
gfg_Run()

Output
let 'obj' has prop_1 property

In this example

  • Object: obj has properties like prop_1, prop_2, etc.
  • Function: gfg_Run() checks if obj has the property prop_1.
  • Result: If prop_1 exists, it logs: "let 'obj' has prop_1 property".

2. Using in Operator

JavaScript in Operator is an inbuilt operator which is used to check whether a particular property exists in an object or not. It returns a boolean value true if the specified property is in an object, otherwise, it returns false.

Syntax

prop in object
JavaScript
let obj = {
    name: 'Jiya',
    age: 25,
    city: 'New Delhi'
};
if ('age' in obj) {
    console.log('The key "age" exists in the JSON object.');
} else {
    console.log('The key "age" does not exist in the JSON object.');
}

Output
The key "age" exists in the JSON object.

In this example

  • Object: obj contains keys: name, age, and city.
  • Check: The code checks if the key 'age' exists in obj.
  • Result: If 'age' exists, it logs, "The key 'age' exists in the JSON object." If not, it logs, "The key 'age' does not exist in the JSON object."

3. Using Object.getOwnPropertyNames() and includes() Method

The Object.getOwnPropertyNames() method in JavaScript is a standard built-in object which returns all properties that are present in a given object except for those symbol-based non-enumerable properties.

Syntax

Object.getOwnPropertyNames(obj);
JavaScript
const json = {
    key1: 'value1',
    key2: 'value2',
    key3: 'value3'
};

const key = 'key2';

if (Object.getOwnPropertyNames(json).includes(key)) {
    console.log(`${key} exists in the JSON object.`);
} else {
    console.log(`${key} does not exist in the JSON object.`);
}

Output
key2 exists in the JSON object.

In this example

  • Object: jsonObject has keys: key1, key2, key3.
  • Check: It checks if key2 exists in the object.
  • Method: Uses Object.getOwnPropertyNames() to get all keys and checks if keyToCheck is in that list.
  • Output: If key2 exists, it logs, "key2 exists in the JSON object." If not, it logs, "key2 does not exist in the JSON object."

4. Using undefined Check

We can also check if a key exists in a JSON object by checking if its value is undefined. If the key doesn’t exist, accessing it will return undefined.

Syntax:

object.key === undefined
JavaScript
let obj = {
    name: "Jenny",
    age: 30,
    city: "Agra"
};

if (obj.name !== undefined) {
    console.log("The 'name' key exists!");
} else {
    console.log("The 'name' key does not exist.");
}

if (obj.address !== undefined) {
    console.log("The 'address' key exists!");
} else {
    console.log("The 'address' key does not exist.");
}

Output
The 'name' key exists!
The 'address' key does not exist.

In this example

  • If obj.name !== undefined, it means the name key exists.
  • If obj.address !== undefined, it means the address key does not exist.

5. Using Object.keys()

Object.keys() returns an array of the object's own enumerable property names. We can check if a key exists by using .includes() to search for the key in the array.

Syntax

Object.keys(object)
JavaScript
const obj = {
    name: 'Amit',
    age: 28,
    city: 'Mumbai'
};

const key = 'age';

if (Object.keys(obj).includes(key)) {
    console.log(`${key} exists in the object.`);
} else {
    console.log(`${key} does not exist in the object.`);
}

Output
age exists in the object.

In this example

  • The Object.keys(person) method returns ['name', 'age', 'city'], and since 'age' is in the array
  • The message "age exists in the object." is logged.

6. Using Object.entries()

Object.entries() returns an array of [key, value] pairs. we can check if the key exists by using .some() to search for the key.

Syntax

Object.entries(object)
JavaScript
const obj = {
    name: 'Neha',
    age: 24,
    city: 'Delhi'
};

const key = 'city';

if (Object.entries(obj).some(entry => entry[0] === key)) {
    console.log(`${key} exists in the object.`);
} else {
    console.log(`${key} does not exist in the object.`);
}

Output
city exists in the object.

In this example

  • Object.entries(person) returns [["name", "Neha"], ["age", 24], ["city", "Delhi"]], and since "city" is found.
  • The message "city exists in the object." is logged.

7. Checking for a Key in Nested JSON

To check for a key in nested objects, a recursive function is needed. It checks if the key exists at any level in the object.

Syntax

function checkKeyInNestedObj(object, key) {
return Object.keys(object).some(k =>
k === key || (typeof object[k] === 'object' && checkKeyInNestedObj(object[k], key))
);
}

Now let's understand this with the help of example

JavaScript
const user = {
    personal: {
        name: 'Raj',
        age: 30
    },
    address: {
        city: 'Kolkata',
        pin: '700001'
    }
};

function checkKeyInNestedObj(object, key) {
    return Object.keys(object).some(k =>
        k === key || (typeof object[k] === 'object' && checkKeyInNestedObj(object[k], key))
    );
}

const key = 'city';

if (checkKeyInNestedObj(user, key)) {
    console.log(`${key} exists in the nested object.`);
} else {
    console.log(`${key} does not exist in the nested object.`);
}

Output
city exists in the nested object.

In this example

  • The function checks if 'city' exists in the user object. Since 'city' is found in the nested address object.
  • The message "city exists in the nested object." is logged.

8. Using JSON.stringify()

We can use JSON.stringify() to convert an object to a string and check if a key exists as a substring. This method is not ideal because it can lead to false positives if the key appears as part of a value.

Syntax

JSON.stringify(object)
JavaScript
const obj = {
    name: 'Priya',
    age: 26,
    city: 'Chennai'
};

const key = 'age';

if (JSON.stringify(obj).includes(`"${key}"`)) {
    console.log(`${key} exists in the object.`);
} else {
    console.log(`${key} does not exist in the object.`);
}

Output
age exists in the object.

In this example

  • JSON.stringify(person) converts the object to the string {"name":"Priya","age":26,"city":"Chennai"}, and since "age" is present as part of the string, it logs "age exists in the object."
  • This method is quick but not always accurate.

When to Use Different Methods to Check Key Existence in a JSON Object

Methods

When to use

hasOwnProperty()

Use when you need to check direct properties (not inherited from prototype chain).

in Operator

Use when you want to check if the property exists anywhere in the object or its prototype chain.

Object.getOwnPropertyNames()

Use when you need to get a list of own properties and check if a key exists among them.

undefined Check

Use for simple checks to see if a key doesn’t exist in an object.

Object.keys()

Use for checking keys in flat objects (not nested).

Object.entries()

Use when you need to work with both keys and values, or if you need the pair itself.

checkKeyInNestedObj() (Recursive)

Use for nested or deeply structured JSON objects, where the key might be within sub-objects.

JSON.stringify()

Use for quick checks but not reliable when accuracy is required. Useful when dealing with small objects.

Conclusion

In JavaScript, checking if a key exists in a JSON object can be done using various methods, each with its own use case. Whether we need to check direct properties, keys in nested objects, or work with both keys and values, there’s a method tailored to your need. Choosing the right method depends on the structure of your JSON and the complexity of your data.


Similar Reads