Implement RegEx in Inline Script Action
Recently I had a little bit of a challenging scenario with Logic Apps. If you imagine I had a Logic App which took an input and then called an external API which returned data in XML format. I then needed to extract one property from within the data for downstream processing. I had a few choices how to get this data:
- I can take the response and write an xpath query to get the desired element
- I can convert the XML to Json and then use a Json schema to see strongly typed properties in the Logic App
- I can use a regular expression to extract the data i want using an Inline Script in the Logic App
- I could consider an external function
The things I was considering in this choice were:
Option 1 - XPath
This would have probably been my first choice if it was a simple query and the xml was not overly complicated. Unfortunately neither of these were true. I used to like with BizTalk development that you could have your xpath in a C# helper class and unit test it in isolation before its used in your wider solution but unfortunately with Logic Apps you cant do this and I thought there is a risk a simple typo would be difficult to spot if a mistake crept in later. Also the query was just too much of a pain to develop in the first place.
Option 2 - Convert to Json
I could use a compose shape with an expression converting the response to XML and then Json like in the below picture.
This would give me a json object to work with like in many Logic Apps. I could then create a schema and access the property as a strongly typed property.
This could be a quite nice approach in many circumstances, its not difficult to implement. In my case I felt again with the Xml being fairly complex it was probably troublesome to get the schema right so this was going to be my plan B.
Option 3 - Inline Script and RegEx
In my case I knew that the xml contains only 1 element with the name I desire. I also already had an Integration Account associated with my logic apps. This means I have inline script available as an option. If I didnt have the integration account already Id have chosen option 2 but option 3 sounded pretty straightforward to implement and meant I didnt need to worry too much about the big nasty xml and could just use a RegEx to grab the element I wanted.
Option 4 - Call a function
Failing any other option you always have the opportunity to call a function and do the logic required in .net code which is often much easier and testable. The trade off is you need to introduce a new function to do it. I felt this would definitely work but I didnt want to introduce new Azure resources for something which shouldnt be too complicated.
Implementing the solution
Initially I thought this was all good but when I tested it I got the below error.
With these changes I was able to successfully implement the regular expression I needed to extract a value from a complex xml document which had been returned from my Http call.
I hope this article is useful to others needing to do similar.