Forum Discussion

nchrk's avatar
nchrk
New Contributor II
3 years ago
Solved

Remove the character "\n" and empty spaces from string

i trying to remove the “\n” character along with empty spaces from string thru mapper expression, but it’s not working.

  1. input value JsonStr := [\n “AC”\n]
    expected output := [“AC”]
  2. input value JsonStr := [\n “AC”,\n “BD”,\n “DE” \n]
    expected output := [“AC”,“BD”,“DE”]
    i tried with JsonStr.toString().replaceAll(/\n/,“”). but it’s not working
  • You can use this expression: $JsonStr != null && $JsonStr != “” ? $JsonStr .replaceAll(“\n”,“”) : $JsonStr

    Explanation: This will first check if the JsonStr is null or empty, if it’s not then it’ll do the replacing else it’ll just put input jsonStr as output. In your case where JsonStr is null it’ll show output null

7 Replies

  • del's avatar
    del
    Contributor III

    @aditya.gupta41,

    I tested a few things with the SOAP snap, but I could not get my ideas to work. However, I had more success testing an idea with the REST POST snap, which does allow you to access the account.username and account.password fields of a REST Basic Auth account.

    You could potentially use the REST POST snap with the SOAP endpoint URL as the Service URL value, add the basic auth account to the snap, and use the following as an expression in the HTTP Entity field.

    '<env:Envelope
        xmlns:env="http://www.w3.org/2003/05/soap-envelope"
        xmlns:ns0="http://siemens.com/agilews">
        <env:Header/>
        <env:Body>
            <ns0:login
                xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
                <credentials>
                    <username>' + account.username + '</username>
                    <password>' + account.password + '</password>
                    <application>' + $credentials.application + '</application>
                </credentials>
            </ns0:login>
        </env:Body>
    </env:Envelope>'
    

    (Note 1: I tried the same with the HTTP Client snap, but it didn’t seem to have access to the account properties)

    (Note 2: The account properties feature of the REST snaps is not publicly documented as far as I can tell, but it has been referenced by SnapLogic employees in multiple community posts; so keep that consideration in mind. I use it in multiple scenarios for hiding API keys)

      • del's avatar
        del
        Contributor III

        @aditya.gupta41 ,

        My first thoughts are that you have variables in your HTTP Entity code that do not have values from upstream. REST Post is likely not null-safe. I posted my sample code based on your originally posted code. So, if you’re using my sample, you need to make sure $credentials.application is populated upstream, modify the variable for your case, or remove it from your test.

        Second thought is you may have put a ‘$’ in front of ‘account.username’ or ‘account.password’ and it is treating it like a variable instead of the account object.

        If those thoughts are not the case, can you post the contents of the HTTP Entity?

  • Hello,

    I am getting below error when writing above expressions in HTTP Entity:

    failure:
    “HTTP Entity: Cannot lookup a property on a null value”
    value:
    “Please check expression syntax and data types.”
    reason:
    “Please check expression syntax and data types.”

    I have created a REST POST basic auth account with username and password.

    Do you have any suggestion?

    Thanks,
    Aditya