Open In App

How to Check if JSON Key Value is Null in JavaScript ?

Last Updated : 16 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Using for Loop and ‘===’ Operator

In this approach, we are using a for loop to iterate through each key in the JSON object and checking if its corresponding value is null using the strict equality operator ===, setting a flag to true if a null value is found.

Syntax:

for (let key in object) {
if (object[key] === value) {
// code
}
}

Example: The below example uses for Loop and ‘===’ Operator to check if the json key value is null in JavaScript.

JavaScript
const jsonData = {
    key1: null,
    key2: "value2",
    key3: undefined,
};
let res = false;
for (let key in jsonData) {
    if (jsonData[key] === null) {
        res = true;
        break;
    }
}
if (res) {
    console.log(
        "Null value is present in the JSON object.");
} else {
    console.log(
        "No null value found in the JSON object.");
}

Output
Null value is present in the JSON object.

Using Object.values() with Array.prototype.includes()

In this approach, we are using Object.values() to extract an array of values from the JSON object, and then we use Array.prototype.includes() to check if the array includes null, indicating the presence of a null value in the JSON object.

Syntax:

const valuesArray = Object.values(object);
if (valuesArray.includes(value)) {
// code
}

Example: The below uses Object.values() with Array.prototype.includes() to check if the json key value is null in JavaScript.

JavaScript
const jsonData = {
    key1: null,
    key2: "value2",
    key3: undefined,
};
const res = Object.values(jsonData);
if (res.includes(null)) {
    console.log(
        "Null value is present in the JSON object.");
} else {
    console.log(
        "No null value found in the JSON object.");
}

Output
Null value is present in the JSON object.

Using Array.prototype.some()

In this approach, we are using Object.values() to extract an array of values from the JSON object, and then we use Array.prototype.some() with a callback function to check if any value in the array is null, indicating the presence of a null value in the JSON object.

Syntax:

const hasValue = array.some(function(item) {
return item === value;
});
if (hasValue) {
// code
}

Example: The below example Array.prototype.some() to check if the json key value is null in JavaScript.

JavaScript
const jsonData = {
    key1: null,
    key2: "value2",
    key3: undefined,
};
const res = Object.values(jsonData).
    some(value => value === null);
if (res) {
    console.log(
        "Null value is present in the JSON object.");
} else {
    console.log(
        "No null value found in the JSON object.");
}

Output
Null value is present in the JSON object.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads