question

EktaSDate-2465 avatar image
0 Votes"
EktaSDate-2465 asked MayankBargali-MSFT commented

Can we validate CSV headers in Logic App ?

Require to validate Customer Email attachment which should contain CSV and CSV should have specific Columns only. Logic APP validate if attachment of email is CSV or not using 'Get item() ends with csv' formula. But could not find how to validate the structure of CSV i.e. column headers. Can you help ?

azure-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

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

@EktaSDate-2465 There is no out-of-box connector that can help you to validate the CSV headers but you can use different actions to get the CSV header.
I am looping through the attachments and then verifying if the attachment name ends with the CSV or not last(split(items('For_each')?['name'],'.')).
If it ends with the CSV name then I am reading the content of the csv file and getting the first rows that has the CSV header details.

 first(split(base64ToString(items('For_each')?['contentBytes']),'
 '))

Code View : first(split(base64ToString(items('For_each')?['contentBytes']),'\n'))

Workflow:
142106-image.png


CSV File passed as attachment:
142098-image.png


Variable has the value of headers:
142125-image.png

Now you can split the header value by comma and loop in to verify whether your required header is present or not. If you don't want to loop in actions then you can pass the header variable to inline code to perform your business logic (to check the required header) and pass the boolean value or as per your need.

If you have complex logic then in case inline code didn't help then you can offload this functionality to the azure function.


image.png (19.1 KiB)
image.png (24.8 KiB)
image.png (49.5 KiB)
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.