与 Dataweave 相关的问题

Issue related to Dataweave

我需要根据类似的 'key' 值将来自 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",
    "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
  }]

var responseLicensing = [{
    "key": "SUBT00009925-1",
    "expiryDate": "2021-01-16"
  },
  {
    "key": "SUBT00009925-2",
    "expiryDate": "2021-01-16",
    "cloudPublicName": "dodp-testcloud",
    "cloudAccountId": "a-t-1000-5001-0687-0024"
  }]
---
{
    responseSfdc map (sfdc,i) -> {
        
    }
}

我需要的输出是这样的-

[{
    "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,
    "expiryDate": "2021-01-16"
  },
  {
    "key": "SUBT00009925-2",
    "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,
    "expiryDate": "2021-01-16",
    "cloudPublicName": "dodp-testcloud",
    "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",
    "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
  }\]

var responseLicensing = \[{
    "key": "SUBT00009925-1",
    "expiryDate": "2021-01-16"
  },
  {
    "key": "SUBT00009925-2",
    "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",
    "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",
    "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
  }]

var responseLicensing = [{
    "key": "SUBT00009925-1",
    "expiryDate": "2021-01-16"
  },
  {
    "key": "SUBT00009925-2",
    "expiryDate": "2021-01-16",
    "cloudPublicName": "dodp-testcloud",
    "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
    })
  }

又拍了一张。

responseSfdc map ((item, index) -> 
    item ++ (responseLicensing[?($.key == item.key)][0] - "key")
)

如果您发现需要从输出中删除更多重复键,您可以在 - key 之后添加它们。或者,可以使用 DW writer property for JSON: [duplicateKeyAsArray] 来输出值数组而不是重复键。

另一种使用函数提高可读性并使用默认值进行安全检查的方法

%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])