Mirth Connect 频道目标排序 JSON 对象数组

Mirth Connect Channel destination sort JSON array of objects

我需要对发送到 Mirth 的一组对象进行排序。最初排序是通过 XSLT 完成的(因为入站数据是 XML),但是随着(我无法控制的)变化,入站数据被更改为 JSON。原始排序是在我保留的目标转换中完成的。

入站 JSON 示例:

{
  "Id":"100001",
  "Set":
  {
    "unimportantdata1":null,
    "unimportantdata2":null,
    "unimportantdata3":"0001-01-01T00:00:00",
    "unimportantdata4":"0001-01-01T00:00:00",
    "ArrayToSort":[
      {
        "vt":"blah",
        "Num":"2",
        "desc":"dp",
        "Value":["1.1","1.2"],
        "Time":"2020-03-23T02:23:41",
        "blah": { "Name": { "LastName":"ob-ln","Firstname":"ob-fn","MiddleName":"ob-mi","Title":null}}
      },
      {
        "vt":"yadda",
        "Num":"1",
        "desc":"dp",
        "Value":["1.1","1.2"],
        "Time":"2020-03-23T02:23:41",
        "blah": { "Name":{"LastName":"ob-ln","Firstname":"ob-fn","MiddleName":"ob-mi","Title":null}}
      }
   ]}
}

我需要按 "Num" 属性 升序排序的 ArrayToSort。

我的问题是:

  1. 是在目标通道转换器中对此进行排序的最佳位置吗?
  2. 我猜要走的路是 JavaScript,但我不确定如何继续 -- 建议?

提前致谢

事实证明解决方案非常简单。

在JavaScript中,您只需要做以下事情:

msg['Set']['ArrayToSort'].sort(function(a,b){
    return a["Num"] - b["Num"];
});

logger.info(JSON.stringify(msg));

可以使用 Array.sort 进行排序。并且可以更新数据

let data = {
  Id: "100001",
  Set: {
    unimportantdata1: null,
    unimportantdata2: null,
    unimportantdata3: "0001-01-01T00:00:00",
    unimportantdata4: "0001-01-01T00:00:00",
    ArrayToSort: [
      {
        vt: "blah",
        Num: "2",
        desc: "dp",
        Value: ["1.1", "1.2"],
        Time: "2020-03-23T02:23:41",
        blah: {
          Name: {
            LastName: "ob-ln",
            Firstname: "ob-fn",
            MiddleName: "ob-mi",
            Title: null
          }
        }
      },
      {
        vt: "yadda",
        Num: "1",
        desc: "dp",
        Value: ["1.1", "1.2"],
        Time: "2020-03-23T02:23:41",
        blah: {
          Name: {
            LastName: "ob-ln",
            Firstname: "ob-fn",
            MiddleName: "ob-mi",
            Title: null
          }
        }
      }
    ]
  }
};
data.Set.ArrayToSort = data.Set.ArrayToSort.sort((x, y) => x.Num - y.Num);
console.log(JSON.stringify(data, null, 2));