Search code examples
mulemule-studiodataweavemulesoftmule4

remove all the extra lines from the array in mule 4


I want to remove the extra lines or "\n\r" & "\n" from the array but my solution is not working. Please provide the correct function or dataweave for this.

input (json array format):

 [{"m":"a\n\r",
   "a":"b\n"},
  {"m":"a\r\n",
   "a":"b\n"}]

expected output(json array format):

 [{"m":"a",
   "a":"b"},
  {"m":"a",
   "a":"b"}]

code:

    %dw 2.0
    var someSpaceJson = write(payload, "application/json", {"indent":false}) 
    output application/json
    ---
    someSpaceJson replace "\n\r" with ""

Solution

  • You need to specify \\ instead of \ to represent the escape char.

    %dw 2.0
    var someSpaceJson = write(payload, "application/json", {"indent":false}) 
    output application/json
    ---
    read((someSpaceJson replace "\\r" with "" replace "\\n" with ""),"application/json")
    

    This should give you your desired output.

    enter image description here