Webhooks, Line items, order information, JSON

  • 4 November 2021
  • 13 replies

Badge +1

Goal: Read in order information from a webhook using JSON. Create a custom event in Klaviyo with that order information.

Problem 1: Klaviyo doesn’t natively accept line items.

Problem 2: The line items coming in on JSON are not being treated as line items in Zapier...I don’t think.


I’ve been following this guide, but there is an amazing amount of gaps, assumptions, simplistic examples, and just overall, not very thorough or helpful.


Here is some sample data from the guide above. Notice how it is structured.


Here is some sample data from my actual webhook:


Although the text is different, the format is the same.


The problem I’m running into is that each order can have any number of “orderRows.” Maybe 1, maybe 10. When trying to access this data later on in the next step, I would expect to find an array of values stored in “productName.” 


However, that is not the case. Instead, each “productName” is being stored as a separate entry. Here’s what I mean:


What I would have expected is something like this, with one row containing all the values:


Now, even if or when I can get that part figured out, I still have the challenge of being able to read that into Klaviyo. I understand it might still take multiple steps and formatting, but again, the guide above is extremely lacking.


What needs to happen is that there is 1 event. That 1 event will have an order total price (also included in JSON). But then each order will have multiple line items for different products, prices, and quantities.

This is what an event looks like when it is read in by a native Magento2 integration:


I don’t particularly care about what my event looks like as long as all the correct information is there and I can access it for segmenting and triggering automations. And yes, all that information is available in the JSON Payload, I just have to get the line item feature working in order to bring it in.


By following the guide and using the line-item to text formatter, I still wasn’t able to read in array data as separate lines in Klaviyo. Here is the formatter input:


Here is the formatter output:


But when I send the “text” items into Klaviyo, it shows up like this:


So, where the hell do I start?


Thank you!

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

13 replies

Userlevel 7
Badge +14

Hi @DavidLGS 

Try using the Looping app for handling line items in an array:

Badge +1

@Troy Tessalone 

I’ve already looked into that, and tried to follow this guide here. But again, it’s not very good and missing some screenshots that would help explain which fields I can loop through.


For example, most of the data I need is nested within orderRows.<order_number> like the screenshot of my webhook data in the original post.


But none of the fields in in Zapier contain an array of all the order numbers. I would maybe expect some field I could select to be: Order Rows: 977182, 977183, 977184, …, 977188


That way it would know to loop through those order numbers, grabbing things like item name, price, qty, sku, etc.


But again, if you go back and read the post and look at the screenshots, each order number is only accessed as a single line. How do I tell it to loop through that?

Userlevel 7
Badge +11

Hey @DavidLGS, welcome to the Community! 

Looking at the screenshots you supplied it appears that you might be receiving a nested array of each of the order line items which may account for why they aren’t behaving as expected here. In which case, using a Code by Zapier (Run Javascript) step that uses the JSON.parse function may help to extract the line item data you’re after. For example: 
Just so I’m clear on the requirements here, are you looking to add each line item as text into the event in Klaviyo grouped like this?

orderrow#1 - price, qty, sku
orderrow#2 - price, qty, sku
orderrow#3 - price, qty, sku

Look forward to hearing from you on this! :)

Badge +1

Just so I’m clear on the requirements here, are you looking to add each line item as text into the event in Klaviyo grouped like this?

orderrow#1 - price, qty, sku
orderrow#2 - price, qty, sku
orderrow#3 - price, qty, sku

Look forward to hearing from you on this! :)


Yes, you are correct.

And I guess if you are correct about the nested array issue, that means I need to be catching the raw webhook? I’ll have to change my trigger and then pass the new endpoint onto the developer so it might be 12 hours before I can try anything new. Sad Day.


I appreciate your answer. At least it gives me something new to try. (And above all, “hope,” that this will eventually work.)


Userlevel 7
Badge +11

That’s right @DavidLGS! You’ll want to use the Catch Raw Hook trigger event for that. 

Fingers crossed that’ll be able to get the data in a better format so that you can extract the line item data from it as desired. If not, it may be worth seeing if your developer is able to adjust the structure of the webhook that’s sent so that we’re able to process the line item data better.

Please do keep us in the loop on how you get on with this! 

Badge +1

@SamB - I was able to get the developer to fix the nested array problem, but either Klaviyo doesn’t recognize the [:newline:] character or Zapier still isn’t sending it correctly. 

Here’s my line-item to text input and results:


Here’s my Klaviyo Event setup:


And here’s how it was received in Klaviyo:



The next thing I realized is that even if I could get the desired outcome from the “line-item to text” step, is that I might not want that result anyways. In an ideal world, each of those line items would be accessible as a separate property in Klaviyo. I need to be able to segment by “item name” and “item category” at the very least so when I go to my Klaviyo audience, I can see everyone that has ever purchased “Item A.” If that’s buried in a text string, it won’t get picked up by the Klaviyo segment. 

I think this is where “Looping” comes into play, but I’m very confused on how to use it to construct an entire order before sending the whole order to Klaviyo. 

Bottom line, this is what I’m trying to recreate so I can segment on any value and any property associated with the item, just like is possible with a native e-commerce integration (even if it doesn’t look this pretty):


And here it is in video explanation form:

Userlevel 7
Badge +11

That’s great news @DavidLGS! I’m so glad you were able to get the webhook structure fixed. :)

Thanks for taking the time to put together that video. I can definitely see what you mean here. As those details are all being added in as a text string into the Order Details property rather than individual line item properties, it’s not going to allow you to filter on each of those line item details in Klavivo as it they will be considered as a single property rather than the multiple properties.

I think instead of using a Looping by Zapier step what would work better is to use a Webhook by Zapier step to send a POST request directly to Klaviyo. That should, in theory, allow you to create the event and send over all the line item details as individual properties which you can later filter on in Klaviyo.

Having a quick look at Klaviyo’s API documentation it seems that the Track Profile Activity endpoint is the one you’d want to use when setting up that Webhook step. I can’t give you the exact setup of how the webhook step would need to be configured but we have some general documentation on how to set up Webhooks steps which you may find useful here: Send webhooks in Zaps

Do you think that approach could work for you here?

Badge +1

Hmmmm, that’s very interesting, @SamB . I forgot that Zapier can send webhooks too.

But, if I’m already sending a POST webhook from the source, wouldn’t it just be faster and remove possible points of failure by removing Zapier from the equation?

Userlevel 7
Badge +11

That’s a very good question, @DavidLGS! It would really depend on whether you’re able to adjust how the initial webhook is sent to match the requirements of Klaviyo’s API.

If you can adjust it then there’s no reason why you can’t just send it on directly to them. But if you can’t and the data isn’t sent over to Klaviyo in the way they are expecting then they won’t be able to process it. In which case, it may be worth using Zapier to send the data over. :)

Badge +1

Hi @SamB,

I’m sure you can’t offer “official” support, but can you look over my setup and notice if I’ve done anything glaringly wrong? Even with such a small, simple payload, I can’t get it into Klaviyo.


I’ve tried it with Unflatten=Yes, and I’ve tried it with my ‘blob’ typed out with line breaks. Neither of those things worked.

I need to use the “Custom Request” because Klaviyo uses nested values.


I’ve been following Klaviyo API docs and guides here for the endpoint, data payload, and headers:

I’m almost certain I’ve typed the “Data” portion correctly. I used to validate it.

And if that’s correct, then I wonder which protocol Zapier is sending it to Klaviyo with. According to the second bullet point, Klaviyo accepts cURL, JavaScript, Node, PHP, Python, and Ruby. 


-- Once I get this super simple payload to communicate with Klaviyo, then I can add more.


Thank you!

Userlevel 7
Badge +11

Hey @DavidLGS!

I’m not super familiar with Klaviyo’s API but it looks as though you’ve set it up as per their documentation. :)

One thing I did notice is that the item name was the same as the item name used in their example. Is “Boots” an actual item that exists in your Klaviyo account? If not that could be why it didn’t go through as expected. In which case retesting with an item that exists within Klaviyo may get it working. Could you give that a try and let me know if that does the trick?

Badge +1

Thanks, @SamB . Tried it. Didn’t work. 

It was a good idea, but in none of the API docs does it mention it has to be anything in your catalog. At least not that I could find.

Badge +1

An update and some more questions, @SamB.

I was able to POST data into Klaviyo. Whoop! The “trick” was in the header. The Content-Type was wrong and needed to be “application/json” when using the “Custom Request” webhook.

Now the questions/next issue: formatting text in an array.


Zapier text array formatting - Watch Video