Check if a key exists inside a JSON object - JavaScript
Last Updated :
21 Jun, 2025
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()
Outputlet '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.');
}
OutputThe 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.`);
}
Outputkey2 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.");
}
OutputThe '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.`);
}
Outputage 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.`);
}
Outputcity 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.`);
}
Outputcity 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.`);
}
Outputage 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
JavaScript Check if a key exists inside a JSON object 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). Syntaxobj.hasOwnProperty(prop);JavaScript
6 min read
How to Check a Key Exists in JavaScript Object? Here are different ways to check a key exists in an object in JavaScript.Note: Objects in JavaScript are non-primitive data types that hold an unordered collection of key-value pairs. check a key exists in JavaScript object1. Using in Operator The in operator in JavaScript checks if a key exists in
2 min read
How to Check if Object is JSON in JavaScript ? JSON is used to store and exchange data in a structured format. To check if an object is JSON in JavaScript, you can use various approaches and methods. There are several possible approaches to check if the object is JSON in JavaScript which are as follows: Table of Content Using Constructor Type Ch
2 min read
Javascript - Catching error if json key does not exist In this article, we will discuss the error handling when a key is not present inside the javascript object. A JSON is a string format that encloses a javascript object in double-quotes. Example: '{"site": "gfg"}' A JSON object literal is the javascript object present inside the JSON String. Exampl
2 min read
How to Check if JSON Key Value is Null in JavaScript ? In JavaScript, the JSON Object can contain null values, which should be identified. The below approaches can be used to check if the JSON key is null. Table of Content Using for Loop and '===' OperatorUsing Object.values() with Array.prototype.includes()Using Array.prototype.some()Using for Loop and
3 min read
How to check if a value is object-like in JavaScript ? In JavaScript, objects are a collection of related data. It is also a container for name-value pairs. In JavaScript, we can check the type of value in many ways. Basically, we check if a value is object-like using typeof, instanceof, constructor, and Object.prototype.toString.call(k). All of the ope
4 min read