我是MuleSoft新手,正在为DataWeave的一个场景而苦苦挣扎。有人能帮我找到一个优化的方法来合并两个有效负载以获得所需的输出吗? payload1有字段,payload2有这些字段的规则集;我们需要用相应的规则来组合每个字段;例如,“name”字段需要用“name”规则组合,类似的“roll”字段也需要用“roll”规则组合(它们可能是单个字段的多个规则);等等:
数据:
payload1 =
[
{
"ID" : "1",
"Name" : "ABC",
"Roll" : 123,
"Address" : "PQR-234",
"Standard" : "5"
},
{
"ID" : "2",
"Name" : "PQR",
"Roll" : 456,
"Address" : "REC-678",
"Standard" : "7"
},.
.
.
]payload2 =
[
{
"field" : "Name",
"field-Type": "String",
"operator": "Not-In-List" ,
"condition-Operand-Type": "",
"error-Message": "Name - Invalid Value: Name not found in the list."
},
{
"field" : "Name",
"field-Type": "String",
"operator": "invalid" ,
"condition-Operand-Type": "",
"error-Message": "Name - Invalid Value: There cannot be special char in a name."
},
{
"field" : "Roll",
"field-Type": "String",
"operator": "Not-In-List" ,
"condition-Operand-Type": "",
"error-Message": "Roll - Invalid Value: Roll not found in the list."
},
{
"field" : "Address",
"field-Type": "String",
"operator": "Not-In-List" ,
"condition-Operand-Type": "",
"error-Message": "Address - Invalid Value: Address not found in the list."
},
.
.
.
]所需输出:
[
{
"ID" : "1",
"Name" : "ABC",
"data-Quality-Rule" :
{
"field" : "ABC",
"field-Type": "String",
"operator": "Not-In-List" ,
"condition-Operand-Type": "",
"error-Message": "Name - Invalid Value: Name not found in the list."
}
},
{
"ID" : "1",
"Name" : "ABC",
"data-Quality-Rule" :
{
"field" : "ABC",
"field-Type": "String",
"operator": "invalid" ,
"condition-Operand-Type": "",
"error-Message": "Name - Invalid Value: There cannot be special char in a name."
}
},
{
"ID" : "1",
"Roll" : "123",
"data-Quality-Rule" :
{
"field" : "123",
"field-Type": "String",
"operator": "Not-In-List" ,
"condition-Operand-Type": "",
"error-Message": "Roll - Invalid Value: Roll not found in the list."
}
},
.
.
.
]发布于 2021-10-21 17:57:22
首先,使用这些键过滤应用于有效负载上的每个元素的规则列表。我使用了namesOf(),但也可以使用pluck()来完成。然后使用列表中每一项上的规则列表来映射输出,而不是尝试在原始有效负载上执行此操作。并使用flatMap()而不是map()来移除每个步骤中不方便的列表嵌套。
脚本:
%dw 2.0
output application/json
fun applyRules(p, r)=
namesOf(p) flatMap ((keyName, order) -> rules filter ($.field == keyName as String))
---
payload flatMap ((item, order) ->
applyRules(item, vars.rules)
map {
ID: item.ID,
Name: item.Name,
"data-quality-rules": $
}
) 有效负载:
[
{
"ID" : "1",
"Name" : "ABC",
"Roll" : 123,
"Address" : "PQR-234",
"Standard" : "5"
},
{
"ID" : "2",
"Name" : "PQR",
"Roll" : 456,
"Address" : "REC-678",
"Standard" : "7"
}
]变量vars.rules:
[
{
"field" : "Name",
"field-Type": "String",
"operator": "Not-In-List" ,
"condition-Operand-Type": "",
"error-Message": "Name - Invalid Value: Name not found in the list."
},
{
"field" : "Name",
"field-Type": "String",
"operator": "invalid" ,
"condition-Operand-Type": "",
"error-Message": "Name - Invalid Value: There cannot be special char in a name."
},
{
"field" : "Roll",
"field-Type": "String",
"operator": "Not-In-List" ,
"condition-Operand-Type": "",
"error-Message": "Roll - Invalid Value: Roll not found in the list."
},
{
"field" : "Address",
"field-Type": "String",
"operator": "Not-In-List" ,
"condition-Operand-Type": "",
"error-Message": "Address - Invalid Value: Address not found in the list."
}
]输出:
[
{
"ID": "1",
"Name": "ABC",
"data-quality-rules": {
"field": "Name",
"field-Type": "String",
"operator": "Not-In-List",
"condition-Operand-Type": "",
"error-Message": "Name - Invalid Value: Name not found in the list."
}
},
{
"ID": "1",
"Name": "ABC",
"data-quality-rules": {
"field": "Name",
"field-Type": "String",
"operator": "invalid",
"condition-Operand-Type": "",
"error-Message": "Name - Invalid Value: There cannot be special char in a name."
}
},
{
"ID": "1",
"Name": "ABC",
"data-quality-rules": {
"field": "Roll",
"field-Type": "String",
"operator": "Not-In-List",
"condition-Operand-Type": "",
"error-Message": "Roll - Invalid Value: Roll not found in the list."
}
},
{
"ID": "1",
"Name": "ABC",
"data-quality-rules": {
"field": "Address",
"field-Type": "String",
"operator": "Not-In-List",
"condition-Operand-Type": "",
"error-Message": "Address - Invalid Value: Address not found in the list."
}
},
{
"ID": "2",
"Name": "PQR",
"data-quality-rules": {
"field": "Name",
"field-Type": "String",
"operator": "Not-In-List",
"condition-Operand-Type": "",
"error-Message": "Name - Invalid Value: Name not found in the list."
}
},
{
"ID": "2",
"Name": "PQR",
"data-quality-rules": {
"field": "Name",
"field-Type": "String",
"operator": "invalid",
"condition-Operand-Type": "",
"error-Message": "Name - Invalid Value: There cannot be special char in a name."
}
},
{
"ID": "2",
"Name": "PQR",
"data-quality-rules": {
"field": "Roll",
"field-Type": "String",
"operator": "Not-In-List",
"condition-Operand-Type": "",
"error-Message": "Roll - Invalid Value: Roll not found in the list."
}
},
{
"ID": "2",
"Name": "PQR",
"data-quality-rules": {
"field": "Address",
"field-Type": "String",
"operator": "Not-In-List",
"condition-Operand-Type": "",
"error-Message": "Address - Invalid Value: Address not found in the list."
}
}
]https://stackoverflow.com/questions/69659596
复制相似问题