Skip to content

Instantly share code, notes, and snippets.

@Mondonno
Last active October 10, 2023 17:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Mondonno/182e12880d609374cd41ea64d054f225 to your computer and use it in GitHub Desktop.
Save Mondonno/182e12880d609374cd41ea64d054f225 to your computer and use it in GitHub Desktop.
Empty JSON cells deleter (can be used in Postman prerequests)
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;
}
@Mondonno
Copy link
Author

Mondonno commented Mar 24, 2022

@qianqian748 Hello again!

I saw your test JSON and i saw that you also want to delete empty values on arrays, not only objects which were treated by my program as a value not an object to check. I just now realized how much more important is analyzing arrays in the objects or analyze objects which are in array. Thank you for reporting this feature leak.

The script should work now as you expected.

Good day/afternoon/night!

@qianqian748
Copy link

@Mondonno Thank you for the quick reply! I tried with the edited code, somehow it still checks on the object and returned the following error:

{
"errors": {
"order": "Required parameter missing or invalid"
}
}

Looks like the object is deleted. This also happens even when i passed in values to the empty item array. Are we able to delete only the
empty item array nested in the line_items array?

Thanks!
Good day/afternoon/night to you too!

@Mondonno
Copy link
Author

Mondonno commented Mar 25, 2022

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:

{
   "order":{
      "order_number":"#1607",
      "email":"andy.famm99@gmail.com",
      "financial_status":"paid",
      "created_at":"2022-03-10T19:08:54+08:00",
      "fulfillment_status":"fulfilled",
      "accepts_marketing":"FALSE",
      "total_tax":"2.28",
      "discount_codes":[
         {
            "code":"hi",
            "amount":"0",
            "type":"fixed amount"
         }
      ],
      "line_items":[
         {
            "title":"broccoli",
            "quantity":"2",
            "price":"10"
         }
      ],
      "shipping_address":{
         "first_name":"Andy",
         "address1":"783b Hello Road",
         "phone":"8342 2341",
         "city":"Singapore",
         "zip":"732746",
         "last_name":"Fam"
      }
   }
}

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:

  1. accepts_marketing needs to be boolean
  2. type in discount_codes needs to be fixed_amount not fixed amount
  3. quantity in line_items needs to be integer

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

@qianqian748
Copy link

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!! =))

@agreylingmezzanine
Copy link

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