Home > Back-end >  Which is the best way of parsing CSV-data in a logic app without using a custom connector?
Which is the best way of parsing CSV-data in a logic app without using a custom connector?

Time:11-03

I have an SFTP trigger in a logic app which fires when a file is added to a certain file area. It is a CSV-formatted file and I want the rows to be parsed and coverted into json. Which is the best way to convert CSV-data into json without using any custom connectors?

I cannot find any built-in connectors doing this job. And as far as I know there are no logic apps functions doing the job either.

CodePudding user response:

1.Created logic app as shown below:

enter image description here

2 .Created container in storage account and uploaded a CSV file in container.

enter image description here

3.Next using compose action to split the contents of the CSV file on every new line into an array. a. Here is the expression used in SplitLines compose action:

split(body('Get_blob_content_(V2)'),decodeUriComponent('
'))

b. Follow the below MS Doc to write expressions:

enter image description here4. Removing last(empty) line from previous output using another compose action as shown below ,

take(outputs('SplitLines'),add(length(outputs('SplitLines')),-1))

enter image description here

5.Separating filed names using compose action

split(first(outputs('SplitLines')), ',')enter image description here

  1. Forming json as shown below using Select action,
**From**: **`skip(outputs('RemoveLastLine'), 1)`**
**Map:**
**`outputs('SplitFieldName')[0]`** **`split(item(), ',')?[0]`**
**`outputs('SplitFieldName')[1]`** **`split(item(), ',')?[1]`**

enter image description here

  1. Tested logic app and it is running successfully. 

enter image description here

  1. Content of CSV file is as shown below: 8

  2. Csv data is formatted as json: enter image description here

Reference:Use data operations in Power Automate (contains video) — Power Automate | Microsoft Docs

CodePudding user response:

Right now, there is no connector/action in logic app that can provide the out of box solution for your requirement. You need to loop in through the array and perform the calculation as per your requirement but I will not suggest you leverage the loop, variables action as it may take time and cost you more.

The alternative would be leveraging the inline code (JavaScript code) to do the calculation as per your requirement. Please note that you will need Integration Account to run your inline code. Please refer to javascript code and modified if needed according to your needs. I have used '_' for differentiating the nested objects. For more details you can refer to previous discussion here.

For complex calculation you can offload this functionality to azure function and write your code as per the supported languages and call azure function from logic app.

  • Related