在Mulesoft DWL中,如何使用循环映射有效负载,直到循环中为null

问题描述

嗨,我有一个json负载,其中有如下所示的循环。

{
   "ABC":[
      {
         "BCD":[
            {
               "ABC":[
                  {
                     "BCD":[
                        {
                           "ABC":[
                              {
                                 "BCD":[]-> Repeats till we get null
                              }
                           ]
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}

上面的循环将继续直到我们得到空值,该空值将因不同的有效负载而有所不同。是他们提供逻辑的一种方法,就像循环应该继续进行,直到我们得到空值。我的输出应该是XML

解决方法

这是在datawave脚本中模拟do-while的一个小技巧。

指示迭代结束(模拟while)。 最简单的方法是将值用作累加器并使用符号作为指标。在累积汇总值时,负结果表示循环已结束。

%dw 2.0
var x=[1,2,3,4,5]
output application/json
---
-(x reduce (item,acc=0) -> if (item <4 and acc >= 0) acc + item else if (acc>0) -acc else acc) 

一些复杂的对象可以用来收集结果,也可以将周期结束的指示符作为对象的一部分

%dw 2.0
var x=[1,5]
output application/json
---
(x reduce (item,acc={sum:0}) -> if (item < 4 and acc.end==null ) (acc - 'sum' ++ {sum: acc.sum+item}) else ( acc ++ {end:true} )).sum

使用条件(如null)终止循环。

https://simpleflatservice.com/mule4/DoWhileImitation.html

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...