[英]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 string
和replace
您可以将 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 操作:
This is what it looks like in the end:这是它最终的样子:
您可以在 for-each 循环之外使用 @body(nestedLogicApp) 来访问数组中所有嵌套逻辑应用的响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.