程序问答   发布时间:2022-05-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在 Dataweave 2.0 中检查数组中的任何值是否为空?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何在 Dataweave 2.0 中检查数组中的任何值是否为空??

开发过程中遇到如何在 Dataweave 2.0 中检查数组中的任何值是否为空?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在 Dataweave 2.0 中检查数组中的任何值是否为空?的解决方法建议,希望对你解决如何在 Dataweave 2.0 中检查数组中的任何值是否为空?有所启发或帮助;

我正在将我的有效负载映射到一个新的有效负载,并在输出如下所示的位置添加一个 Errors 数组。 :

payload : [{

"test: "test","test2" : "","test3" : "test3"

},{

"test: "test","test2" : "test2","test3" : "test3"

}]

预期输出:`有效载荷:[{

"test: "test","test3" : "test3","Errors" : {

  "test2" : "Test2 is NulL"

  }

},"Errors" : {

  }

}]`

预期输出:我想获得一个输出,其中我只从 Payload 获取所有对象,其中 Errors 数组具有任何非空值的键,否则应将其过滤掉。

我使用下面的表达式来实现,但这并不可行,因为它需要我为 Errors 数组中的每个键添加空检查。

errArr."Errors" filter ((item,indeX) -> item."test" != "" or item."test2" != "" or

item."test3" != "")

必须有更好的方法来做到这一点?有没有办法只检查每个项目(键)的值而不定义它们的名称?

解决方法

请注意,您的输入有一些错误。

脚本:

%dw 2.0
import * from dw::core::Arrays
import * from dw::core::Objects
output application/json
---
payload map {
    ($),Errors: $ 
                filterObject ((value,key) ->  isEmpty(value)) 
                mapObject ((value,key) ->  (key): key ++ " is NULL") 
}

输入:

[
    {
        "test": "test","test2" : "","test3" : "test3"
    },{
        "test": "test","test2" : "test2","test3" : "test3"
    }
]

输出:

[
  {
    "test": "test","test2": "","test3": "test3","Errors": {
      "test2": "test2 is NULL"
    }
  },{
    "test": "test","test2": "test2","Errors": {
      
    }
  }
]
,

试试这个,解释如下:

%dw 2.0
output application/json

var data = [{
    "test": "test","test3" : "test3"
    
    }
]

---
data map {
    ($),errors: $ mapObject (v,k) -> (
        if (isEmpty(v)) {(k): "$(k) is empty"} else {}
    )
}

这是算法以及文档中的链接:

  1. 使用 @H_485_5@map 迭代数组中的对象
  2. 创建一个@R_197_11198@并在@R_197_11198@中添加现有的(键,值)对 使用 dynamic elements 功能。
  3. errors 字段添加到@R_197_11198@。使用 @H_485_5@mapObject 函数计算 errors 对象,该函数标识所有空字段(注意我说的是空字段,而不仅仅是 null)。

我给您的建议是,确保您在提问时提供范围适当的输入和输出示例数据集。这将确保您能更及时地获得问题的答案。

,

这就是你所追求的吗?

输入

[{
 "test": "test","test3" : "test3","Errors" : {
  "test": null,"test2" : "Test2 is NULL","test3" : ""
  }
},{
 "test": "1231test123","test2" : "123test23232","test3" : "12421test3","Errors" : {
  "test": "",{
 "test": "3asdsadasd","test2" : "123123","test3" : "d323e2d23","Errors" : {
  "test": "123","test3" : ""
  }
}
]

脚本

%dw 2.0
import * from dw::core::Arrays
import * from dw::core::Objects
output application/json
---
payload -- (payload map $ filter ( valuesOf( $.Errors ) some ( !isEmpty($) and ($ != null) and sizeOf($) >0)))

输出

[
  {
    "test": "1231test123","test2": "123test23232","test3": "12421test3","Errors": {
      "test": "","test3": ""
    }
  }
]

大佬总结

以上是大佬教程为你收集整理的如何在 Dataweave 2.0 中检查数组中的任何值是否为空?全部内容,希望文章能够帮你解决如何在 Dataweave 2.0 中检查数组中的任何值是否为空?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。