2017-05-05 18 views
2

我在Azure Logic App中有一個For_Each循環,它調用另一個嵌套的Logic App。從嵌套邏輯應用的每一次迭代的結果是包含一個字符串數組,像這樣的JSON對象:如何將Azure邏輯應用程序中的For_Each循環的輸出合併到單個平面陣列?

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

所以從在父邏輯應用我的for_each環路中的輸出如下所示:

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

我想將所有這些字符串放入一個單獨的列表中,我可以將其傳遞給另一個操作。

我該怎麼做?是否有可能使用工作流定義語言和內置函數,還是需要使用外部函數(在服務或Azure函數中)?

+1

我們將介紹對變量的數組支持,在這些變量中,您可以輕鬆地將對象追加到數組中以滿足您的確切場景。 –

+0

@DerekLi謝謝。爲了解決這個問題,把它變成一個答案。 – JcFx

+0

實際上有另一種訪問您預期的數據的方式,添加了一個答案。 –

回答

1

在上面@ DerekLi的有用評論中,看起來在使用Logic Apps架構版本2016-06-01編寫本文時,這是不可能的。

Logic Apps的強大優勢之一是能夠利用Azure函數的強大功能來解決這樣的問題,這些問題無法在模式語言中解決。

重新寫入陣列在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" 
} 
0

還有一種方式使用工作流定義語言來完成。 (https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-workflow-definition-language)。

使用功能stringreplace你可以使用字符串而不是對象來處理你的json。

這裏是一個遵循Parse_JSON行動,您的數據Flat_List動作:

您的數據:

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

Flat_List組件:

​​

這裏會發生什麼?首先,我們使用的是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的功能可能更容易重新稍後再讀,但可能有很多原因,當您可以在Logic App中執行作業時,不想實例化新的Azure功能。

如果需要隨意問更多細節:)

0

可以使用@body(nestedLogicApp)的for-each循環的外部訪問陣列中的所有嵌套的邏輯Apps的響應。

3

有一個更簡單的解決方案,使用數組變量。 在頂層,外對於每個循環,聲明一個變量與InitializeVariable動作:

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

裏面的對於每個,使用AppendToArrayVariable動作。您可以追加剛剛調用的嵌套邏輯應用程序的Response對象。

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

希望它有幫助。

+0

剛剛在我的邏輯應用中使用了這個,感謝發佈。 – Beefcake

相關問題