5

I have a Logic App that is being triggered when there is a Security Alert in Security Center.

I have a step where I map a subset of the inputs into a JSON document and use that to create a file.

I need the JSON document that I'm creating to all be in one line, so I need to make sure I replace any control line feeds in the inputs.

Example input:

{
    "headers": {
        "Content-Type": "application/json"
    },
    "body": {
        "RemediationSteps": "[\r\n  \"1. Enforce the use of strong passwords\",\r\n  \"2. Add the source IP to NSG block list for 24 hours\",\r\n  \"3. Create an allow list for RDP access in NSG\"\r\n]"
    }
}

My mapping (in the Designer):

replace(triggerBody()?['RemediationSteps'], '\r\n', ' ')

However, I'm still getting new lines in my JSON document.

Andy T
  • 10,223
  • 5
  • 53
  • 95
  • I don't see anything wrong with your replace function. Have you checked the raw output of the action that is doing the replace? Can you still see the '\r\n' substrings there? – Paco de la Cruz Aug 06 '18 at 03:45
  • Is triggerBody() returning actual JSON? Some Triggers emit base64 content. – Johns-305 Aug 06 '18 at 11:55

4 Answers4

8

When edited in design view, logic apps adds a backslash to the original backslash to cancel it out. If you go to Code view you can remove it manually.

From:

"value": "@{replace(items('...')['...'],'\\\r\\\n',' ')}"

To:

"value": "@{replace(items('...')['...'],'\r\n',' ')}"
Rich
  • 6,470
  • 15
  • 32
  • 53
  • 1
    Thanks! Had same issue, and this was the only solution I could find that worked for me. Just go to code view and fix it there, so simple... – Michael McQuirk Sep 30 '21 at 08:10
3

The above solutions didn't work for me in a Microsoft Flow as the web editor adds extra backslashes. There is no code editor option. What worked was to uri encode the string and then to do a replace:

decodeUriComponent(replace(uriComponent(body('bodyitem')?['bodykey']),'%0A','%3Cbr%3E'))

'%0A' is the '\n' uriencoded and '%3Cbr%3E' is '<br>' uriencoded.

First encode, do the replace then decode. Hope this helps!

jassent
  • 529
  • 3
  • 10
1

I had a similar problem. You have to literally use an "enter". This is what it looks like:

json(concat('{"items":',string(split(outputs('GetAttachmentContent'),'')),'}'))

Hope it helps.

d3jn
  • 1,392
  • 3
  • 13
  • 21
  • Hi Fellipe, you can format your code by highlighting it and clicking the `{}` button at the top of the editor. You can edit your answer with the button at the bottom. – hat Feb 21 '19 at 12:38
1

Literally putting a new line in the expression worked for me:

replace(triggerBody()?['Body'], '
', '<br/>')
Waldemar
  • 56
  • 2