Open In App

JavaScript Check if a key exists inside a JSON object

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with JSON objects in JavaScript, it’s often necessary to check if a specific key exists within the object. This check is important, especially when dealing with dynamic or external data, to ensure that your code handles objects correctly and only accesses keys that are present.

Below are the methods to Check if a key exists inside a JSON object:

Methods to Check if a Key Exists Inside a JSON Object

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);

Example: In this example the gfg_Run, the function checks if obj has its own property prop_1 using hasOwnProperty(). It logs that obj has this property because it exists.

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


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

Example: In this example, The in operator checks if a specified key, like age, exists in the jsonObject. It returns true if the key exists, otherwise false.

JavaScript
// 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.');
}

Output
The key "age" exists 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);

Example: In this example, the Object.getOwnPropertyNames() returns an array of property names from jsonObject. includes(keyToCheck) checks if keyToCheck is in that array, confirming its presence in the object.

JavaScript
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.`);
}

Output
key2 exists in the JSON object.


Next Article

Similar Reads