Power Automate: OCR Extraction With Logic Apps, Javascript And RegEx Filter


Hey Power Addicts! ๐Ÿ˜Ž In today’s blog post, we’ll discuss the possibilities of invoking regular expression (RegEx) from creating a Logic Apps HTTP Trigger with JavaScript code. Now the most common way for this has been through use of a cloud stored Excel-file and invoking the TypeScript RegEx from the Excel Automation tool in Power Automate. However in this guide, I’m going to spice it up with a different approach – mostly because I’m more used to do it this way from the past.

We’ll explore how to create a Power Automate flow with a Computer Vision service to read image based text and call a Logic Apps flow. We’ll then invoke a JavaScript code with RegEx patterns that outputs the matches, resulted back to our Power Automate flow.

So, whether you’re a developer, business owner, or just looking to optimize your workflow with RegEx, this guide is for you as well.


Our goals:

  • Combine Azure Computer Vision service to recognize text in images and convert it to structured text in Power Automate flow
  • Integrate a Power Automate flow to use the Logic Apps as a RegEx tool.
  • Configure a dedicated Logic Apps for RegEx-filtering with JavaScript.

Prerequisite:

  • Azure Subscription to create a FREE Azure Computer Vision service
  • Azure Subscription to create a FREE integration account
  • Azure Subscription to create an Azure Logic Apps.
  • Power Automate license to use HTTP-actions (Premium Connector)
  • Basic JavaScript skills for beginner!
  • Some RegEx experience… ๐Ÿ˜‚


Creating the Azure Computer Vision Service

Let’s start with creating the service if you haven’t done it yet. if you do have, then don’t mind skipping the steps for Creating the Azure Computer Vision Service.
Fill out the necessary information for your existing Subscriptions and resource groups, fill out a unique name for the Computer Vision service and check the box at the bottom before you continue to Review and create.
Verify the information and then click “Create
Click “Go to resource” when Azure has finished installing the service.
Now head to the “Keys and Endpoints” option, and save your key and endpoint information. These values will be used later on in our Power Automate flow.

Creating the Azure Integration Account

The Azure Integration Account is needed to take advantage of the JavaScript tools for Logic Apps. We want to set it up before we make use of the Logic Apps in advance.

Start by navigating to the “Integration accounts” in Azure
Click on “Create“.
Fill out the necessary information and name the integration account to whatever you want, as long as the name is recognizable. Click on “Review + create
Verify the account details and click on “Create“.

When the deployment has completed, you can move on to creating the Azure Logic Apps. Yay!


Creating the Azure Logic Apps

Head over to the Logic Apps service in Azure.
Click “Add” to add a new Logic Apps.
Fill out the necessary information with the same Resource Group and service location as the “Integration Account” service. Select the Standard Subscription and click “Review and create” then verify the information anc “Create” the service.
When your Logic Apps has been deployed, select “Go to resource” to connect our Integration account with the Logic App.
Navigate to the “Workflow settings” and select the Integration account that we just created. If it does not show up, then you might have created it in a different Resource group and need to correct this.
Head over to the “Logic app designer” and select “When a HTTP request is received
Paste in the following Request Body JSON Schema.

Request Body JSON Schema:

{
    "properties": {
        "text": {
            "type": "string"
        }
    },
    "type": "object"
}

Create a new action for our RegEx function with “Execute JavaScript Code“. For this demo, this one will specifically be extracting any values that resembles the Norwegian Organization Number format.

Code:

//match examples: 987654321 or 987 654 321
var regex = /(\d{9}|\d{3} \d{3} \d{3})/g;

//assigns input text to the "text" variable
var text = workflowContext.trigger.outputs.body.text

//returns an array of regex matches (array of strings)
return text.match(regex);

Create another “Execute JavaScript Code” action for our second RegEx function. This one will extract any values that resembles any Norwegian Phone number format.

Code:

/*match examples:
98765432, 987 65 432, +47 98 76 54 32
+4798765432, 98 76 54 32 */
regex = /(?:\+47)?(\d{8}|\d{3} \d{2} \d{3}|\d{2} \d{2} \d{2} \d{2})/g;

//assigns input text to the "text" variable
var text = workflowContext.trigger.outputs.body.text

//returns an array of regex matches (array of strings)
return text.match(regex);

Now create and initialize three variables. 1x Array variable, 2x Object variable:

These variables will be used to append our JSON body response payload. As it depends on if we get results back or not after a RegEx match.

Add a Condition action:

Next create the first condition for our Norwegian Organization Number – RegEx result.

On the Condition – set the “Result” for Norwegian Orgno not equal to null

Add a “Set variable” on each branch of the If statement with the information below.

Add an “Append to array variable” action below it with the value of the “Set variable” for each branch.

True branch:

{
  "arrNoOrg": "@{body('Execute_JavaScript_Code_-_Norwegian_Organization_No')}",
  "match": @{true}
}

False branch:

{
  "arrNoOrg": @{null},
  "match": @{false}
}

Add another Condition action:

Now create the second condition for our Norwegian Phone Number – RegEx result.

On the Condition – set the “Result” for Norwegian Phoneno not equal to null

Add the same structure as mentioned previously, this time based on the results from “Norwegian Phone Number

True branch:

{
  "arrNoPhone": "@{body('Execute_JavaScript_Code_-_Norwegian_Phone_No')}",
  "match": @{true}
}

False branch:

{
  "arrNoPhone": @{null},
  "match": @{false}
}

When finished, we’ll end with a Response action that we need to return to our Power Automate flow.

Use the output of the Array variable, although not necessary I am used to converting it to a JSON object at the same time.

Code:

json(string(variables('payloadBodyResults')))

Now save the flow and copy the URL for later use in our Power Automate flow:

Alriiight! ๐Ÿ˜Ž We’re now ready with the Logic Apps flow! Let’s continue with the main Power Automate flow (luckily a short flow) to create the original trigger logic!


Creating the Power Automate flow

Head over to Power Automate and create a new flow with “Manually trigger a flow” trigger. I name this flow “OCR Text with RegEx
For content input, we’re going to include a “File Content“. This is an easy trigger input which can be used and uploaded from our mobile application for Power Automate.
Add the “Computer Vision API” action, and copy in the values that we saved from before when creating the Computer Vision.
Paste in the URI created from the Logic Apps flow that we saved from before when creating the Logic Apps (from its trigger action).

Body payload:

{
  "text": "@{outputs('Optical_Character_Recognition_(OCR)_to_Text')?['body/text']}"
}

Wow! We’re actually finished with the basics for this demo! Now I am NOT going to include anything else, but you may store it to a SharePoint list for new information handling, search for a customer in the CRM-system or Dataverse. The limit is yours to what you want to achieve with extracting text from images.

Now, to finish it off – Run the Power Automate flow! For the demo runs, I am going to make use of and upload the following PNG-image – a randomly generated “Agreement for Partnership“:

Your Power Automate starts, converts the image to text through Computer Vision, sends the datao to Logic Apps trigger, which runs the JavaScript code and return the matches as a response body:

Back in Power Automate flow run, copy the response body

Tadaaa! your RegEx match results are returned in an array in Power Automate flow! Use it however you wish from this step on. And remember to parse the JSON body with a Parse JSON action!๐Ÿ˜


Summary

The Power Automate Flow

The Logic Apps Flow

In this guide, we’ve explored how to use Azure Computer Vision service and Power Automate flow to convert text in images to structured text, which is passed to a dedicated Logic App for RegEx filtering with JavaScript code invocation. This approach allows for efficient and accurate extraction of important information from image-based content, while also providing powerful RegEx filtering capabilities through Logic Apps.

Overall, this approach can be used to automate the extraction and processing of text data from various sources, enabling more efficient and accurate data management and analysis. With the right tools and resources, developers can quickly and easily implement these solutions in their workflows, improving productivity and accuracy in their work.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *