โ11-18-2020 08:33 PM
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
โ11-19-2020 01:31 AM
@Ish
Please refer attached pipeline.
conditionalField_2020_11_19.slp (3.7 KB)
โ11-19-2020 06:34 AM
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.
โ11-19-2020 09:51 AM
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
โ11-19-2020 12:14 PM
Thanks @ptaylor! I will do that. Note: I canโt login to access the Aha! link.