【问题标题】:How to check if any values in an array is not null in Dataweave 2.0?如何在 Dataweave 2.0 中检查数组中的任何值是否不为空?
【发布时间】:2021-04-28 20:47:30
【问题描述】:

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

payload : [{

"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",

 "test3" : "test3",

 "Errors" : {

  }

}]`

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

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

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

item."test3" != "")

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

【问题讨论】:

  • 只是为了记录,Dataweave中的空字符串不是null
  • 是的,我明白这一点。我的目标是仅在特定键的验证失败时向数组添加错误,如果不可能,我需要提取成功行(错误中没有错误)。
  • 创建一个预期的输出,因为您的句子:I want to get an output where I only get all objects from Payload where Errors array has any key with not null values otherwise it should be filtered out. 对我来说毫无意义。根据您的具体输入形成我可以收集的内容,您应该有一个空数组。
  • @George 对不起,我到处都是,我更新了输入输出,希望现在更清楚一点。

标签: dataweave anypoint-studio mule-component mulesoft


【解决方案1】:

试试这个,代码后面有解释:

%dw 2.0
output application/json

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

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

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

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

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

【讨论】:

  • 我的输入输出错误,这确实解决了问题,但问题不是所有验证都只是 NULL/空检查。但我现在会接受这是一个解决方案,因为我可以通过一些调整从这些答案中得到我真正想要的。再次感谢您的解释,这非常有帮助。
【解决方案2】:

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

脚本:

%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",
    "test3": "test3",
    "Errors": {
      
    }
  }
]

【讨论】:

  • 它部分工作,假设我在数组中有第一个对象,其中 Errors 数组没有错误,然后数组中的第二个对象有一些错误,当我用 尝试这个时不是 它没有给出准确的结果:payload filter (valuesOf($.Errors) some not ($ != "")) 基本上我试图获取所有错误键都是“”的对象。我怎样才能得到这些对象?
  • 请更新不同案例的示例为每个案例添加预期输出。否则很难理解需要什么。
  • 我更新了答案以匹配新的输入和输出。请在以后的所有问题中说明它们。
  • 谢谢,非常感谢您抽出宝贵的时间。
【解决方案3】:

这就是你所追求的吗?

输入

[{
 "test": "test",
 "test2" : "test2",
 "test3" : "test3",
 "Errors" : {
  "test": null,
  "test2" : "Test2 is NULL",
  "test3" : ""
  }
},
{
 "test": "1231test123",
 "test2" : "123test23232",
 "test3" : "12421test3",
 "Errors" : {
  "test": "",
  "test2" : "",
  "test3" : ""
  }
},
{
 "test": "3asdsadasd",
 "test2" : "123123",
 "test3" : "d323e2d23",
 "Errors" : {
  "test": "123",
  "test2" : "",
  "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": "",
      "test2": "",
      "test3": ""
    }
  }
]

【讨论】:

  • 或者你可以只使用 !isEmpty 并且仍然通过执行有效负载从初始有效负载中删除它来达到所需的 - (满足条件的派生字段)
  • 这正是我正在寻找的,但正如你所说,如果验证失败,有没有办法只能将这些错误添加到 Errors 对象中?在这种情况下,我只需要检查错误对象的大小以查看是否有任何错误。如果有帮助,我可以再问一个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 2021-12-26
相关资源
最近更新 更多