我如何在由另一个属性分组的逻辑应用程序中合并JSON行

问题描述 投票:0回答:1

我有一个逻辑应用程序,它从应用程序编写到应用程序见解均执行失败的运行,并且我希望通过操作名称将所有错误分组为一条消息。有人可以解释如何做吗?

我的起始数据如下:

[{ "messageError": "Notification sent to AppName but not received for request: 20200213215520_hUu22w9RZlyc, user [email protected] Status: NotFound", 
   "transactionKey": "20200213215520_hUu22w9RZlyc"},
 { "messageError": "App to App Import Request: 20200213215520_hUu22w9RZlyc from user [email protected] 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 protected] Status: NotFound"}, 
                     {"App to App Import Request: 20200213215520_hUu22w9RZlyc from user [email protected] 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的情况下进行分组。有没有人做过,并且对如何分组有建议?

arrays json azure-logic-apps
1个回答
0
投票

为此要求,我认为我们可以在一开始就使用液体模板对您的json数据执行“分组依据”操作。但是根据一些测试,似乎azure逻辑应用程序在其液体模板中不支持“分组依据”。因此,有两种解决方案可供我们选择:

A。一种解决方案是通过“ For each”循环,“ If”条件和组合json数据在逻辑应用程序中进行这些操作,并且我们还必须初始化这么多变量。我首先尝试了此解决方案,但在逻辑应用程序中创建了很多动作之后就放弃了。太复杂了。

B。另一种解决方案是在逻辑应用程序中调用azure函数,我们可以在函数中对json数据进行操作。这也不容易,但是我认为它比第一个解决方案要好。因此,我尝试了此解决方案并获得了成功。请参考以下步骤:

1。我们需要create一个带有“ HTTP”触发器的天蓝色功能应用程序。

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”的变量,以模拟您的场景。enter image description here

4。然后单击create function in your logic app并选择您刚才创建的“ HTTP”触发器。

5。运行逻辑应用程序后,我们可以得到如下所示的结果:

[
  {
    "transactionKey": "20200213215520_hUu22w9RZlyc",
    "messageErrors": [
      "xxxxxxxxx",
      "yyyyyyyy"
    ]
  },
  {
    "transactionKey": "keykey",
    "messageErrors": [
      "testtest11",
      "testtest22",
      "testtest33"
    ]
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.