Categories
JavaScript Answers

How to check if a JSON is empty in Node?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *