JSONata 对数组进行排序/排序

JSONata sort / order by of an array

我想订购一个阵列。下面的 JSONata 表达式有一个传入数组,如下所示。

[{"id":"Air-1a",
  "Controller":"ESP62",
 "Cntr-TaskNo":10,
 "Cntr-GPIO":13,
 "name":"Air",
 "valueName":"Humidity",
 "Sensor":"DHT22",
 (and many other key pairs)}, 
 {next object}, ...]

然后我使用以下 JSONata 表达式转换数组:

payload.(
        { "Controller" : $.Controller,
          "Cntr-TaskNo": $.CntrDef.TaskNo,
          "Cntr-GPIO"  : $.CntrDef.GPIO,
          "name"       : $.name,
          "valueName"  : $.valueName,
          "Sensor"     : $.Sensor,
          "id"         : $.id
        }
) 

但现在我想 - 在同一个 JSONata 表达式中,首先对控制器进行排序,然后对 GPIO 进行排序。先尝试使用控制器。

我试过了:

payload.(
        { $sort("Controller",function($l, $r){$l.Controller > $r.Controller}) : $.Controller ,
          "Cntr-TaskNo": $.CntrDef.TaskNo,
          "Cntr-GPIO"  : $.CntrDef.GPIO,
          "name"       : $.name,
          "valueName"  : $.valueName,
          "Sensor"     : $.Sensor,
          "id"         : $.id
        }
) 

以及尝试使用 ~> 链接命令在末尾添加排序功能。我还尝试了 order-by 运算符。

谁能给我指出正确的方向?

//---------- 将 'ESP62' 更改为“-”的新流程不起作用:

[{"id":"874b0c77.f87418","type":"inject","z":"6f27a311.d135bc","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":180,"wires":[["8c196590.c20638"]]},{"id":"8c196590.c20638","type":"change","z":"6f27a311.d135bc","name":"Dataset","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"id\":\"Air-1a\",\"Controller\":\"ESP62\",\"CntrTaskNo\":10,\"CntrGPIO\":13,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"111\",\"bbb\":\"222\",\"ccc\":\"333\"},{\"id\":\"Air-2a\",\"Controller\":\"ESP72\",\"CntrTaskNo\":11,\"CntrGPIO\":14,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"444\",\"bbb\":\"555\",\"ccc\":\"666\"},{\"id\":\"Air-1a\",\"Controller\":\"ESP62\",\"CntrTaskNo\":2,\"CntrGPIO\":9,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"777\",\"bbb\":\"888\",\"ccc\":\"999\"},{\"id\":\"Air-1a\",\"Controller\":\"-\",\"CntrTaskNo\":10,\"CntrGPIO\":12,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"777\",\"bbb\":\"888\",\"ccc\":\"999\"}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":180,"wires":[["13981162.14e28f"]]},{"id":"c8a256a5.a170c8","type":"debug","z":"6f27a311.d135bc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":180,"wires":[]},{"id":"13981162.14e28f","type":"change","z":"6f27a311.d135bc","name":"Jsonata $sort","rules":[{"t":"set","p":"payload","pt":"msg","to":"($sort(payload,function($l , $r){$l.Controller > $r.Controller}) ; \t$sort(payload,function($l , $r){$l.CntrGPIO > $r.CntrGPIO}))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":180,"wires":[["c8a256a5.a170c8"]]}]

我建议首先对数据集进行排序,然后转换已经排序的对象数组。转换很简单,您想知道如何排序,所以我在下面展示了一种可能的解决方案。它使用带有两个串联的 $sort 函数的表达式。

在更好地理解要求后进行编辑。

我在更改节点中使用此表达式成功测试了 Node-RED 流:

($a := $sort(payload,function($l , $r){$l.Controller > $r.Controller}) ; $sort($a,function($l , $r){(($l.Controller = $r.Controller) and ($l.CntrGPIO > $r.CntrGPIO))}))

流(包含硬编码的数据集):

[{"id":"a7814b7e.3adeb8","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"8bf10833.c71748","type":"inject","z":"a7814b7e.3adeb8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":140,"wires":[["9e365564.edca08"]]},{"id":"9e365564.edca08","type":"change","z":"a7814b7e.3adeb8","name":"Dataset","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"id\":\"Air-1a\",\"Controller\":\"ESP62\",\"CntrTaskNo\":10,\"CntrGPIO\":13,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"111\",\"bbb\":\"222\",\"ccc\":\"333\"},{\"id\":\"Air-2a\",\"Controller\":\"ESP72\",\"CntrTaskNo\":11,\"CntrGPIO\":14,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"444\",\"bbb\":\"555\",\"ccc\":\"666\"},{\"id\":\"Air-1a\",\"Controller\":\"ESP62\",\"CntrTaskNo\":2,\"CntrGPIO\":9,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"777\",\"bbb\":\"888\",\"ccc\":\"999\"},{\"id\":\"Air-1a\",\"Controller\":\"-\",\"CntrTaskNo\":10,\"CntrGPIO\":12,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"777\",\"bbb\":\"888\",\"ccc\":\"999\"}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":140,"wires":[["762f6421.074fec"]]},{"id":"f827bddb.c9acd","type":"debug","z":"a7814b7e.3adeb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":140,"wires":[]},{"id":"762f6421.074fec","type":"change","z":"a7814b7e.3adeb8","name":"Jsonata $sort","rules":[{"t":"set","p":"payload","pt":"msg","to":"($a := $sort(payload,function($l , $r){$l.Controller > $r.Controller}) ; $sort($a,function($l , $r){(($l.Controller = $r.Controller) and ($l.CntrGPIO > $r.CntrGPIO))}))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":140,"wires":[["f827bddb.c9acd"]]}]

还在 Jsonata 练习器中进行了测试:http://try.jsonata.org/S1IlT3y-E

您可以使用以下表达式对数组进行排序:

payload^(Controller, CntrDef.GPIO)

排序运算符 ^ 将对数组进行排序,首先增加 Controller 的值,然后增加 CntrGPIO 的值。然后您可以转换该数组中的每个对象

payload^(Controller, CntrDef.GPIO).{
   "Controller" : Controller,
   "Cntr-TaskNo": CntrDef.TaskNo,
   "Cntr-GPIO"  : CntrDef.GPIO,
   "name"       : name,
   "valueName"  : valueName,
   "Sensor"     : Sensor,
   "id"         : id
}