Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am unable to use padRight

Im trying to add zeros when necessary to a mapping but it is not working.

This is the payload im working with:

[
    {
        
        "groupName": "POSCH",
        
        "groupMembers": [
            {
                "dealerNumber": "262",
                
                "mainDealer": true
            },
            {
                "dealerNumber": "243",
                
                "mainDealer": false
            }
        ]
    },
    {
        
        "groupName": "PRUCKNER",
        
        "groupMembers": [
            {
                "dealerNumber": "213",
                
                "mainDealer": true
            },
            {
                "dealerNumber": "262",
                
                "mainDealer": false
            }
        ]
    }]

Im currently writting this in order to filter the dealerNUmber that contains true in maindealer only. I believe that is in a good state but i cant add the zeros

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

%dw 2.0
output application/json
---
payload map {
    groupName: $.groupName,
    dealers: $.groupMembers filter $.mainDealer == true map {
        dealerNumber: $.dealerNumber padRight(5, "0"),
        locationName: $.locationName
    }
}

In order to get the 262 becoming 00262.

But it is giving me this error :

Invalid input ',', expected ')' for the enclosed expression. (line 7, column 48):


7|         dealerNumber: $.dealerNumber padRight(5, "0"),
                                                  ^
Location:
main (line: 7, column:48)

>Solution :

There are a
couple of problems with the DataWeav
e.

  1. As per your requirement you need leftPad and not rightPad since you are trying to add zeros to the left of your string. You need to import it as it is in the dw::core::Strings module.
  2. You are mixing infix and prefix notations. You can use only one of them at a time and infix is only possible when you want to call the function with 2 params.
    Refer this documentation

DataWeave supports both prefix notation (function (param1, param2))
and infix notation (param1 function param2) for function calls. Note
that you can use infix notation only with functions that accept only
two parameters
.

Your DataWeave should look like this after the suggested changes:

%dw 2.0
import leftPad from dw::core::Strings
output application/json
---
payload map {
    groupName: $.groupName,
    dealers: $.groupMembers filter $.mainDealer == true map {
        dealerNumber: leftPad($.dealerNumber,5, "0"),
        locationName: $.locationName
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading