我有两个数组,一个是输入有效载荷,另一个是来自目标api的输出有效载荷,现在使用这两个数组,我试图为最终用户设定最终响应,在最终用户中,我需要将输入有效负载数组中的id与输出有效负载数组中的id字段匹配,然后显示每个数组的状态。
输入有效载荷
var actualPayload = [
{
"id": "b3_12XY335",
"fName": "test-335",
"lName": "t335",
"email": "test-335@gmail.com"
},
{
"id": "b3_12XY346",
"fName": "test-346",
"lName": "346",
"email": "test-346@gmail.com"
},
{
"id": "b3_12XY347",
"fName": "test-347",
"lName": "347",
"email": "test-347@gmail.com"
},
{
"id": "b3_12XY120",
"fName": "test-120",
"lName": "120",
"email": "test-120@gmail.com"
}
]目标系统的有效载荷
var payload = {
"status": "created",
"error": "error",
"rows": [{
"id": "b3_12XY120",
"status": "created"
}]
}下面是我的数据编织代码
%dw 2.0
import * from dw::core::Strings
output application/json skipNullOn="everywhere"
---
(actualPayload map (item, index) -> {
(payload.rows map(item1, index1) ->{
"Id": substringBefore(item.id, "_"),
"studentId": item.id,
"status" : if((item.id == item1.id) and (item1.status == "created" or item1.status == "updated") ) true else false,
("error" : payload.error) if((item1.id != item.id))
})
})我在上面的数据编织代码中遇到的问题是,如果所有对象都导致错误,那么只有错误信息才会从目标api返回,但是在这种情况下,上面的数据编织不会返回响应。
因此,当payload.rows不是来自目标系统时,我希望上面的数据编织代码给我结果,然后我想用实际有效载荷中所有in的错误元素中的错误信息来框架输出响应。
当目标响应中没有行时,我希望从实际有效负载中针对每个对象的数据编织返回以下输出,并从目标响应中的error字段映射错误,对于从目标api返回的任何行,我希望上面提到的输出来自数据编织。
[
{
"Id": "b3",
"studentId": "b3_12XY335",
"status": false,
"error": "error"
},
{
"Id": "b3",
"studentId": "b3_12XY346",
"status": false,
"error": "error"
},
{
"Id": "b3",
"studentId": "b3_12XY347",
"status": false,
"error": "error"
},
{
"Id": "b3",
"studentId": "b3_12XY120",
"status": false,
"error": "error"
}
]发布于 2022-09-08 14:03:48
提供的代码的问题是,只有当有一个rows对象时,映射才会发生。而不是这样,您可以使用id对行进行查找。可以通过使用groupBy按Id对有效负载中的行进行分组来创建查找对象。这样,实际有效负载中的每个项都不会迭代行。
%dw 2.0
import * from dw::core::Strings
output application/json
var actualPayload = [
{
"id": "b3_12XY335",
"fName": "test-335",
"lName": "t335",
"email": "test-335@gmail.com"
},
{
"id": "b3_12XY346",
"fName": "test-346",
"lName": "346",
"email": "test-346@gmail.com"
},
{
"id": "b3_12XY347",
"fName": "test-347",
"lName": "347",
"email": "test-347@gmail.com"
},
{
"id": "b3_12XY120",
"fName": "test-120",
"lName": "120",
"email": "test-120@gmail.com"
}
]
var groupedRowsById = payload.rows default [] groupBy $.id
var statusesToMatch = ["created", "updated"]
---
(actualPayload map (item, index) -> do {
var matchedRow = groupedRowsById [item.id]
---
{
Id: substringBefore(item.id, "_"),
studentId: item.id,
status: ((! isEmpty(matchedRow)) and (statusesToMatch contains (matchedRow[0].status))),
(error: payload.error) if (isEmpty(matchedRow))
}
})输出:
[
{
"Id": "b3",
"studentId": "b3_12XY335",
"status": false,
"error": "error"
},
{
"Id": "b3",
"studentId": "b3_12XY346",
"status": false,
"error": "error"
},
{
"Id": "b3",
"studentId": "b3_12XY347",
"status": false,
"error": "error"
},
{
"Id": "b3",
"studentId": "b3_12XY120",
"status": true
}
]发布于 2022-09-08 13:52:42
我不熟悉给定代码中的语言,但是我可以指出代码中的基本缺陷,我可以提供一个伪代码来解决您的问题。
1:(actualPayload map (item, index) -> {
2:(payload.rows map(item1, index1) ->{
3: "Id": substringBefore(item.id, "_"),
4: "studentId": item.id,
5: "status" : if((item.id == item1.id) and (item1.status == "created" or item1.status == "updated") ) true else false, ("error" : payload.error) if((item1.id != item.id))
6:})
7:})好的,所以代码中的基本缺陷是您不必要地在line#2上遍历目标有效负载,相反,您应该只在目标有效负载中找到匹配的对象。伪代码应该是这样的。
1:(actualPayload map (item, index) -> {
2: targetObj= payload.find(x=> x.id=item.id)
3: if (targetObj){//put your logic here if target object is found}
4: else {// put your logic here if target object is not found}
6:}) 发布于 2022-09-09 06:00:28
您可以使用leftJoin连接两个数组,即有效负载和来自目标系统的响应。它将在将有效负载的每个元素与响应中的相应元素匹配之后返回一个数组。
%dw 2.0
import leftJoin from dw::core::Arrays
import substringBefore from dw::core::Strings
output application/json
---
leftJoin(payload, vars.systemResponse.rows, (student) -> student.id, (result) -> result.id)
map {
Id: $.l.id substringBefore "_",
studentId: $.l.id,
status: !isEmpty($.r), // if r is empty that means there is no error. so status will be true
(error: vars.systemResponse.error) if(isEmpty($.r))
}https://stackoverflow.com/questions/73649804
复制相似问题