Question

Null values are removed (Code mode)

  • 1 March 2023
  • 3 replies
  • 341 views

Hi there,

I’m trying to setup a simple zap, where I receive JSON data through a webhook trigger, and want to transform it in Code mode.

I’m seeing how to achieve this, but I’m having issue with the way Zapier handle missing values. To illustrate :

 

Here is my data (simplified) :

{
data: {
items: [
{
firstName: "testA",
options: [customFields: [name: "Email", answer: "testA@test.com"]]
},
{
firstName: "testB",
},
{
firstName: "testC",
options: [customFields: [name: "Email", answer: "testC@test.com"]]
}
]
}
}

As you can see, I may or may not have an “options” key for each items.

 

In code mode, I’m setting my Input Data like this :

So far so good, everything looks like expected. There’s a missing email, because TestB doesn’t have one. Perfect.

 

However, in my code, the null email is stripped from the inputData.

inputData.email equals to “testA@test.com,testc@test.com” instead of “testA@test.com,,testc@test.com”.

This is an issue because I lose the information that “my second item is a null value”, which I need in my script.

 

Here is a test in code mode to demonstrate the behavior :

 

 

I’ll need to test this further but from what I can tell it’s not exclusively related to code mode but I’m seeing this kind of behavior across zapier actions (loop, formatting...), where my null value is removed.

 

Is there a way to prevent this behavior, and keep the null values around ?


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

3 replies

Userlevel 7
Badge +14

@Todd Harper

Here’s a trick…

Zap 1

  1. Trigger: ???
  2. Action: ???
  3. Action: POST - Webhook
    1. See highlighted sentence in screenshot of the Data section.
    2. If you leave these empty, all fields from the previous step will be used.
    3. TIP: Add a query string parameter (e.g. TS) with a timestamp to use for cache busting and deduplication.

Zap 2

  1. Trigger: Webhooks - Catch Hook
    1. This will give you the RAW JSON for arrays and objects
  2. Action: Code
  3. Action: ???

 

 

Userlevel 6
Badge +8

Hi @Clement O!

The consensus myself and some other certified experts have come to is that the best way to handle this Zapier quirk is by handling all of the data in a single code step, so you can manipulate the JSON data in exactly the format it comes in before Zapier processes it as strings. So while the webhook would remain your trigger, you would then use a code step to perform an additional HTTP request to get the data a second time and handle it from the code step (using no input data).

(Just saw @Troy Tessalone’s suggestion for catching a raw hook...that is a pretty similar solution, and actually means you don’t need to perform the request twice, so is probably an even cleaner solution!)

I have successfully manipulated Zapier’s output data in a code step before, but it involves some pretty workaround-y code and lots of looping. I’m an amateur/early career developer and the solution made me go, “Yes! I finally did it!” But my mid-career software engineer spouse looked at it and went, “This is egregious.” So...y’know...probably not recommend.

Userlevel 7
Badge +14

Hi @Clement O 

Good question.

 

You can try using Formatter > Utilities > options related to line items to prep the data before sending it to the Code step.

 

Or you can try this trick…

Change your Zap trigger to Webhooks - Catch Raw Hook.

That you should give your the raw JSON of the nested objects to allow you to better parse in the Code step.