-
-
Save ThePVD/15c150276c1dd75650cfdb3009a0a0a1 to your computer and use it in GitHub Desktop.
Empty JSON cells deleter (can be used in Postman prerequests)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const deleteEmptyValues = (body) => { | |
let obj = {}; | |
let keys = (bb) => (!Object.keys(bb).length && Object.keys(bb).length !== undefined) | |
typeof body === 'string' ? body = JSON.parse(body) : null; | |
for (const key in body) { | |
let skip = [false]; | |
if (body.hasOwnProperty(key)) | |
if ((body[key] !== false && !body[key]) || (body[key].length !== undefined && !body[key].length)) { skip[0] = true; } | |
else | |
if (typeof body[key] === 'object') { | |
for (const ii of body[key]) { ((!!ii && ii !== 0) && keys(ii)) ? skip[0] = true : skip[0] = false; } | |
if (!skip[0]) | |
for (const id of body[key]) | |
if (id instanceof Object) { | |
for (const kk in id) { | |
console.log(id[kk]); | |
(id.hasOwnProperty(kk) ? (!id[kk] ? skip[0] = true : (!keys(id[kk]) ? skip[0] = false : skip[0] = true)) : null); | |
console.log(skip) | |
} | |
} | |
} | |
!skip[0] ? Object.defineProperty(obj, key, { configurable: true, enumerable: true, writable: true, value: body[key] }) : null; | |
} | |
return obj; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment