简体   繁体   中英

Stored procedure in logic app outputs splitted xml object

I wrote a logic app that executes a stored procedure in an on-prem database with the on-prem gateway. In SQL Server Management Studio, I get the result as a complete xml object.

But when I execute that stored procedure in Azure logic apps my result of the same xml object gets split into multiple json objects instead of one json object with the full xml string inside.

In SQL Server Management Studio, I have FOR XML RAW, Root('<RootName>') after the SQL select statement.

Why is this, and how do I solve this problem?

Below is an screenshot:

在此处输入图片说明

I solved a similar issue by implementing simple Function App to fix the output.

In my case it is a JSON generated by SQL query FOR JSON AUTO, but you should be able to apply the same to XML. The following article mentions that

A large result set splits the long JSON string across multiple rows.

I implemented a simple Function App to process the output and consolidate it into single JSON payload.

Here is the code of my Function which is a Generic Webhook function:

    public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
    {
        log.Info($"ProcessSentimentJson was triggered!");

        string jsonContent = await req.Content.ReadAsStringAsync();

        StringBuilder sb = new StringBuilder();
        JArray array = JArray.Parse(jsonContent);

        List<string> list = array.Select(p => (string)p["JSON_F52E2B61-18A1-11d1-B105-00805F49916B"]).ToList();
        foreach (var l in list)
            sb.Append(l);

        return req.CreateResponse(HttpStatusCode.OK, sb.ToString());
    }

Then you can add it to your Logic App, pass the results from the stored proc and use the output in the next activity:

在此处输入图片说明

Happy coding!

Solution by Jakub is indeed one way to solve it, but comes with an additional cost (azure function and logic app action to call the function).

Another way would be to return the XML from SQL as a base64 string. That way you use the base64ToString and xml functions in your Logic App.

The Azure Function solution worked great, I just coded it exactly how it was described. The only hard part is getting the references working in azure. Note: this works with any type of string data, I was using XML in my case.

Here's the code I used:

#r "Newtonsoft.Json"
#r "System.Xml.Linq"
#r "System.Linq"

using System;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;
using System.Xml.Linq;
using System.Linq;

public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info($"Webhook Combine Messages was triggered!");

    string jsonContent = await req.Content.ReadAsStringAsync();

    StringBuilder sb = new StringBuilder();
    JArray array = JArray.Parse(jsonContent);

    List<string> list = array.Select(p => (string)p["XML_F52E2B61-18A1-11d1-B105-00805F49916B"]).ToList();
    foreach (var l in list)
        sb.Append(l);

    return req.CreateResponse(HttpStatusCode.OK, sb.ToString());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM