Search code examples
azure-logic-appsazure-logic-app-standard

Convert CSV file as array in logic apps


I want to read CSV file as array in logic apps. I didn't find any documents related to it. Any help on this would be appreciated. Thank you!


Solution

  • Based on your question here are few arounds that we can try below either way for 2 different outputs.

    READ EACH OBJECT CSV FILE AS ARRAY OBJECT: We can use split and add the output of the compose connector into Array by adding 'Initialise Variable' Connector and for converting each word into array variable we need to add for each connector inside another for each connector to iterate the items inside the CSV file then with a split variable inside the inner for each connector having split(item(),',') expression.

    READ EACH ROW IN CSV FILE AS AN ARRAY: We can use Plumsail's Parse CSV connector in order to convert each CSV row as an array considering headers in the CSV file.

    Here are the screenshots for your reference

    enter image description here

    enter image description here

    • Expression in compose connector

      split(body('Get_blob_content_(V2)'),'\n')
      
    • Expression in For each 2 connector

      split(item(),',')
      

    Note: Make sure you go to your code view and change the expression in your split from split(body('Get_blob_content_(V2)'),'\\n') to split(body('Get_blob_content_(V2)'),'\n') as when we are writing the expression for this it first takes '\n' as string and adds another "" to the expression.

    Here is the code view of my logic app

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "Compose": {
                    "inputs": "@split(body('Get_blob_content_(V2)'),'\n')",
                    "runAfter": {
                        "Get_blob_content_(V2)": [
                            "Succeeded"
                        ]
                    },
                    "type": "Compose"
                },
                "Compose_2": {
                    "inputs": "@variables('array1')",
                    "runAfter": {
                        "For_each": [
                            "Succeeded"
                        ]
                    },
                    "type": "Compose"
                },
                "For_each": {
                    "actions": {
                        "For_each_2": {
                            "actions": {
                                "Append_to_array_variable": {
                                    "inputs": {
                                        "name": "array1",
                                        "value": "@items('For_each_2')"
                                    },
                                    "runAfter": {},
                                    "type": "AppendToArrayVariable"
                                }
                            },
                            "foreach": "@split(item(),',')",
                            "runAfter": {},
                            "type": "Foreach"
                        }
                    },
                    "foreach": "@outputs('Compose')",
                    "runAfter": {
                        "Initialize_variable": [
                            "Succeeded"
                        ]
                    },
                    "type": "Foreach"
                },
                "Get_blob_content_(V2)": {
                    "inputs": {
                        "host": {
                            "connection": {
                                "name": "@parameters('$connections')['azureblob_1']['connectionId']"
                            }
                        },
                        "method": "get",
                        "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files/@{encodeURIComponent(encodeURIComponent('JTJmY29udGFpbmVyMjQlMmZkYXRhLWFydGljbGUuY3N2'))}/content",
                        "queries": {
                            "inferContentType": true
                        }
                    },
                    "metadata": {
                        "JTJmY29udGFpbmVyMjQlMmZkYXRhLWFydGljbGUuY3N2": "/container24/data-article.csv"
                    },
                    "runAfter": {},
                    "type": "ApiConnection"
                },
                "Initialize_variable": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "array1",
                                "type": "array",
                                "value": "@outputs('Compose')"
                            }
                        ]
                    },
                    "runAfter": {
                        "Compose": [
                            "Succeeded"
                        ]
                    },
                    "type": "InitializeVariable"
                },
                "Parse_CSV": {
                    "inputs": {
                        "body": {
                            "content": "@{base64(body('Get_blob_content_(V2)'))}",
                            "headers": "url,user_id,token_id,username,password"
                        },
                        "host": {
                            "connection": {
                                "name": "@parameters('$connections')['plumsail']['connectionId']"
                            }
                        },
                        "method": "post",
                        "path": "/flow/v1/Documents/jobs/ParseCsv"
                    },
                    "runAfter": {
                        "Compose": [
                            "Succeeded"
                        ]
                    },
                    "type": "ApiConnection"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {
                "$connections": {
                    "defaultValue": {},
                    "type": "Object"
                }
            },
            "triggers": {
                "manual": {
                    "inputs": {
                        "schema": {}
                    },
                    "kind": "Http",
                    "type": "Request"
                }
            }
        },
        "parameters": {
            "$connections": {
                "value": {
                    "azureblob_1": {
                        "connectionId": "/subscriptions/<Your subscription>/resourceGroups<Your resourec group >/providers/Microsoft.Web/connections/azureblob-1",
                        "connectionName": "azureblob-1",
                        "id": "/subscriptions/<Your subscription>/providers/Microsoft.Web/locations/northcentralus/managedApis/azureblob"
                    },
                    "plumsail": {
                        "connectionId": "/subscriptions/<Your subscription>/resourceGroups/<Your resourec group >/providers/Microsoft.Web/connections/plumsail",
                        "connectionName": "plumsail",
                        "id": "/subscriptions/<Your subscription>/providers/Microsoft.Web/locations/northcentralus/managedApis/plumsail"
                    }
                }
            }
        }
    }
    

    REFERENCES: Convert CSV elements into a single Array using Azure Logic Apps - Stack Overflow