You can check whether a key exists in a JSON object using several JavaScript methods. Common approaches include Object.hasOwn(), the in operator, Object.keys(), and Object.entries().
- Object.hasOwn() checks whether a property belongs directly to an object.
- The in operator checks both own and inherited properties.
- Object.keys() and Object.entries() can search through an object's properties.
- Nested objects can be searched using recursion.
[Approach 1]: Using Object.hasOwn()
The Object.hasOwn() method checks whether the specified property exists directly on an object.
Example: This example checks whether the prop_1 key exists in the object.
const obj = {
prop_1: "val_1",
prop_2: "val_2",
prop_3: "val_3",
prop_4: "val_4"
};
const key = "prop_1";
if (Object.hasOwn(obj, key)) {
console.log(`The key "${key}" exists.`);
} else {
console.log(`The key "${key}" does not exist.`);
}
Output
The key "prop_1" exists.
[Approach 2]: Using the in Operator
The in operator checks whether a property exists in an object. Unlike Object.hasOwn(), it also checks properties inherited through the prototype chain.
Example: This example checks whether the age key exists in the object.
const obj = {
name: "Joy",
age: 25,
city: "New Delhi"
};
if ("age" in obj) {
console.log('The key "age" exists in the object.');
} else {
console.log('The key "age" does not exist in the object.');
}
Output
The key "age" exists in the object.
[Approach 3]: Using Object.keys()
The Object.keys() method returns an array containing an object's own enumerable property names. The includes() method can then check whether a specific key is present.
Example: This example checks for the age key.
const obj = {
name: "Alen",
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.
[Approach 4]: Using Object.getOwnPropertyNames()
The Object.getOwnPropertyNames() method returns an array containing all own property names of an object, including non-enumerable properties.
Example: This example checks whether key2 exists in the object.
const obj = {
key1: "value1",
key2: "value2",
key3: "value3"
};
const key = "key2";
if (Object.getOwnPropertyNames(obj).includes(key)) {
console.log(`${key} exists in the object.`);
} else {
console.log(`${key} does not exist in the object.`);
}
Output
key2 exists in the object.
[Approach 5]: Using Object.entries()
The Object.entries() method returns an array of key-value pairs. You can use the some() method to check whether a specific key exists.
Example: Checks whether the city key exists.
const obj = {
name: "Noddy",
age: 24,
city: "Delhi"
};
const key = "city";
if (Object.entries(obj).some(([currentKey]) => currentKey === key)) {
console.log(`${key} exists in the object.`);
} else {
console.log(`${key} does not exist in the object.`);
}
Output
city exists in the object.
[Approach 6]: Using an undefined Check
You can check whether accessing a property returns undefined. However, this approach cannot distinguish between a missing property and an existing property whose value is explicitly undefined.
const obj = {
name: "Jenny",
age: 30,
city: "Agra"
};
if (obj.name !== undefined) {
console.log("The 'name' key exists!");
}
if (obj.address === undefined) {
console.log("The 'address' key does not exist.");
}
Output
The 'name' key exists! The 'address' key does not exist.
[Approach 7]: Checking Keys in Nested Objects
For nested JSON objects, a recursive function can check whether a key exists at any level.
Example: Searches for the city key inside a nested object.
const user = {
personal: {
name: "Ron",
age: 30
},
address: {
city: "Kolkata",
pin: "700001"
}
};
function checkKeyInNestedObj(object, key) {
return Object.keys(object).some(currentKey =>
currentKey === key ||
(
object[currentKey] !== null &&
typeof object[currentKey] === "object" &&
checkKeyInNestedObj(object[currentKey], 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.
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. |