Conditionally Remove or Add a Field

Hello,

How do you conditionally remove (or add) a field to a document?

E.g., I have a mapper:

Expression -> Target Path
20 -> $field1
10 -> $field2
‘’ -> $field3
null -> $field4

So I now have:
[
{
‘field1’:20
,‘field2’:10
,‘field3’:’’
,‘field4’:null
}
]

Now for example in pseudo code:

if ( field2 > 5 ) { delete(.field2) }
if ( field3 == '') {delete(.field3) }
if ( field4 == null ) { delete(.field4) }

So now it looks like:

[
{
‘field1’:20
}
]

How can I do that? Note: I can achieve this with a JSON Generator using Apache Velocity, but I’m hoping there is a way to do this using something like a Mapper.

It would also be OK to do this in the opposite direction, e.g.,

if ( input <= 4 ) { create(.field2, $input) }

Let me know if you have any questions,
Thanks!
Ish

@Ish
Please refer attached pipeline.
conditionalField_2020_11_19.slp (3.7 KB)

Thanks @Supratim! I really appreciate you taking the time to put that together for me.

I’m hoping this is possible to do in a way that is more akin to what I believe is the UX philosophy of SnapLogic though. If constructing an array/object on the fly and applying a filter on a single line is currently the way to achieve this, I’ll put this in as a feature request.

1 Like

Hi Ish,

Yes, there should be a straightforward way to conditionally omit values from the Mapper’s output without requiring complex filter expressions or the like. This has been requested by other users. Please go ahead and submit that feature request with your customer success rep, and reference this Aha ticket, which requested the same thing: https://snaplogic.ideas.aha.io/ideas/IIC-I-903.

Thanks,
Patrick

Thanks @ptaylor! I will do that. Note: I can’t login to access the Aha! link.

Sorry, Aha is a mystery to us all. :slight_smile: Just reference the ticket in your communications with your CS or support rep.