10 examples of 'check if json object is empty' in JavaScript

Every line of 'check if json object is empty' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
45export function isObjectEmpty(object) {
46 if (Object.keys(object).length === 0) {
47 return true;
48 }
49 return false;
50}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

245function isObjectEmpty (object) {
246 if (typeof object !== 'object') return false
247 if (Object.keys(object).length === 0) return true
248 return false
249}
44function isObjectEmpty(obj) {
45 for (var key in obj) {
46 if (obj.hasOwnProperty(key)) {
47 return false;
48 }
49 }
50 return true;
51}
13export function isObjectEmpty(object) {
14 if (object) {
15 const keys = Object.keys(object);
16 return keys.length === 0;
17 }
18 return true;
19}
2export function isObjectEmpty(object) {
3 for (const key in object) {
4 return false;
5 }
6 return true;
7}
717function checkJSON(obj) {
718 if (!obj) {
719 return true;
720 }
721 try {
722 JSON.stringify(obj);
723 } catch (e) {
724 return false;
725 }
726 return true;
727}
16static isObjectEmpty(obj) {
17 return obj && obj.constructor === Object && Object.keys(obj).length === 0;
18}
198function isObjectEmpty(object: ObjectValue) {
199 let propertyCount = 0;
200 for (let [, binding] of object.properties) {
201 if (binding && binding.descriptor && binding.descriptor.enumerable) {
202 propertyCount++;
203 }
204 }
205 return propertyCount === 0;
206}
24export function isEmpty(obj) {
25 for (var prop in obj) {
26 if (obj.hasOwnProperty(prop)) {
27 return false;
28 }
29 }
30 return JSON.stringify(obj) === JSON.stringify({});
31}
70function isObjectEmpty(obj) {
71 return Array.from(Object.values(obj)).length === 0;
72}

Related snippets