Arrays 如何在按其他属性分组的逻辑应用程序中组合JSON行
我有一个逻辑应用程序,它正在从写入应用程序洞察的应用程序中获取失败的运行,我希望将所有错误按操作名称分组到一条消息中。有人能解释一下怎么做吗 我的起始数据如下所示:Arrays 如何在按其他属性分组的逻辑应用程序中组合JSON行,arrays,json,azure-logic-apps,Arrays,Json,Azure Logic Apps,我有一个逻辑应用程序,它正在从写入应用程序洞察的应用程序中获取失败的运行,我希望将所有错误按操作名称分组到一条消息中。有人能解释一下怎么做吗 我的起始数据如下所示: [{ "messageError": "Notification sent to AppName but not received for request: 20200213215520_hUu22w9RZlyc, user email@email.com Status: NotFound", "transactionKey
[{ "messageError": "Notification sent to AppName but not received for request: 20200213215520_hUu22w9RZlyc, user email@email.com Status: NotFound",
"transactionKey": "20200213215520_hUu22w9RZlyc"},
{ "messageError": "App to App Import Request: 20200213215520_hUu22w9RZlyc from user email@email.com was unable to insert to following line(s) into App with error(s) :\r\n Line 123: Unable to unlock this record.",
"transactionKey": "20200213215520_hUu22w9RZlyc"}]
我试图从中得到的是一行,它将两个messageError值连接到一个公共事务键上的一个语句中。大概是这样的:
[{ "messageErrors": [{"Notification sent to AppName but not received for request: 20200213215520_hUu22w9RZlyc, user email@email.com Status: NotFound"},
{"App to App Import Request: 20200213215520_hUu22w9RZlyc from user email@email.com was unable to insert to following line(s) into App with error(s) :\r\n Line 123: Unable to unlock this record."}],
"transactionKey": "20200213215520_hUu22w9RZlyc"}]
数据集中可能有多达20行,并且连接需要足够智能,以便仅当有多行具有相同transactionKey时才进行分组。有没有人这样做过,并对如何对它们进行分组提出了建议?对于这个需求,我认为我们可以使用liquid template在开始时对json数据执行“分组方式”操作。但根据一些测试,azure logic应用程序似乎不支持其液体模板中的“分组方式”。因此,我们可以选择两种解决方案: 答:一种解决方案是在logic app中通过“For each”循环、“If”条件执行这些操作,组合json数据和许多其他操作,并且我们必须初始化许多变量。我首先尝试了这个解决方案,但在logic应用程序中创建了这么多操作之后,我放弃了它。太复杂了 另一个解决方案是在logic app中调用azure函数,我们可以在函数代码中对json数据进行操作。这也不容易,但我认为这比第一个解决方案要好。所以我尝试了这个解决方案,并取得了成功。请参考以下步骤: 1。我们需要一个带有“HTTP”触发器的azure函数应用程序 2.在您的“HTTP”触发器中,请参阅下面的代码:
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
string body = await req.Content.ReadAsStringAsync();
JArray array = JArray.Parse(body);
JArray resultArray = new JArray();
JObject tempObj = new JObject();
foreach (var obj in array)
{
JObject jsonObj = JObject.Parse(obj.ToString());
string transactionKey = jsonObj.GetValue("transactionKey").ToString();
string messageError = jsonObj.GetValue("messageError").ToString();
Boolean hasKey = false;
foreach (var item in tempObj)
{
JObject jsonItem = (JObject)item.Value;
string keyInItem = jsonItem.GetValue("transactionKey").ToString();
if (transactionKey.Equals(keyInItem))
{
hasKey = true;
break;
}else
{
hasKey = false;
}
}
if (hasKey.Equals(false))
{
JObject newObj = new JObject();
JArray newArr = new JArray();
newArr.Add(messageError);
newObj.Add("transactionKey", transactionKey);
newObj.Add("messageErrors", newArr);
tempObj.Add(transactionKey, newObj);
}
else
{
JObject oldObj = (JObject)tempObj.GetValue(transactionKey);
JArray oldArr = (JArray)oldObj.GetValue("messageErrors");
oldArr.Add(messageError);
oldObj.Property("messageErrors").Remove();
oldObj.Add("messageErrors", oldArr);
tempObj.Property(transactionKey).Remove();
tempObj.Add(transactionKey, oldObj);
}
}
foreach (var x in tempObj)
{
resultArray.Add(x.Value);
}
return resultArray;
}
非常感谢。我将把这个标记为答案,因为它解决了这个问题,尽管它确实保留了逻辑应用程序。在逻辑应用程序中使用Javascript步骤会更好,这正是我将尝试使用您的代码作为基础的地方。
[
{
"transactionKey": "20200213215520_hUu22w9RZlyc",
"messageErrors": [
"xxxxxxxxx",
"yyyyyyyy"
]
},
{
"transactionKey": "keykey",
"messageErrors": [
"testtest11",
"testtest22",
"testtest33"
]
}
]