[英]Parsing JSON array in Azure Logic App from base64 encoded string to use in For_each
[英]How can I merge the outputs from a For_Each loop in an Azure Logic App to a single flat array?
我在 Azure 逻辑应用程序中有一个For_Each
循环,它调用另一个嵌套的逻辑应用程序。 嵌套逻辑应用程序每次迭代的结果是一个包含字符串数组的 JSON 对象,如下所示:
{
"Results": ["string a", "string b"]
}
因此,父逻辑应用程序中 For_Each 循环的输出如下所示:
[
{"Results": ["string a", "string b"]},
{"Results": ["string c", "string d"]}
]
我想将所有这些字符串放入一个可以传递给另一个操作的平面列表中。
我怎样才能做到这一点? 是否可以使用工作流定义语言和内置函数,或者我是否需要使用外部函数(在服务或 Azure 函数中)?
有一个更简单的解决方案,使用数组变量。 在顶层,在 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"
}
希望能帮助到你。
根据上面@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"
}
还有一种方法可以使用工作流定义语言来实现。 ( https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-workflow-definition-language )。
使用 fonctions string
和replace
您可以将 json 作为字符串而不是对象处理。
这是一个Flat_List
操作,它遵循Parse_JSON
操作与您的数据:
您的数据:
[
{"Results": ["string a", "string b"]},
{"Results": ["string c", "string d"]}
]
Flat_List 组件:
"Flat_List": {
"inputs": "@replace(replace(replace(string(body('Parse_JSON')),']},{\"Results\":[',','),'}]','}'),'[{','{')",
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "Compose"
},
这里会发生什么? 首先,我们使用string
来获取您的 json 数据并给出:
[{"Results":["string a", "string b"]},{"Results":["string c", "string d"]}]
我们将所有]},{"Results":[
替换为,
。
我们将所有的}]
替换为}
。
我们将所有的[{
替换为{
。
我们得到字符串{"Results":["string a","string b","string c","string d"]}
然后你可以自由地将它解析回 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"
}
您可以将其视为概念证明,因为 Azure 函数以后可能更容易重新阅读,但可能有很多理由不想实例化新的 Azure 函数,而您可以在逻辑应用程序中完成这项工作。
如果需要,请随时询问更多详细信息:)
您可以在 for-each 循环之外使用 @body(nestedLogicApp) 来访问数组中所有嵌套逻辑应用的响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.