How to convert flat file to JSON in logic app standard

Suganya Sivashanmugam 1 Reputation point
2021-10-01T07:43:12.003+00:00

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 Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,542 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pramod Valavala 20,656 Reputation points Microsoft Employee Moderator
    2021-10-06T12:17:45.127+00:00

    @Suganya Sivashanmugam 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"  
       }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.