user12445682
user12445682

Reputation: 25

Issue related to Dataweave

I need to append the data from responseLicensing (like expiryDate, cloudAccountName, cloudAccountId) to the responseSfdc data depending on the similar 'key' value.

Perform a lookup in the responseLicensing object using the key in an instance of responseSfdc and append some key-value pairs picked as a result of a successful lookup, to responseSfdc object.

%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) -> {
        
    }
}

Output I need is something like this -

[{
    "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"
  }]

Upvotes: 0

Views: 94

Answers (4)

Ray A
Ray A

Reputation: 447

Another approach using function for readability and using default for safety check

%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" is to remove the extra field from responseLicensing variable before appending it to the result. "default {}" preventing the Null error. If you don't have match, the - key will fail and you will get an exception like:

You called the function '-' with these arguments: 
  1: Null (null)
  2: String ("key")

Another Option:

Using mergeWidth while checking the match, you don't need to use - to remove duplicate element.

Example:

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

Upvotes: 1

short stack stevens
short stack stevens

Reputation: 692

Another take.

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

If you find you have more duplicate keys you need removed from the output you can add them after - key. Alternately there is a DW writer property for JSON: [duplicateKeyAsArray] that you can use to output an array of values instead of duplicate keys.

Upvotes: 0

Salim Khan
Salim Khan

Reputation: 4303

Another approach

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

Upvotes: 1

aled
aled

Reputation: 25699

Seems like a work for the leftJoin() function.

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

Upvotes: 2

Related Questions