Forum Discussion

manohar's avatar
manohar
Contributor
3 years ago

Ignore mapping if empty

hi there,
how can I ignore the mapping if I am not getting any values? example in the below screenshot for the record, there’s no attachment so mapping should not happen. for this example since no attachement its failing in the downstream snaps.


Thanks
Manohar

13 Replies

  • bojanvelevski's avatar
    bojanvelevski
    Valued Contributor

    You can directly replace Copy + Filters with a Router. You can accomplish this by updating the expression in the Mapper(Filter) snap. Check pipeline below:

    mytransformation_2022_10_26.slp (69.8 KB)

    I would recommend the router version with proper mappings afterwards.

    Regards

  • Hi @manohar ,

    You can use Filter snap before the mapping and filter out the records that do not have attachment.

    BR,
    Marjan

  • bojanvelevski's avatar
    bojanvelevski
    Valued Contributor

    Hi @manohar,

    If I understand correctly, you don’t need those fields in the final result, if their value is null ?
    If that’s the case, you can try the following expression:

    sl.ensureArray($attachments.attachment.filter((v,k)=>v!=null))

    and map the Target path as $attachments only.

    Let me know if this helps,
    Bojan

    • manohar's avatar
      manohar
      Contributor

      Hi @bojanvelevski , that’s right.

      But wondering with this approach, how can I avoid the target portion?

      Thanks
      Manohar

    • marjan_karafilo's avatar
      marjan_karafilo
      Contributor

      Hi @manohar ,

      You can also use Router snap, where you will check if there is attachment, like:

      $attachments.get(‘attachment’) != null

      After that, you can use two different mappers, one without the attachment and one with it.

      BR,
      Marjan

  • bojanvelevski's avatar
    bojanvelevski
    Valued Contributor

    You cannot impact the attachments array directly in one mapper, since you have another mappings happening on the same level in the target object.

    What you can do though, is add another Mapper that will serve as a filter. The mapper uses the Mapping root to go down to the attachment object and filter out the “type”,“text” and “intent” fields if they are null.

    mytransformation_2022_10_25.slp (69.7 KB)

    Let me know if this helps,
    Bojan.

    • manohar's avatar
      manohar
      Contributor

      Thanks @bojanvelevski wondering how can I remove “attachments” node as well, if they are coming as null.

      I went with this solution for now but trying to reduce no. of snaps to get to that

      Thanks
      Manohar

      • JensDeveloper's avatar
        JensDeveloper
        Contributor II

        Hi @manohar

        I think it’s possible in a mapper snap with this expression and as target path nothing.
        I had this question also before to delete the fields that are empty and had this solution below

        Expression: jsonPath($,“$…[?(value == {}) ]”)
        The expressions checks the paths if they are null and then delete them if so. After that you need a next mapper snap that checks all the fields of null values and delete them.

        Example in the pipeline with mapper snap below

        The reason for two snaps is I think that you first need to check the values inside the array and after that you can check the higher parent for null values. I think you will understand if you check the pipeline.

        If there is a more efficient way I am happy to hear.
        Test_Pip_V1.3_2022_10_26 (1).slp (6.2 KB)