与 DataWeave 中的场景作斗争
Struggling with a scenario in DataWeave
以下是传入的示例负载。它有一个字段“名称”,可以具有任何值,例如 XYZ、PQR、ABC 和特定值“TEST”。
传入的负载:
[
{
"Name": "XYZ",
"Maths": "90",
"English": "85"
},
{
"Name": "ABC",
"Maths" : "91",
"English": "90"
},
{
"Name": "TEST",
"Maths" : "89",
"English": "86"
},
]
下面是来自具有值列表的数据库的响应。
传入的负载名称需要与数据库对象中的“名称”字段进行比较。
来自不同应用程序的数据=
[
{"Name": "ABC",
"Gender": "Female",
"Class": "4"},
{"Name": "PQR",
"Gender": "Male",
"Class": "5"},
{"Name": "XYZ",
"Gender": "Female",
"Class": "3"},
{"Name": "OPQ",
"Gender": "Female",
"Class": "3"},
...
]
如果传入负载中的一个或多个对象中的“名称”是“TEST”,则需要跳过该对象,其余匹配 名称的对象应该是输出;但是,如果任何单个对象中的“名称”与数据库响应中的名称列表不匹配,则需要跳过所有对象,并且空负载应该是输出 [].
在上述情况下,传入有效负载中的名称 - XYZ、ABC 存在于数据库中,因此以下是预期输出(需要跳过 TEST 名称对象):
[
{
"Name": "XYZ",
"Maths": "90",
"English": "85"
},
{
"Name": "ABC",
"Maths" : "91",
"English": "90"
}
]
在下面的场景中,传入的有效载荷在其中一个对象中具有“名称”作为“堆栈”,而“堆栈”不存在于数据库响应对象中,因此空的有效载荷输出:
[
{
"Name": "XYZ",
"Maths": "90",
"English": "85"
},
{
"Name": "ABC",
"Maths" : "91",
"English": "90"
},
{
"Name": "Stack", ----Not present in database response objects
"Maths" : "91",
"English": "90"
}
]
预期输出:
[]
试试这个方法:
输入
[
{
"Name": "XYZ",
"Maths": "90",
"English": "85"
},
{
"Name": "ABC",
"Maths" : "91",
"English": "90"
},
{
"Name": "TEST",
"Maths" : "89",
"English": "86"
}
]
脚本
%dw 2.0
output application/json
var compareWith = [
{"Name": "ABC",
"Gender": "Female",
"Class": "4"},
{"Name": "PQR",
"Gender": "Male",
"Class": "5"},
{"Name": "XYZ",
"Gender": "Female",
"Class": "3"},
{"Name": "OPQ",
"Gender": "Female",
"Class": "3"}
]
var inputNames = payload.Name
---
if (inputNames contains "TEST") payload filter(compareWith.Name contains $.Name) else if(sizeOf(payload filter(compareWith.Name contains $.Name)) != sizeOf(payload)) [] else payload
输出
[
{
"Name": "XYZ",
"Maths": "90",
"English": "85"
},
{
"Name": "ABC",
"Maths": "91",
"English": "90"
}
]
以下是传入的示例负载。它有一个字段“名称”,可以具有任何值,例如 XYZ、PQR、ABC 和特定值“TEST”。
传入的负载:
[
{
"Name": "XYZ",
"Maths": "90",
"English": "85"
},
{
"Name": "ABC",
"Maths" : "91",
"English": "90"
},
{
"Name": "TEST",
"Maths" : "89",
"English": "86"
},
]
下面是来自具有值列表的数据库的响应。 传入的负载名称需要与数据库对象中的“名称”字段进行比较。
来自不同应用程序的数据=
[
{"Name": "ABC",
"Gender": "Female",
"Class": "4"},
{"Name": "PQR",
"Gender": "Male",
"Class": "5"},
{"Name": "XYZ",
"Gender": "Female",
"Class": "3"},
{"Name": "OPQ",
"Gender": "Female",
"Class": "3"},
...
]
如果传入负载中的一个或多个对象中的“名称”是“TEST”,则需要跳过该对象,其余匹配 名称的对象应该是输出;但是,如果任何单个对象中的“名称”与数据库响应中的名称列表不匹配,则需要跳过所有对象,并且空负载应该是输出 [].
在上述情况下,传入有效负载中的名称 - XYZ、ABC 存在于数据库中,因此以下是预期输出(需要跳过 TEST 名称对象):
[
{
"Name": "XYZ",
"Maths": "90",
"English": "85"
},
{
"Name": "ABC",
"Maths" : "91",
"English": "90"
}
]
在下面的场景中,传入的有效载荷在其中一个对象中具有“名称”作为“堆栈”,而“堆栈”不存在于数据库响应对象中,因此空的有效载荷输出:
[
{
"Name": "XYZ",
"Maths": "90",
"English": "85"
},
{
"Name": "ABC",
"Maths" : "91",
"English": "90"
},
{
"Name": "Stack", ----Not present in database response objects
"Maths" : "91",
"English": "90"
}
]
预期输出: []
试试这个方法:
输入
[
{
"Name": "XYZ",
"Maths": "90",
"English": "85"
},
{
"Name": "ABC",
"Maths" : "91",
"English": "90"
},
{
"Name": "TEST",
"Maths" : "89",
"English": "86"
}
]
脚本
%dw 2.0
output application/json
var compareWith = [
{"Name": "ABC",
"Gender": "Female",
"Class": "4"},
{"Name": "PQR",
"Gender": "Male",
"Class": "5"},
{"Name": "XYZ",
"Gender": "Female",
"Class": "3"},
{"Name": "OPQ",
"Gender": "Female",
"Class": "3"}
]
var inputNames = payload.Name
---
if (inputNames contains "TEST") payload filter(compareWith.Name contains $.Name) else if(sizeOf(payload filter(compareWith.Name contains $.Name)) != sizeOf(payload)) [] else payload
输出
[
{
"Name": "XYZ",
"Maths": "90",
"English": "85"
},
{
"Name": "ABC",
"Maths": "91",
"English": "90"
}
]