0
votes

What's the quickest way to convert an array to a string variable (whilst keeping all the line breaks) using Logic App? Below is my definition which reads each array and append to a string variable, works fine but not the most efficient. The idea is that I can then create a BLOB from the final output without the ["..."]

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each": {
                "actions": {
                    "Append_to_string_variable": {
                        "inputs": {
                            "name": "Result String",
                            "value": "@concat(substring(items('For_each'), 0, sub(length(items('For_each')), 0)), '\r\n')"
                        },
                        "runAfter": {},
                        "type": "AppendToStringVariable"
                    }
                },
                "foreach": "@variables('CSV Content')",
                "runAfter": {
                    "Initialize_Result_String": [
                        "Succeeded"
                    ]
                },
                "runtimeConfiguration": {
                    "concurrency": {
                        "repetitions": 1
                    }
                },
                "type": "Foreach"
            },
            "Initialize_Result_String": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Result String",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "CSV Content",
                            "type": "array",
                            "value": [
                                "Header1,Header2",
                                "Data1,Data2",
                                "Data3,Data4"
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Initialize_variable_2": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Final Result",
                            "type": "string",
                            "value": "@variables('Result String')"
                        }
                    ]
                },
                "runAfter": {
                    "For_each": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {},
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

Basically I'm looking for a way to convert the array

[
  "Header1,Header2",
  "Data1,Data2",
  "Data3,Data4"
]

to a string

Header1,Header2
Data1,Data2
Data3,Data4
1
Why can't you just create a blob using the Initialize variable 2 content directly from the logic app that you have mentioned? like this is the best way of converting an array into string.SwethaKandikonda-MT
Hi @SwethaKandikonda-MTm because there is a process in between that only works when the data is in an array format, which is why at the end I'm searching for the fastest way to then convert that array back to a string format in order to create a BLOBtommyhmt
Sorry might have misunderstood you slightly there, yes Initialize variable 2 is available to be used to create a BLOB, but the process of getting it is from Initialising a blank Result String and filling it with the array element one at a time which is slow. I'm hoping therefore that there is a faster way of converting the initial CSV Content variable like using an string function but that doesn't seem to keep my line breaks very well, also it keeps the [""], see below for my end result if I just use the string function: ["Header1,Header2","Data1,Data2","Data3,Data4"]tommyhmt

1 Answers

0
votes

One of the workarounds is to use the Join Connector which takes joins each item in the array. Here is the screenshot of my logic app

enter image description here

RESULT:-

enter image description here

In Storage account:-

enter image description here

Below is the codeview of my logic app

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Create_blob_(V2)": {
                "inputs": {
                    "body": "@body('Join')",
                    "headers": {
                        "ReadFileMetadataFromServer": true
                    },
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['azureblob']['connectionId']"
                        }
                    },
                    "method": "post",
                    "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files",
                    "queries": {
                        "folderPath": "/container1",
                        "name": "sample1",
                        "queryParametersSingleEncoded": true
                    }
                },
                "runAfter": {
                    "Join": [
                        "Succeeded"
                    ]
                },
                "runtimeConfiguration": {
                    "contentTransfer": {
                        "transferMode": "Chunked"
                    }
                },
                "type": "ApiConnection"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "CSV Content",
                            "type": "array",
                            "value": [
                                "Header1,Header2",
                                "Data1,Data2",
                                "Data3,Data4"
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Join": {
                "inputs": {
                    "from": "@variables('CSV Content')",
                    "joinWith": "\n"
                },
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Join"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "manual": {
                "inputs": {},
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {
        "$connections": {
            "value": {
                "azureblob": {
                    "connectionId": "/subscriptions/<Your_Subscription_Id>/resourceGroups/<Your_ResourceGroup>/providers/Microsoft.Web/connections/azureblob",
                    "connectionName": "azureblob",
                    "id": "/subscriptions/<Your_Subscription_Id>/providers/Microsoft.Web/locations/northcentralus/managedApis/azureblob"
                }
            }
        }
    }
}