Automating Unstructured Text Parsing into JSON with Azure OpenAI and Power Automate
Tags: openai, azure, powerautomate, automation
Published: 2023-12-12 16:57:55

# Parsing Text with Azure OpenAI in Power Automate

If you've worked with Power Automate, you're familiar with the challenges of parsing text, handling body contents, and outputting data for subsequent actions in your flow.

## The Challenge with Unstructured Data

I've been experimenting with Azure Open AI for various tasks, and it's significantly improved the way I handle such development challenges. The latest task I tackled was this:
- **Receiving an email body.**
- **Identifying fields and values within that email to use in the flow.**

This usually involves complex parsing, composing, and potentially using AI Builder to identify keywords in unstructured data like an email body.

## A Solution with Azure OpenAI

So, what if we could let General AI take over and provide us with a clean JSON schema as requested? Here's how I approached it:

### Leveraging Azure Functions and Azure OpenAI

I used Azure Functions to tap into Azure Open AI models. This setup allowed me to integrate them seamlessly into various applications and Power Automate via HTTP requests, enabling custom modifications like setting the prompt sent.

### The Outcome

After several tests, the GPT-4 model from Azure Open AI proved to be extremely consistent. It provided outputs that I could easily use throughout my Power Automate flow.

#### So, a prompt like this to the **system role**

```
Please analyze the provided email screenshot and extract the relevant information according to the specific fields identified 
in the email body sample. Structure the extracted data into a 
JSON object with the following fields:
 'Incident', 'Short Description', 'Description',
'Name', 'VIP Status', 'Title', 'Location', 'Business Phone', 'Mobile Phone', 'Category', 'Subcategory', 'Impact', 
'Urgency', and 'Priority'. 
Ensure that the output is formatted as a structured JSON object. For any text that is unclear or cannot be read, 
use a placeholder or indicate 'information not available.' 
If there are recognition errors (e.g., with names), 
please note the potential correct information alongside.
```

#### Will return a well-structured JSON within only a single Parse JSON action from the output.

``` json
{
    "body": {
        "Incident": "INC0000XXX",
        "Short Description": "New Incident!",
        "Description": "SharePoint has an issue again",
        "Name": "John",
        "VIP Status": false,
        "Title": "Highway Sales Mgr",
        "Location": "CA USA",
        "Business Phone": "915-XX-X69",
        "Mobile Phone": "Not listed",
        "Category": "Application",
        "Subcategory": "BI",
        "Impact": "3 - Low",
        "Urgency": "3 - Low",
        "Priority": "5 - Very Low"
    }
}
```

#### Then, you can simply use it on any further actions 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0y9hh6divpzulwm00jo.png)
## Conclusion

Has anyone else experimented with similar use cases? I'd love to hear about your experiences and insights, as they could be incredibly helpful to other developers in our community.

Hope that provides some insights!