简体   繁体   English

如何将 Azure 逻辑应用程序中 For_Each 循环的输出合并到单个平面阵列?

[英]How can I merge the outputs from a For_Each loop in an Azure Logic App to a single flat array?

I have a For_Each loop in an Azure Logic App that calls another, nested, Logic App.我在 Azure 逻辑应用程序中有一个For_Each循环,它调用另一个嵌套的逻辑应用程序。 The result from each iteration of the nested Logic Apps is a JSON object that contains an array of strings, like this:嵌套逻辑应用程序每次迭代的结果是一个包含字符串数组的 JSON 对象,如下所示:

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

So the output from my For_Each loop in the parent Logic App looks like this:因此,父逻辑应用程序中 For_Each 循环的输出如下所示:

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

I want to put all these strings into a single flat list that I can pass to another action.我想将所有这些字符串放入一个可以传递给另一个操作的平面列表中。

How can I do this?我怎样才能做到这一点? Is it possible using the workflow definition language and built-in functions, or do I need to use an external function (in a service, or an Azure Function)?是否可以使用工作流定义语言和内置函数,或者我是否需要使用外部函数(在服务或 Azure 函数中)?

There's a simpler solution, working with Array Variables.有一个更简单的解决方案,使用数组变量。 At the top level, outside the For Each loop, declare a variable with an InitializeVariable action:在顶层,在 For Each 循环之外,声明一个带有 InitializeVariable 操作的变量:

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

Inside the For Each, use a AppendToArrayVariable action.在 For Each 中,使用 AppendToArrayVariable 操作。 You can append the Response object of the Nested Logic App you just called.您可以附加您刚刚调用的嵌套逻辑应用程序的 Response 对象。

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

Hope it helps.希望能帮助到你。

Picking up on @DerekLi's useful comment above, it seems this is not possible at the time of writing with Logic Apps schema version 2016-06-01 .根据上面@DerekLi 的有用评论,在撰写 Logic Apps 架构版本2016-06-01时,这似乎是不可能的。

One of the great strengths of Logic Apps is the ability to leverage the power of Azure Functions to solve problems like this that can't (yet) be solved in the schema language. Logic Apps 的一大优势是能够利用 Azure Functions 的强大功能来解决这样的问题,这些问题(尚)无法在架构语言中解决。

Re-writing the array is trivial in c# within a function:在 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);
}

And this function can then be passed the result of the For_Each loop:然后可以将此函数传递给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"
}

There is also a way to do it using the workflow definition language.还有一种方法可以使用工作流定义语言来实现。 ( https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-workflow-definition-language ). https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-workflow-definition-language )。

Using the fonctions string and replace you can work on your json as a string rather than on objects.使用 fonctions stringreplace您可以将 json 作为字符串而不是对象处理。

Here is a Flat_List action that follows a Parse_JSON action with your data:这是一个Flat_List操作,它遵循Parse_JSON操作与您的数据:

Your data:您的数据:

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

Flat_List component: Flat_List 组件:

 "Flat_List": {
            "inputs": "@replace(replace(replace(string(body('Parse_JSON')),']},{\"Results\":[',','),'}]','}'),'[{','{')",
            "runAfter": {
                "Parse_JSON": [
                    "Succeeded"
                ]
            },
            "type": "Compose"
        },

What happens here?这里会发生什么? First we use string that takes your json data and gives:首先,我们使用string来获取您的 json 数据并给出:

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

We replace all the ]},{"Results":[ by , .我们将所有]},{"Results":[替换为,

We replace all the }] by } .我们将所有的}]替换为}

We replace all the [{ by { .我们将所有的[{替换为{

We get the string {"Results":["string a","string b","string c","string d"]}我们得到字符串{"Results":["string a","string b","string c","string d"]}

Then you are free to parse it back to json with:然后你可以自由地将它解析回 json:

"Parse_JSON_2": {
                "inputs": {
                    "content": "@outputs('Flat_List')",
                    "schema": {
                        "properties": {
                            "Results": {
                                "items": {
                                    "type": "string"
                                },
                                "type": "array"
                            }
                        },
                        "type": "object"
                    }
                },
                "runAfter": {
                    "Flat_List": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            }

You can see it as a proof of concept as the Azure Function may be easier to re-read later but there may be many reason not to want to instantiate a new Azure Function while you can do the job in Logic App.您可以将其视为概念证明,因为 Azure 函数以后可能更容易重新阅读,但可能有很多理由不想实例化新的 Azure 函数,而您可以在逻辑应用程序中完成这项工作。

Feel free to ask for more details if needed :)如果需要,请随时询问更多详细信息:)

This technique works pretty well, and only uses run-of-the-mill Logic App actions:这种技术效果很好,并且只使用普通的 Logic App 操作:

    1. start with declaring an empty array variable (action Variable: Initialise variable ) 1.从声明一个空数组变量开始(动作变量:初始化变量
    2. iterate through your items (action Control: For each ), eg the resultset from a previous action 2. 遍历您的项目(动作控制:对于每个),例如上一个动作的结果集
    • in each iteration, first compose the JSON fragment you need (action Data Operations: Compose )在每次迭代中,首先组合您需要的 JSON 片段(操作数据操作:组合
    • then append the output of your Compose action to the array (action: Variable: Append to array variable )然后将您的 Compose 操作的输出附加到数组中(操作:变量:附加到数组变量
    3. then, outside the loop, join the elements of the array (action Data Operations: Join ) 3.然后,在循环外,加入数组的元素(动作数据操作:加入
    4. do what you need with the output of the Join action, eg send as response payload (action Request: Response ) 4. 使用 Join 操作的输出做您需要的操作,例如作为响应负载发送(操作Request: Response

This is what it looks like in the end:这是它最终的样子:

逻辑应用程序设计器屏幕截图

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 base64 编码字符串解析 Azure 逻辑应用程序中的 JSON 数组,以在 For_each 中使用 - Parsing JSON array in Azure Logic App from base64 encoded string to use in For_each 逻辑应用程序:如何使用每个循环合并多个响应 - Logic app : How to merge multiple response using each loop Azure 逻辑应用程序:如何将单个 For Each 变量传递到执行存储过程步骤? - Azure Logic App: How do I pass a single For Each variable to an Execute Stored Procedure step? Azure 每个循环的逻辑应用程序每次发送多个 email。 怎么只发一个email? - Azure Logic App For Each loop sending multiple email each time. How to send only one email? 如何遍历 Azure 逻辑应用程序中 Json 主体内的数组? - How can I iterate over an array which is inside Json body in Azure Logic App? 如何利用 for_each 缩短我的 Terraform 代码? - How can I leverage for_each to shorten my Terraform code? 如何从Azure中的API应用程序调用逻辑应用程序? - How can I call my Logic App from API app in Azure? Terraform 来自多个模块输出的 NSG 规则,带有 for_each - Terraform NSG rules from multiple module outputs with for_each 如何从数组变量 [fileName 和 FileContent] 创建 zip 文件。 使用 azure function 或 Azure 逻辑应用程序(没有任何第三方服务) - How can I create a zip file from array variable [fileName and FileContent]. using azure function or Azure logic app(with out any third part service) 如何在 terraform for_each 中正确循环? - How to loop correctly in terraform for_each?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM