question

SuganyaSivashanmugam-0111 avatar image
0 Votes"
SuganyaSivashanmugam-0111 asked PramodValavala-MSFT answered

How to convert flat file to JSON in logic app standard

I am pinging a source system and getting some details in a base 64 encoded format. I have decoded it to flat file using decodebase64 function available on the Azure Logic app function. Now I want to transform that flat file to json so that I can send the result to my destination system. If we have to write an azure function for it, will anyone be able to share their code with me. I am new this and I am not sure how to write an azure function to do the conversion. Thanks in Advance.

azure-functionsazure-logic-apps
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

PramodValavala-MSFT avatar image
0 Votes"
PramodValavala-MSFT answered

@SuganyaSivashanmugam-0111 You could use the inline code action to do this in a couple of lines compared to having an Azure Function for this.

Here is code that you could use

var data = workflowContext.actions.Compose.outputs;
var rows = data.split('\n').map(a => a.split(','));

var keys = rows.shift();
var json = [];

for (let row of rows) {
    let o = {};
    for (let i = 0; i < row.length; i++) {
        o[keys[i]] = row[i];
    }
    json.push(o);
}

return json;

and here is the Workflow JSON for reference.

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": "id,name,rank\n1,Alex,1\n2,Bob,2\n3,Charlie,3",
                "runAfter": {},
                "type": "Compose"
            },
            "Execute_JavaScript_Code": {
                "inputs": {
                    "code": "var data = workflowContext.actions.Compose.outputs;\r\nvar rows = data.split('\\n').map(a => a.split(','));\r\n\r\nvar keys = rows.shift();\r\nvar json = [];\r\n\r\nfor (let row of rows) {\r\n    let o = {};\r\n    for (let i = 0; i < row.length; i++) {\r\n        o[keys[i]] = row[i];\r\n    }\r\n    json.push(o);\r\n}\r\n\r\nreturn json;"
                },
                "runAfter": {
                    "Compose": [
                        "Succeeded"
                    ]
                },
                "type": "JavaScriptCode"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "triggers": {
            "manual": {
                "inputs": {},
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "kind": "Stateless"
}



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.