cancel
Showing results for 
Search instead for 
Did you mean: 

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

nchrk
New Contributor II

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
1 ACCEPTED SOLUTION

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

image

View solution in original post

7 REPLIES 7

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

image

Thanks Soni for your response. But i’m getting error when this property is not available in input schema object in mapper.
Error :- $JsonStr is not defined

how do we check the property exists or not. if exists, replace the values
$.get(‘JsonStr) != null && $.get(‘JsonStr) =“” ? $.get(‘JsonStr).toString().replaceAll(’\n’,’'):$.get('JsonStr)

I mentioned $JsonStr because you mentioned it in the sample data that you shared in comment.

$Jsonstr is the name of the input string that is coming from upstream snaps.

Now if you want to check if a specific field is there or not, you can use the below.
$.hasPath(‘$JsonStr’) ? ($JsonStr != null && $JsonStr != “” ? $JsonStr .replaceAll(“\n”,“”) : $JsonStr) : “”
Let me know if it works.