Search code examples
csvazure-logic-apps

Azure Logic App - Create CSV form JSON to send in Email


Hello so I have my Logic App that is receiving data such as

{
     "id": "123",
        "Email": "test@test.com",
        "Name": "Test Test",
        "Message": [
            {
                "Num": "123",
                "Flag": "A",
                "Value": "US",
            },
            {
                "Num": "123",
                "Flag": "A",
                "Value": "CA",
            }
        ]
    }

And in the Logic app I Create CSV table with the body which is working fine. But when I tried attaching this output in an email it just gave me my json back

enter image description here

The main question is how can I make a CSV document which I can add as an attachment on an Email I am sending out? Is there a way I can create a temporary CSV that isn't saved anywhere and just send in the email?

I tried Sharepoint & Excel Online - Create File however both are giving me permission problems and would like to avoid them. Should I just try to create the csv on the backend and send it to the logic app instead of creating the file in the Logic app?


Solution

    • Primarily, you need to convert your Json data to an Array before passing it to Create CSV table action, as it expects array input. So either you can use createArray(triggerBody()) or you can directly pass below request body in When a HTTP request is received trigger.
    [
      {
        "id": "123",
        "Email": "test@test.com",
        "Name": "Test Test",
        "Message": [
          {
            "Num": "123",
            "Flag": "A",
            "Value": "US"
          },
          {
            "Num": "123",
            "Flag": "A",
            "Value": "CA"
          }
        ]
      }
    ]
    
    • I am able to attach this CSV file as an attachment while sending email by using below workflow.

    enter image description here

    enter image description here

    Code-

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "Create_CSV_table": {
                    "inputs": {
                        "format": "CSV",
                        "from": "@createArray(triggerBody())"
                    },
                    "runAfter": {},
                    "type": "Table"
                },
                "Send_an_email_(V2)": {
                    "inputs": {
                        "body": {
                            "Attachments": [
                                {
                                    "ContentBytes": "@{base64(body('Create_CSV_table'))}",
                                    "Name": "test.csv"
                                }
                            ],
                            "Body": "<p>Testing....</p>",
                            "Importance": "Normal",
                            "Subject": "Test Email",
                            "To": "******"
                        },
                        "host": {
                            "connection": {
                                "referenceName": "office365"
                            }
                        },
                        "method": "post",
                        "path": "/v2/Mail"
                    },
                    "runAfter": {
                        "Create_CSV_table": [
                            "SUCCEEDED"
                        ]
                    },
                    "type": "ApiConnection"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "triggers": {
                "When_a_HTTP_request_is_received": {
                    "kind": "Http",
                    "type": "Request"
                }
            }
        },
        "kind": "Stateful"
    }
    

    Output -

    enter image description here

    enter image description here

    If you want to create a csv file by using Message then you need to take the value of Message in the From input of Create CSV table action.