英文:
DataWeave 1.0 lower function is not working as expected
问题
以下是您要的代码翻译部分:
{
"phone": "123-234-9872",
"Type": "Phone",
"aType": ["T1", "T2"]
}
期望的输出:
{
"aType": ["T1", "T2"]
}
英文:
I have a below function that checks for the string and removes the value.
It works fine when the string matches the case accurately. I tried to add lower to check for both possibilities in a given string. but it is not working as expected.
If the payload contains a value from filterList
it removes only the key and value that matches exactly as "phone"
. But, the expectation is to remove if key/value has "phone","Phone","secondaryPhone" etc
Input:
{
"phone":"123-234-9872",
"Type":"Phone",
"aType": ["T1", "T2"]
}
Expected output:
{
"aType": ["T1", "T2"]
}
Script:
%dw 1.0
%output application/json skipNullOn="everywhere"
%function remove(content, filterList)
content match {
:array -> $ map (value,index) -> remove(value, filterList),
:object -> $ mapObject (value,key) ->
(key): null when (filterList contains (lower key as :string))
otherwise remove(value, filterList)
,
default -> null when ((content !=null) and (filterList contains content as :string)) otherwise content
}
---
remove(payload,["phone","email","accountNumber"])
答案1
得分: 1
以下是您要翻译的内容:
问题在于默认情况下使用的条件不包括更改内容的大小写。该条件也有点复杂,因为脚本没有使用 match
来考虑内容为字符串时的情况。充分利用模式匹配的功能可以简化评估。
%dw 1.0
%output application/json skipNullOn="everywhere"
%function remove(content, filterList)
using (lowerFilters=filterList map (lower $)) (content match {
:array -> $ map (value,index) -> remove(value, lowerFilters),
:object -> $ mapObject (value,key) ->
(key): null when (lowerFilters contains (lower key as :string))
otherwise remove(value, lowerFilters)
,
:string -> null when (lowerFilters contains (lower content)) otherwise content,
default -> content
})
---
remove(payload,["phone","email","accountNumber"])
另一个改进是使用一个本地变量,将filterList
中的项也转换为小写。例如,"accountNumber"
即使原始版本中的实际值为 "accountnumber"
时也不匹配。
英文:
The problem is that the condition used for default doesn't include changing the case of the content. The condition is also a bit complex because the script is not using match
to consider the case for when content is a string. Using the full power of pattern matching simplifies the evaluation.
%dw 1.0
%output application/json skipNullOn="everywhere"
%function remove(content, filterList)
using (lowerFilters=filterList map (lower $)) (content match {
:array -> $ map (value,index) -> remove(value, lowerFilters),
:object -> $ mapObject (value,key) ->
(key): null when (lowerFilters contains (lower key as :string))
otherwise remove(value, lowerFilters)
,
:string -> null when (lowerFilters contains (lower content)) otherwise content,
default -> content
})
---
remove(payload,["phone","email","accountNumber"])
An additional improvement is to use a local variable that lowers also the items in filterList
. For example "accountNumber"
would not match even when the actual value is "accountnumber"
in the original version.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论