问题描述
我需要根据类似的“键”值,将来自responseLicensing的数据(例如expiryDate,cloudAccountName,cloudAccountId)附加到responseSfdc数据中。
使用responseSfdc实例中的键在responseLicensing对象中执行查找,并将成功查找后选择的一些键值对附加到responseSfdc对象。
%dw 2.0
output application/json
var responseSfdc = [{
"key": "SUBT00009925-1","contractEndDate": null,"product": {
"productName": "ArtPro+ Subscription","productCode": "ArtPro+S","downloadURL": null,"upgradeProduct": null
},"projectReference": null,"orderNumber": "O-0000001105","orderCreationDate": "2020-07-14T07:48:04.000Z","subscriptionName": null,"autorenewal": null
},{
"key": "SUBT00009925-2","autorenewal": null
}]
var responseLicensing = [{
"key": "SUBT00009925-1","expiryDate": "2021-01-16"
},"expiryDate": "2021-01-16","cloudPublicName": "dodp-testcloud","cloudAccountId": "a-t-1000-5001-0687-0024"
}]
---
{
responseSfdc map (sfdc,i) -> {
}
}
我需要的输出是这样的-
[{
"key": "SUBT00009925-1","autorenewal": null,"cloudAccountId": "a-t-1000-5001-0687-0024"
}]
解决方法
似乎是leftJoin()函数的作品。
[%dw 2.0
output application/json
import * from dw::core::Arrays
var responseSfdc = \[{
"key": "SUBT00009925-1","contractEndDate": null,"product": {
"productName": "ArtPro+ Subscription","productCode": "ArtPro+S","downloadURL": null,"upgradeProduct": null
},"projectReference": null,"orderNumber": "O-0000001105","orderCreationDate": "2020-07-14T07:48:04.000Z","subscriptionName": null,"autoRenewal": null
},{
"key": "SUBT00009925-2","autoRenewal": null
}\]
var responseLicensing = \[{
"key": "SUBT00009925-1","expiryDate": "2021-01-16"
},"expiryDate": "2021-01-16","cloudPublicName": "dodp-testcloud","cloudAccountId": "a-t-1000-5001-0687-0024"
}\]
---
leftJoin( responseSfdc,responseLicensing,(sfdc) -> sfdc.key,(license) -> license.key) map ($.l ++ ($.r - "key"))][1]
,
另一种方法
%dw 2.0
output application/json
var responseSfdc = [{
"key": "SUBT00009925-1","autoRenewal": null
}]
var responseLicensing = [{
"key": "SUBT00009925-1","cloudAccountId": "a-t-1000-5001-0687-0024"
}]
---
responseSfdc map () -> using (id = $.key)
{
a:$
}.a
++
{
(responseLicensing filter ($.key == id) map (responseLicensingValue) -> {
cloudAccountId : responseLicensingValue.cloudAccountId,cloudPublicName: responseLicensingValue.cloudPublicName
})
}
,
另一种使用函数来提高可读性并使用默认值进行安全检查的方法
%dw 2.0
output application/json
var responseSfdc = [] // Assuming you have data for responseSfdc in array
var responseLicensing = [] // Assuming you have data for responseLicensing in an array
fun getLicenseInfo(key) = responseLicensing[?($.key == key)][0]
---
responseSfdc map (item,index) -> (item) ++
((getLicenseInfo(item.key) default {}) - "key")
“ - key
”用于在将responseLicensing变量附加到结果之前,将其删除。
“ default {}
”防止Null错误。如果没有匹配项,则- key
将会失败,并且会出现类似以下的异常:
You called the function '-' with these arguments:
1: Null (null)
2: String ("key")
另一个选项:
在检查匹配项时使用 mergeWidth ,您无需使用 -
即可删除重复的元素。
示例:
%dw 2.0
import mergeWith from dw::core::Objects
var responseSfdc = []
var responseLicensing = []
output application/json
---
responseSfdc map ((sfdc) -> sfdc mergeWith
responseLicensing[?($.key == sfdc.key)][0])
,
另一种方式。
responseSfdc map ((item,index) ->
item ++ (responseLicensing[?($.key == item.key)][0] - "key")
)
如果发现有更多重复的密钥需要从输出中删除,则可以在 - key
之后添加它们。另外,还有一个DW writer property for JSON:[duplicateKeyAsArray]
,可用于输出值数组,而不是重复的键。