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; | |
} |
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!! =))
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 @qianqian748 !
I think that my script is doing a good work and this error has nothing to this script.
Output what i'm getting when inputing to script your request body:
I think the problem is that you are using your API uncorrectly.
I do not know shopify API but after i read docs i was able to figure out problems with your request body:
accepts_marketing
needs to beboolean
type
indiscount_codes
needs to befixed_amount
notfixed amount
quantity
inline_items
needs to beinteger
There are probably more mistakes in your request body, so check it and after that try to send request.
I think that from my side it is all what i can do for you, since it is not related to this gist.
Regards, Mondonno