英文:
How to remove an entry from JSON using dataweave?
问题
I understand that you want a translation of the code portion you provided. Here is the translated code:
%dw 2.0
output application/json
var keyToRemove = "queueNumber|name|uniqueNumber|cur|UserId" splitBy "|"
fun removePair(e, keyToRemove) =
e match {
case is Array -> e map removePair($, keyToRemove)
case is Object -> e mapObject (v, k) ->
if (keyToRemove contains(k as String) or keyToRemove contains(v as String))
{}
else
{(k): removePair(v, keyToRemove)}
else -> e
}
---
removePair(payload, keyToRemove)
Please note that this translation retains the structure and logic of the original code while converting it to Chinese characters.
英文:
I have a json like below:
{ "queueNumber": "123", "field": "name", "UserId": [ 12, 12, 34 ], "test": [ { "objectName": "test", "uniqueNumber": "123456" } ] }
I have a function which iterates over each key and removes it if matches. But, I also need to check for the value. I tried with the below function it works perfect when it comes to check and remove the key. But, it is giving an exception when I am checking for the value.
%dw 2.0
output application/json
var keyToRemove = "queueNumber|name|uniqueNumber|cur|UserId" splitBy "|"
fun removePair(e, keyToRemove) =
e match {
case is Array -> e map removePair($, keyToRemove)
case is Object -> e mapObject (v, k) ->
if (keyToRemove contains(k as String) or keyToRemove contains(v as String))
{}
else
{(k): removePair(v, keyToRemove)}
else -> e
}
---
removePair(payload, keyToRemove)
Error:
Cannot coerce String (queueNumber) to Boolean if (keyToRemove contains(k as String) or keyToRemove contains(v as String))
How to enhance this function to also check and remove if any one of the given fields matches the value?
答案1
得分: 1
以下是翻译好的部分:
要从对象中删除单个属性的语法如下:
{
"queueNumber": "123",
"UserId": [
12,
12,
34
],
"test": [
{
"objectName": "test",
"uniqueNumber": "123456"
}
]
}
要删除单个属性后的结果:
{
"queueNumber": "123",
"UserId": [
12,
12,
34
],
"test": [
{
"objectName": "test",
"uniqueNumber": "123456"
}
]
}
要删除多个属性(数组)的语法如下:
{
"queueNumber": "123",
"UserId": [
12,
12,
34
]
}
要删除多个属性后的结果:
{
"queueNumber": "123",
"UserId": [
12,
12,
34
]
}
要删除单个嵌套属性,可以使用以下方法:
{
"queueNumber": "123",
"field": "name",
"UserId": [
12,
12,
34
],
"test": [
{
"objectName": "test"
}
]
}
要删除单个嵌套属性后的结果:
{
"queueNumber": "123",
"field": "name",
"UserId": [
12,
12,
34
],
"test": [
{
"objectName": "test"
}
]
}
要删除多个嵌套属性,请使用以下方法:
{
"field": "name",
"UserId": [
12,
12,
34
],
"test": [
{
"objectName": "test"
}
]
}
英文:
Assuming, this is your input:
{
"queueNumber": "123",
"field": "name",
"UserId": [
12,
12,
34
],
"test": [
{
"objectName": "test",
"uniqueNumber": "123456"
}
]
}
To remove a single property from an object the syntax is like this:
%dw 2.0
output application/json
---
payload -"field"
The result:
{
"queueNumber": "123",
"UserId": [
12,
12,
34
],
"test": [
{
"objectName": "test",
"uniqueNumber": "123456"
}
]
}
To remove multiple properties (array), this is the syntax:
%dw 2.0
output application/json
---
payload -- ["field", "test"]
The result:
{
"queueNumber": "123",
"UserId": [
12,
12,
34
]
}
To remove a single nested property, it can be done like this:
%dw 2.0
output application/json
fun removePair(e, key) =
e match {
case is Array -> e map removePair($, key)
case is Object -> e mapObject (v, k) ->
if ((k as String) == key)
{}
else
{(k): removePair(v, key)}
else -> e
}
---
removePair(payload , 'uniqueNumber')
The result:
{
"queueNumber": "123",
"field": "name",
"UserId": [
12,
12,
34
],
"test": [
{
"objectName": "test"
}
]
}
To remove multiple nested properties use this:
%dw 2.0
output application/json
fun removePair(e, key) =
e match {
case is Array -> e map removePair($, key)
case is Object -> e mapObject (v, k) ->
if (key contains (k as String))
{}
else
{(k): removePair(v, key)}
else -> e
}
---
removePair(payload , ['uniqueNumber', "queueNumber"])
The result:
{
"field": "name",
"UserId": [
12,
12,
34
],
"test": [
{
"objectName": "test"
}
]
}
答案2
得分: 0
以下是您要翻译的部分:
在条件中有几个错误。首先,您应该使用括号,以便正确评估表达式的顺序。其次 - 由于第一个错误,您尚未遇到此错误 - 您试图将 v 转换为字符串,但当它是一个数组时,无法这样做。因此,您应该更新表达式,类似于:
case is Object -> e mapObject (v, k) -> if ((keyToRemove contains (k as String)) or ((v is String) and (keyToRemove contains (v as String))))
说到这一点,您可能希望考虑在脚本中使用 filterObject() 来删除不需要的成员。这使得转换的意图更清晰,更容易理解:
%dw 2.0
output application/json
var keyToRemove = ["queueNumber","name","uniqueNumber","cur","UserId"]
fun removePair(e, keyToRemove) =
e match {
case is Array -> e map removePair($, keyToRemove)
case is Object ->
e
filterObject ((v, k) -> !((keyToRemove contains (k as String)) or ((v is String) and (keyToRemove contains (v as String)))))
mapObject ($$):removePair($,keyToRemove)
else -> e
}
---
removePair(payload, keyToRemove)
输出(对于问题中的输入):
{
"test": [
{
"objectName": "test"
}
]
}
请注意,由于您的原始文本中包含了 HTML 编码的引号("),我在翻译中保留了这些引号。如果需要,您可以将其还原为正常的双引号。
英文:
There are several errors in the condition. First you should use parenthesis so the expression is evaluated in the correct order. Secondly -you haven't got yet this error because of the first one- you are trying to convert v to a String but when it is an array it is not possible to do that. So you should update the expression to something like:
case is Object -> e mapObject (v, k) -> if ((keyToRemove contains (k as String)) or ((v is String) and (keyToRemove contains (v as String))))
Having said that you may want to consider using filterObject() in your script to remove unwanted members. This makes the intention of the transformation clearer and easier to understand:
%dw 2.0
output application/json
var keyToRemove = ["queueNumber","name","uniqueNumber","cur","UserId"]
fun removePair(e, keyToRemove) =
e match {
case is Array -> e map removePair($, keyToRemove)
case is Object ->
e
filterObject ((v, k) -> !((keyToRemove contains (k as String)) or ((v is String) and (keyToRemove contains (v as String)))))
mapObject ($$):removePair($,keyToRemove)
else -> e
}
---
removePair(payload, keyToRemove)
Output (for input in the question):
{
"test": [
{
"objectName": "test"
}
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论