Home > Net >  How do I parse text by line in an Azure Logic App?
How do I parse text by line in an Azure Logic App?

Time:08-19

I've got a Logic App that reads in an email from a form submission. Unfortunately, I only have access to the email and not where the form results are stored, so I currently have the Logic App set up to pull in the email and convert it to text.

If it was just one field, I assume I could just grab it all, throw it in a variable and split it to get what I need, but I'm not sure how to do this with multiple lines.

The emails always come in with this format:

---

Date: 08/18/2022

Time: 09:30:00

Requestor Name: Robert Bobson

Requestor Email: [email protected]

Requestor Phone Number: 800-867-5309

Site Name: CompanyName

Site Number: 123456789

---

Am I able to set a line index and then grab everything on that line and then split it before assigning it to a variable? Is this going to require RegEx or is there a workaround or expression in Logic Apps that will handle this?

Thank you in advance!

CodePudding user response:

enter image description here

You can Convert the email body using "Html To Text" action and then assign it to an array variable by using the split expression for any delimiter.

CodePudding user response:

You can achieve your requirement using expressions.

First, try removing the extra lines from the given email. below is the expression I'm using to achieve the same.

split(outputs('Compose'),'\n\n')

The above expression results in: enter image description here

Am I able to set a line index and then grab everything on that line and then split it before assigning it to a variable?

yes, This is possible using the below expression

outputs('Compose_2')?[0] gives the Date 
outputs('Compose_2')?[1] gives the Time
...

RESULTS:

enter image description here

Alternatively, You can convert the string into Parsable Json and then Parse it through Parse JSON Action. Below is the flow of my Logic App

enter image description here

In the above step I'm extracting the values on both left and right sides and storing them into a variable. You can use either of slice or substring functions to extract the values.

enter image description here

In the next step I'm trying to Parse the values through Parse JSON action.

RESULTS:

enter image description here

You can test the same using below code view in your logicapp

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": "Date: 08/18/2022\n\nTime: 09:30:00\n\nRequestor Name: Robert Bobson\n\nRequestor Email: [email protected]\n\nRequestor Phone Number: 800-867-5309\n\nSite Name: CompanyName\n\nSite Number: 123456789",
                "runAfter": {},
                "type": "Compose"
            },
            "Compose_2": {
                "inputs": "@split(outputs('Compose'),'\n\n')",
                "runAfter": {
                    "Compose": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Compose_3": {
                "inputs": "@body('Parse_JSON')?['Site Name']",
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "length",
                            "type": "integer"
                        }
                    ]
                },
                "runAfter": {
                    "Compose_2": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_variable_2": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Json",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Parse_JSON": {
                "inputs": {
                    "content": "@concat('{',substring(variables('Json'),0,sub(length(variables('Json')),1)),'}')",
                    "schema": {
                        "properties": {
                            "Date": {
                                "type": "string"
                            },
                            "Requestor Email": {
                                "type": "string"
                            },
                            "Requestor Name": {
                                "type": "string"
                            },
                            "Requestor Phone Number": {
                                "type": "string"
                            },
                            "Site Name": {
                                "type": "string"
                            },
                            "Site Number": {
                                "type": "string"
                            },
                            "Time": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    }
                },
                "runAfter": {
                    "Until": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            },
            "Until": {
                "actions": {
                    "Append_to_string_variable": {
                        "inputs": {
                            "name": "Json",
                            "value": "@{outputs('Current_Object')},"
                        },
                        "runAfter": {
                            "Current_Object": [
                                "Succeeded"
                            ]
                        },
                        "type": "AppendToStringVariable"
                    },
                    "Current_Object": {
                        "inputs": "\"@{substring(outputs('Compose_2')?[variables('length')],0,indexOf(outputs('Compose_2')?[variables('length')],':'))}\":\"@{slice(outputs('Compose_2')?[variables('length')],add(indexOf(outputs('Compose_2')?[variables('length')],':'),2),length(outputs('Compose_2')?[variables('length')]))}\"",
                        "runAfter": {},
                        "type": "Compose"
                    },
                    "Increment_variable": {
                        "inputs": {
                            "name": "length",
                            "value": 1
                        },
                        "runAfter": {
                            "Append_to_string_variable": [
                                "Succeeded"
                            ]
                        },
                        "type": "IncrementVariable"
                    }
                },
                "expression": "@equals(variables('length'), length(outputs('Compose_2')))",
                "limit": {
                    "count": 60,
                    "timeout": "PT1H"
                },
                "runAfter": {
                    "Initialize_variable_2": [
                        "Succeeded"
                    ]
                },
                "type": "Until"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {},
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}
  • Related