簡體   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?

我在 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 stringreplace您可以將 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 函數,而您可以在邏輯應用程序中完成這項工作。

如果需要,請隨時詢問更多詳細信息:)

這種技術效果很好,並且只使用普通的 Logic App 操作:

    1.從聲明一個空數組變量開始(動作變量:初始化變量
    2. 遍歷您的項目(動作控制:對於每個),例如上一個動作的結果集
    • 在每次迭代中,首先組合您需要的 JSON 片段(操作數據操作:組合
    • 然后將您的 Compose 操作的輸出附加到數組中(操作:變量:附加到數組變量
    3.然后,在循環外,加入數組的元素(動作數據操作:加入
    4. 使用 Join 操作的輸出做您需要的操作,例如作為響應負載發送(操作Request: Response

這是它最終的樣子:

邏輯應用程序設計器屏幕截圖

您可以在 for-each 循環之外使用 @body(nestedLogicApp) 來訪問數組中所有嵌套邏輯應用的響應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM