Given a JSON Object, the task is to check whether a key exists in the Object or not using JavaScript.
Below are the methods to Check if a key exists inside a JSON object:
Table of Content
JavaScript hasOwnProperty() Method
This 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);
Parameters:
- prop: This parameter is required. It specifies the string name or Symbol of the property to check.
Return Value:
It returns a boolean value indicating whether the object has the given property as its own property.
Example 1: This example checks for prop_1 of the obj by using hasOwnProperty property.
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() |
let 'obj' has prop_1 property
Example 2: This example checks for pro_1 of the obj by simple array access.
let obj = { prop_1: "val_1" ,
prop_2: "val_2" ,
prop_3: "val_3" ,
prop_4: "val_4" ,
}; function gfg_Run() {
ans = "" ;
let prop = 'pro_1' ;
if (obj[prop]) {
ans = "let 'obj' has " + prop + " property" ;
} else {
ans = "let 'obj' has not " + prop + " property" ;
}
console.log(ans);
} gfg_Run() |
let 'obj' has not pro_1 property
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
Parameters:
- prop: This parameter holds the string or symbol that represents a property name or array index.
- object: This parameter is an Object that is to be checked whether it contains the prop.
Return value:
This method returns a boolean value. The value true is returned if the specified property is found in an object, else it returns false.
Example: In this example, we are using in
Operator.
// Example JSON object let jsonObject = { name: 'John' ,
age: 25,
city: 'New York'
}; // Check if 'age' key exists in jsonObject if ( 'age' in jsonObject) {
console.log( 'The key "age" exists in the JSON object.' );
} else {
console.log( 'The key "age" does not exist in the JSON object.' );
} |
The key "age" exists in the JSON object.
Using Reflect.ownKeys()
and includes()
method
JaScript Reflect.ownKeys() method in Javascript is used to return an array of the target object’s own property keys and it ignores the inherited properties.
Syntax:
Reflect.ownKeys( obj );
Parameters:
- Obj: This parameter holds the target object and it is used to get its own keys.
Return value:
- This method always returns the Array of the target object’s own property keys.
Example: In this example we are using Reflect.ownKeys()
and includes()
method.
const jsonObject = { key1: 'value1' ,
key2: 'value2' ,
key3: 'value3'
}; const keyToCheck = 'key2' ;
if (Reflect.ownKeys(jsonObject).includes(keyToCheck)) {
console.log(`${keyToCheck} exists in the JSON object.`);
} else {
console.log(`${keyToCheck} does not exist in the JSON object.`);
} |
key2 exists in the JSON object.
Using Object.getOwnPropertySymbols()
and includes()
method
The Object.getOwnPropertySymbols() method in JavaScript is a standard built-in object which returns an array of all symbol properties that are present in a given object. An empty array is returned until symbol properties are set on the object.
Syntax:
Object.getOwnPropertySymbols(obj);
Parameters:
- obj: This parameter is the object whose symbol properties are to be returned.
Return value:
- This method returns an array of all symbol properties that correspond to the properties found directly in the given object.
Example: In this example we are using Object.getOwnPropertySymbols()
and includes()
method
const jsonObject = { [Symbol( 'key1' )]: 'value1' ,
[Symbol( 'key2' )]: 'value2' ,
[Symbol( 'key3' )]: 'value3'
}; const keyToCheck = Symbol( 'key2' );
if (Object.getOwnPropertySymbols(jsonObject).includes(keyToCheck)) {
console.log(`${keyToCheck.toString()} exists in the JSON object.`);
} else {
console.log(`${keyToCheck.toString()} does not exist in the JSON object.`);
} |
Symbol(key2) does not exist in the JSON object.
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);
Parameters:
This method accepts a single parameter as mentioned above and described below:
- obj: This parameter holds the object whose enumerable and non-enumerable properties are to be returned.
Return value:
- This method returns an array of strings that corresponds to the properties found directly in the given object.
Example: In this example we are using Object.getOwnPropertyNames() and includes() method.
const jsonObject = { key1: 'value1' ,
key2: 'value2' ,
key3: 'value3'
}; const keyToCheck = 'key2' ;
if (Object.getOwnPropertyNames(jsonObject).includes(keyToCheck)) {
console.log(`${keyToCheck} exists in the JSON object.`);
} else {
console.log(`${keyToCheck} does not exist in the JSON object.`);
} |
key2 exists in the JSON object.
Using Object.keys()
and includes()
method
The Object.keys()
method in JavaScript is used to retrieve an array of the enumerable property names of an object. It returns an array containing the keys of the object.
Syntax:
Object.keys(obj);
Parameter:
- obj: It is the object whose enumerable properties are to be returned.
Return Value:
- It returns an array of strings that represent all the enumerable properties of the given object.
Example: In this example we are using Object.keys() and includes() method
const jsonObject = { key1: 'value1' ,
key2: 'value2' ,
key3: 'value3'
}; const keyToCheck = 'key2' ;
if (Object.keys(jsonObject).includes(keyToCheck)) {
console.log(`${keyToCheck} exists in the JSON object.`);
} else {
console.log(`${keyToCheck} does not exist in the JSON object.`);
} |
key2 exists in the JSON object.