Remove set node items if body empty

Hi guys,

I am trying to setup a workflow that involves receiving multiple objects with specific ids that then goes into a template, and then into a set node. I am trying to figure out the best way to set this up.

If one of the objects is empty I want to hide that set template for that object so that it does not insert into the final node.

Is this something that can be accomplished with an expression?

You can see the sample below.

Any help would be much appreciated!

I’m not sure how to do that with an expression, but , you can use Function and Functionitem nodes that allow you easily to manipulate the data, removing or adding fields with a few lines of javascript

Hi @jtas21, from your example it sounds like you might not want to get rid of an entire n8n item, but a single field?

If you want to remove a specific field value (not the field itself) using only the Set node, for example in your SetIDs node based on a condition, you could for example:

  1. Use JavaScript’s ternary operator to check the condition.
  2. Return null in the respective field if the condition is not met.

Wrapped in an expression this could look like so:

{{$node["Set"].json["id3"] ? "ID3: " +  $node["Set"].json["id3"] : null}}

This would check if the id3 field of the Set node has a truthy value. If so, it would return ID3: followed by the respective value. Otherwise null. So this won’t quite remove the field but should be close enough for most scenarios. If you actually need to remove a field, you’d need to use the nodes suggested by @xavier.

1 Like

Ah yep that’s it @MutedJam! I just replaced null with ‘’, and that works.

Thanks so much!

{{$node["Set"].json["id2"] ? "ID2: " +  $node["Set"].json["id2"] : ''}}
{{$node["Set"].json["id3"] ? "ID3: " +  $node["Set"].json["id3"] : ''}}
1 Like