如何在由另一个 属性 分组的逻辑应用程序中合并 JSON 行
How can I combine JSON rows in a logic app grouped by another property
我有一个逻辑应用程序,它从应用程序写入到应用程序洞察力运行失败,我想按操作名称将所有错误分组到一条消息中。有人可以解释如何做到这一点吗?
我的起始数据如下:
[{ "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数据做"group by"操作。但根据一些测试,似乎 azure logic 应用程序在其 liquid 模板中不支持 "group by"。所以有两种方案供我们选择:
一个。一种解决方案是通过 "For each" 循环、"If" 条件、组合 json 数据和许多其他操作在逻辑应用程序中执行这些操作,我们还必须初始化许多变量。我首先尝试了这个解决方案,但在逻辑应用程序中创建了这么多操作后我放弃了它。太复杂了。
乙。另一种解决方案是在logic app中调用一个azure函数,我们可以在函数代码中对json数据进行操作。这也不容易,但我认为它比第一个解决方案更好。所以我尝试了这个解决方案并获得了成功。请参考以下步骤:
1. 我们需要 create 带有 "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;
}
3. 测试并保存函数,然后转到您的逻辑应用程序。在逻辑应用程序中,我使用下面的 json 数据初始化一个名为 "data" 的变量来模拟您的场景。
4. 然后 create function in your logic app 并选择您刚才创建的 "HTTP" 触发器。
5.逻辑应用运行后,我们可以得到如下结果:
[
{
"transactionKey": "20200213215520_hUu22w9RZlyc",
"messageErrors": [
"xxxxxxxxx",
"yyyyyyyy"
]
},
{
"transactionKey": "keykey",
"messageErrors": [
"testtest11",
"testtest22",
"testtest33"
]
}
]
我有一个逻辑应用程序,它从应用程序写入到应用程序洞察力运行失败,我想按操作名称将所有错误分组到一条消息中。有人可以解释如何做到这一点吗?
我的起始数据如下:
[{ "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数据做"group by"操作。但根据一些测试,似乎 azure logic 应用程序在其 liquid 模板中不支持 "group by"。所以有两种方案供我们选择:
一个。一种解决方案是通过 "For each" 循环、"If" 条件、组合 json 数据和许多其他操作在逻辑应用程序中执行这些操作,我们还必须初始化许多变量。我首先尝试了这个解决方案,但在逻辑应用程序中创建了这么多操作后我放弃了它。太复杂了。
乙。另一种解决方案是在logic app中调用一个azure函数,我们可以在函数代码中对json数据进行操作。这也不容易,但我认为它比第一个解决方案更好。所以我尝试了这个解决方案并获得了成功。请参考以下步骤:
1. 我们需要 create 带有 "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;
}
3. 测试并保存函数,然后转到您的逻辑应用程序。在逻辑应用程序中,我使用下面的 json 数据初始化一个名为 "data" 的变量来模拟您的场景。
4. 然后 create function in your logic app 并选择您刚才创建的 "HTTP" 触发器。
5.逻辑应用运行后,我们可以得到如下结果:
[
{
"transactionKey": "20200213215520_hUu22w9RZlyc",
"messageErrors": [
"xxxxxxxxx",
"yyyyyyyy"
]
},
{
"transactionKey": "keykey",
"messageErrors": [
"testtest11",
"testtest22",
"testtest33"
]
}
]