In JavaScript, you can check if a key exists in a JSON object in the following ways:
Using Object.prototype.hasOwnProperty()
You can use the Object.prototype.hasOwnProperty()
method to check if a key exists in a JSON object in the following way:
const jsonObj = { key1: 'value1', }; console.log(jsonObj.hasOwnProperty('key1')); // true console.log(jsonObj.hasOwnProperty('key2')); // false
You should use the Object.prototype.hasOwnProperty()
method when you want to check if a key exists as an own, directly defined, property of an object, ignoring inherited properties (i.e. properties in the prototype chain). For example:
const jsonObj = { key1: 'value1' }; const protoObj = { key2: 'value2' }; // set the prototype of `jsonObj` to `protoObj` Object.setPrototypeOf(jsonObj, protoObj); // check for the existence of properties console.log(jsonObj.hasOwnProperty('key1')); // true (exists in `jsonObj` itself) console.log(jsonObj.hasOwnProperty('key2')); // false (exists in the prototype chain)
As you can see from the example above, the Object.prototype.hasOwnProperty()
method returns true
only if the key exists as an own property of the object itself, and not if it exists in the prototype chain.
Using the in
Operator
You can use the in
operator to check if a key exists in a JSON object in the following way:
const jsonObj = { key1: 'value1', }; console.log('key1' in jsonObj); // true console.log('key2' in jsonObj); // false
You should use the in
operator when you want to check if a key exists in an object, including its prototype chain. For example:
const jsonObj = { key1: 'value1' }; const protoObj = { key2: 'value2' }; // set the prototype of `jsonObj` to `protoObj` Object.setPrototypeOf(jsonObj, protoObj); // check for the existence of properties console.log('key1' in jsonObj); // true (exists in `jsonObj` itself) console.log('key2' in jsonObj); // true (exists in the prototype chain)
As you can see from the example above, the in
operator returns true
if the key exists in an object or anywhere in the object's prototype chain.
Checking Against undefined
In JavaScript, when you try to access a non-existent key in an object, it returns undefined
. Therefore, you can use the presence of undefined
to determine if a key exists in an object, for example, like so:
const jsonObj = { key1: 'value1', }; console.log(typeof jsonObj.key1 !== 'undefined'); // true console.log(typeof jsonObj.key2 !== 'undefined'); // false
You should check against undefined
when you want to check if a key exists in an object, including its prototype chain. For example:
const jsonObj = { key1: 'value1' }; const protoObj = { key2: 'value2' }; // set the prototype of `jsonObj` to `protoObj` Object.setPrototypeOf(jsonObj, protoObj); // check for the existence of properties console.log(typeof jsonObj.key1 !== 'undefined'); // true (exists in `jsonObj` itself) console.log(typeof jsonObj.key2 !== 'undefined'); // true (exists in the prototype chain)
You should use this method with caution as it is possible to assign undefined
as a value to a property of an already parsed JSON object.
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.