Power Apps Custom Connector XML to JSON

Let’s say you have a web API and you need to use it in Power Apps. You created a Custom Connector, set all your actions and then wanted to test it but your API returns only XML. Unfortunately Power Apps Custom Connectors cannot process XML. You need to convert it to JSON.

Thanks to Microsoft because you can add your custom code to convert your XML to JSON. According to Microsoft your custom code must be in C# and have a maximum execution time of 5 seconds. If your code exceeds 5 seconds, it won’t run. Below you can find the code to convert an XML return from an API to JSON format.

Custom Connector Code

public class Script: ScriptBase
{
    public override async Task<HttpResponseMessage> ExecuteAsync()
    {
        HttpResponseMessage response = await this.Context.SendAsync(this.Context.Request, this.CancellationToken).ConfigureAwait(continueOnCapturedContext: false);
        var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false);

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(responseString);
        string jsonText = JsonConvert.SerializeXmlNode(doc);

        var result = JObject.Parse(jsonText);
        response.Content = CreateJsonContent(result.ToString());
        return response;
    }
}

Place this code in Code (Preview) section of your custom connector. You can also save it as a file and upload it. Your code has to implement ExecuteAsync method. The class name has to be Script and must implement ScriptBase. Of course you can create other methods and they need to be called from ExecuteAsync method.

If you like to get more information about writing code in a custom connector please see this link.

Screenshot of Code (Preview)

Always test your connector.