如何将Azure Logic App中的For_Each循环的输出合并到单个平面阵列?

时间:2017-05-05 18:11:54

标签: azure azure-logic-apps azure-app-service-envrmnt

我在Azure Logic App中有一个For_Each循环,它调用另一个嵌套的Logic App。嵌套逻辑应用程序的每次迭代的结果都是一个包含字符串数组的JSON对象,如下所示:

{
 "Results": ["string a", "string b"]
}

因此,父逻辑应用程序中的For_Each循环的输出如下所示:

[
 {"Results": ["string a", "string b"]},
 {"Results": ["string c", "string d"]}
]

我想将所有这些字符串放入一个可以传递给另一个动作的单个平面列表中。

我该怎么做?是否可以使用工作流定义语言和内置函数,还是需要使用外部函数(在服务或Azure函数中)?

4 个答案:

答案 0 :(得分:5)

使用Array Variables是一个更简单的解决方案。 在顶层,在For Each循环之外,使用InitializeVariable操作声明变量:

"Initialize_Items_variable": {
    "inputs": {
        "variables": [
            {
                "name": "Items",
                "type": "Array",
                "value": []
            }
        ]
    },
    "runAfter": {},
    "type": "InitializeVariable"
}

在For Each中,使用AppendToArrayVariable操作。您可以附加刚刚调用的嵌套逻辑应用程序的Response对象。

"Append_to_Items_variable": {
    "inputs": {
        "name": "Items",
        "value": "@body('Nested_Logic_App_Response')"
    },
    "runAfter": {
    },
    "type": "AppendToArrayVariable"
}

希望它有所帮助。

答案 1 :(得分:1)

在上面提到@ DerekLi的有用评论时,在撰写Logic Apps架构版本2016-06-01时似乎无法做到这一点。

Logic Apps的一大优势是能够利用Azure Functions的强大功能来解决这样的问题,这些问题无法(尚未)在模式语言中得到解决。

在函数c#中重写数组是微不足道的:

using System.Net;

public class Result
{
    public List<string> Results {get; set;}
}

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    var inputs = await req.Content.ReadAsAsync<List<Result>>();
    var outputs = new List<string>();

    foreach(var item in inputs)
    {
        log.Info(item.Results.ToString());
        outputs.AddRange(item.Results.Where(x => !string.IsNullOrEmpty(x)));
    }

    return req.CreateResponse(HttpStatusCode.OK, outputs);
}

然后可以将此函数传递给For_Each循环的结果:

"MyFunction": {
    "inputs": {
                "body": "@body('Parse_JSON')",
                "function": {
                    "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Web/sites/{function-app-name}/functions/{function-name}"
                },
                "method": "POST"
            },
            "runAfter": {
                "For_each": [
                    "Succeeded"
                ]
            },
            "type": "Function"
}

答案 2 :(得分:0)

您可以在for-each循环之外使用@body(nestedLogicApp)来访问所有嵌套的逻辑应用程序&#39;数组中的响应。

答案 3 :(得分:0)

此技术效果很好,并且仅使用常规的Logic App操作:

    1。首先声明一个空数组变量(操作变量:初始化变量
    2。遍历您的项目(操作 Control:针对每个对象),例如上一个动作的结果集
    • 在每次迭代中,首先组成所需的JSON片段(操作数据操作:撰写
    • 然后将您的Compose操作的输出附加到数组(操作:变量:追加到数组变量
    3。然后,在循环外部,连接数组的元素(操作数据操作:连接
    4。使用Join操作的输出来执行所需的操作,例如作为响应负载发送(操作请求:响应

这是最终的样子:

Logic App Designer screenshot