Transforming API Responses

Transforming API Responses

Custom connectors are amazing to consume external APIs in Canvas Apps or Power Automate Flows. But what are you doing, when the external API response does not fit for you?

To be honest, this week I had the same issue with an own API. I set up my custom connector and everything was fine. The connector worked as expected and my operation provided a response. So far so good…

But, on the second look, I found out my response won’t work in my Canvas App. In detail, I want to display a list of my “userFields”. As you can see, my connector provided me a nested record:

I am screwed! What can I do? … adjust my API or should I …

Wait, I have an idea! I read somewhere that I can use a policy…

Custom Connector Policies

Microsoft Docs says: “Policies can be used to modify the behavior of connectors at runtime.”. Ok, sounds interesting. Furthermore, Microsoft have added some templates. This will help me a lot!

In my example, my API provides an array “value”. Furthermore, each record in this array have a property “userFields”. But this “userFields” is an object.

For that reason, I want to modify my API response. In detail, I want to get the following format for my “userFields”:

{
      "value": [
            {
                  // ...
                  "userFields": [
                        {
                              "key": "Custom.ValueArea",
                              "value": "Business"
                        },
                        {
                              "key": "Custom.ProcessGroupID",
                              "value": "PROD100"
                        },
                        {
                              "key": "Custom.ProcessArea",
                              "value": "PROD"
                        },
                        {
                              "key": "Custom.Critical",
                              "value": "true"
                        },
                        {
                              "key": "Custom.Severity",
                              "value": "4"
                        }
                  ]
            }
      ]
}

… and checkpot, there is a predefined policy template: “Convert an object to an array” (more details)!

Creating my Custom Connector Policy

First of all, I navigate to my custom connectors operations and create a new policy:

Furthermore, I give my policy a name and specify the template “Convert an object to an array”:

Now I select the operation, where my policy will be applied. In my case this is my “get-processmap” operation:

Afterwards, I must specify the “Target object or collection path”. This is the relative path in my response JSON, where my objects can be found. In my case, my response body is:

{
      "value": [
            // my objects
      ]
}

In conclusion, my “Target object or collection path” is “@body().value”:

Next step. Each of my items inside of the “value” array contains the property “userFields”, which is an object. Remember, I want to transform this object into an array. Therefore, my property subpath is “userFields”. Furthermore, my policies should replace the original property. As a result, my new property path is: @item().userFields”.

In addition, I want to get an array of key-value pairs. Let’s use the default here:

Finally, my new policy should run on the API response of my operation:

Perfect, my result policy looks great:

And now … 1-2-3 test and …. I get a correctly transformed response:

Summary

In the beginning, my API did not meet my requirements. I solved this problem by adding a policy to my custom connector.

In detail, I have added a policy based on a template. Afterwards, I have configured the operation, my transformation details, and I have specified when my policy should run. Finally, I saved my custom connector and tested the result … wow – my solution worked as expected.

Wow, that took less than 5 minutes … Let me say this: Policies in custom connectors are very powerful!

Share
Comments are closed.