Implement RegEx in Inline Script Action
  • Updated on 01 Jan 2020
  • 4 minutes to read
  • Contributors
  • Print
  • Comments
  • Share
  • Dark
    Light

Implement RegEx in Inline Script Action

  • Print
  • Comments
  • Share
  • Dark
    Light

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:

  1. I can take the response and write an xpath query to get the desired element
  2. I can convert the XML to Json and then use a Json schema to see strongly typed properties in the Logic App
  3. I can use a regular expression to extract the data i want using an Inline Script in the Logic App
  4. 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.

image.png

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

To start off I implemented a workflow like below. I have my call with HTTP to the external service and then I added my Javascript to extract the value I want using RegEx.

image.png

If we take a closer look at the Javascript you can see that I am referencing the response or output from the Http call. I am then using the match function in javascript which allows me to run a regular expression. I know there will be an element with the tags <d:pspid>{some value}</d:pspid>. I can grab this with the regex and then I can use a string replace to remove the tags.

Initially I thought this was all good but when I tested it I got the below error.

image.png

This is a little gotcha that I want to make you aware about. I ended up chatting to Derek Li from the product team to workout how to get around this. The problem was that the response from the HTTP call is not necessarily in the right content type for my javascript to work. This meant under the hood I had an object with an encoded string as one of the properties which was my response content. Some Logic App actions may give you a string automatically depending on what they are doing but in the case of the Http one I had an object which means that the match function wasnt relevant to the object I had initialised in the javascript. You can see the error below.

image.png

The challenge was that inline script can not at the current time access variables so I was unsure how to convert my response to be something I could access in the javascript. Fortunately Derek had a pearl of wisdom and suggested using the Compose action. I was able to add the compose action and then use the string expression on my Http Body to make the output from the compose action be a correctly formatted string containing the xml. I was then able to modify my javascript to reference the output from the compose shape. You can see this below.

image.png

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.

Was this article helpful?