To check if a JSON is empty in Node, we check if the object has any keys.
For instance, we write
const isEmptyObject = (obj) => {
return !Object.keys(obj).length;
};
//...
if (isEmptyObject(query)) {
// ...
} else {
// ...
}
to define the isEmptyObject
function.
In it, we call Object.keys
to get the keys of obj
in an array.
Then we check if its length
is 0 to see if obj
is empty.
Then we use an if statement to check if query
is empty.