Last active
October 10, 2023 17:57
-
-
Save Mondonno/182e12880d609374cd41ea64d054f225 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 checkBodyType = (body) => { | |
if(Array.isArray(body)) return 0; | |
else if(typeof body == "object") return 1; | |
else throw new Error("Unknown type of the body"); | |
} | |
const definePropertySettings = { configurable: true, enumerable: true, writable: true } | |
const deleteEmptyValues = (currentBody) => { | |
if(typeof currentBody === "string") currentBody = JSON.parse(currentBody); | |
let bodyType = checkBodyType(currentBody); | |
let newBody; | |
if(bodyType == 1) newBody = {}; | |
else newBody = []; | |
for(const key in currentBody) { | |
if(!currentBody.hasOwnProperty(key)) continue; | |
if(Array.isArray(currentBody[key])) { | |
if(currentBody[key].length === 0) continue; | |
let foundValue = deleteEmptyValues(currentBody[key]); | |
if(foundValue.length === 0) continue; | |
if(bodyType == 1) { | |
Object.defineProperty(newBody, key, { | |
...definePropertySettings, | |
value: foundValue | |
}); | |
} | |
else { | |
newBody.push(foundValue); | |
} | |
continue; | |
} else if(typeof currentBody[key] == "object") { | |
let foundValue = deleteEmptyValues(currentBody[key]); | |
let objectEntries = Object.entries(foundValue); | |
if(objectEntries.length === 0) continue; | |
if(bodyType == 1) { | |
Object.defineProperty(newBody, key, { | |
...definePropertySettings, | |
value: foundValue | |
}); | |
} | |
else newBody.push(foundValue); | |
continue; | |
}else if(typeof currentBody[key] == "string") { | |
if(currentBody[key] === "") continue; | |
else if(currentBody[key].startsWith("{{") && currentBody[key].endsWith("}}")) { | |
let firstAttempt = currentBody[key].substring(2, currentBody[key].length-1); | |
let finalAttempt = firstAttempt.substring(0, firstAttempt.length-1); | |
if(pm.variables.has(finalAttempt)) { | |
let value = pm.variables.get(finalAttempt); | |
if(typeof value == "string" && value.length === 0) continue; | |
} | |
} | |
} | |
else if(typeof currentBody[key] == "undefined") continue; | |
else if(typeof currentBody[key] == "function") continue; | |
else if(typeof currentBody[key] == "symbol") continue; | |
if(bodyType == 1) { | |
Object.defineProperty(newBody, key, { | |
...definePropertySettings, | |
value: currentBody[key] | |
}); | |
} | |
else newBody.push(currentBody[key]); | |
} | |
return newBody; | |
} |
great script. exactly what I needed. Just a note, in your postman usage "converted" might need to be "convertedBody"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Mondonno! Thank you very much for trying out the request and pointing out some of my errors. Really appreaciate your time and suggestions. I will look deeper into my code. Thanks!! =))