I’ve got a Zap that takes Shopify order data and routes that to an API, using a code step to properly format the data.
I need to be able to grab the line item fields and use them as arrays in my code step. Currently, I’m using the Input Data fields, and separating the line items by comma in JS, but I’d like to know if there is a way to get arrays of the line items as a direct input.
Separating by comma becomes problematic as soon as there are other fields like SKU or Variant, which might not have a value assigned, so Zapier removes the comma, and I’ll have an array of say, 5 products titles, and a SKU array that only has 4 items, because one of the products didn’t have one assigned in Shopify.
Best answer by TimS
Hi @dannymo ! Tim here from the Zapier Support team!
Just in case you haven’t found a good answer on Stack Overflow yet, I saw this and thought I could chime in.
There isn’t currently a way to input line items / arrays directly into a Code Step - as you’ve noticed we coerce all inputs to Strings. We do have a feature request we’re tracking to add support for sending Arrays in as they can be the most problematic for the reasons you mentioned.
In the meantime, here’s the approach I use to get my arrays into Code Steps without worrying about commas and null values being dropped:
Use the Formatter Utility Action Line Items to Text Action to convert the Line Items to a block of text with our own custom delimiters so we can more easily convert that back into the array in the Code Step.
Here’s how I set up my Line Items - I put the exact same delimiter inbetween each Line Item value like name, qty, etc. In this case ‘-----’. I also set the separator for each iteration of the arrays to ‘|||’
That gives us some values, and the one we’re interested in is the delimited full text value:
We input this single value into the Code Step and split it twice - first on the iteration delimiter and then on the value name delimiter.
If we log that to the console, we can see that we now have an array of arrays that we can work with.
Due to the nature of the Formatter Step processing the arrays properly, we won’t get any misaligned data if there were missing data points, and we can customize the delimiters to make sure they work with the values we’re getting. One last thing to note is that the individual elements are still all converted to strings, so to work with numbers, you’ll need to convert those back in your code.
Hi there @dannymo - Thanks for posting this up! I see you had a conversation with Christian and Leo from Zapier Support. You're definitely asking great questions, but I'm afraid we have limited knowledge on this specifically. As this is an advanced feature, but your question is definitely valid, can you post this question on StackOverflow and tag "Zapier" to it?
We have a couple of developers there that should be able to help you out more:
And you had originally posted this topic in “Ask the Community” but I’ve moved it to Developer Discussion as mentioned by Leo. Hope this bump to the topic gets attention from our Experts!
Hi @dannymo ! Tim here from the Zapier Support team!
Just in case you haven’t found a good answer on Stack Overflow yet, I saw this and thought I could chime in.
There isn’t currently a way to input line items / arrays directly into a Code Step - as you’ve noticed we coerce all inputs to Strings. We do have a feature request we’re tracking to add support for sending Arrays in as they can be the most problematic for the reasons you mentioned.
In the meantime, here’s the approach I use to get my arrays into Code Steps without worrying about commas and null values being dropped:
Use the Formatter Utility Action Line Items to Text Action to convert the Line Items to a block of text with our own custom delimiters so we can more easily convert that back into the array in the Code Step.
Here’s how I set up my Line Items - I put the exact same delimiter inbetween each Line Item value like name, qty, etc. In this case ‘-----’. I also set the separator for each iteration of the arrays to ‘|||’
That gives us some values, and the one we’re interested in is the delimited full text value:
We input this single value into the Code Step and split it twice - first on the iteration delimiter and then on the value name delimiter.
If we log that to the console, we can see that we now have an array of arrays that we can work with.
Due to the nature of the Formatter Step processing the arrays properly, we won’t get any misaligned data if there were missing data points, and we can customize the delimiters to make sure they work with the values we’re getting. One last thing to note is that the individual elements are still all converted to strings, so to work with numbers, you’ll need to convert those back in your code.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More about cookies
Customize which cookies to allow
Marketing cookies
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising.
Social media cookies
label
These cookies are set by a range of social media services that we have added to the site to enable you to share our content with your friends and networks. They are capable of tracking your browser across other sites and building up a profile of your interests. This may impact the content and messages you see on other websites you visit. If you do not allow these cookies you may not be able to use or see these sharing tools.
Analytics/performance cookies
These cookies enable the website to provide enhanced functionality and personalization, and allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. These cookies may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly and we will not know how you are using our site.
Functional cookies
label
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
Essential cookies
Always active
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.