veejay
veejay

Reputation: 115

How to write a function filter an object from incoming array using dataweave?

Given an array shown below

array:

{
        "documentToSigner": [
          {
            "documentName": "ESRA For aa aa",
            "typeCd": "SIGNATURE",
            "subtypeCd": "CAPTURE",
            "eSignLiveExtension": {
              "extractInd": true
            },
            "documentItemName": "Signature001"
          },
          {
            "documentName": "ODP Agreement",
            "typeCd": "SIGNATURE",
            "subtypeCd": "CAPTURE",
            "eSignLiveExtension": {
              "extractInd": true
            },
            "documentItemName": "Signature002"
          },
          {
            "documentName": "ODP Agreement",
            "typeCd": "INPUT",
            "subtypeCd": "LABEL",
            "eSignLiveExtension": {
              "extractInd": true,
              "autoFieldBindingCd": "{approval.signed}"
            },
            "documentItemName": "SignatureDate002"
          }
        ]
      }

I have an JSON variable as below I want to filter and extract the noOfDocumentItemToSigner if the key and content version from Array matches with the JSON.

Example: For: aAuDQ0000004mX60AI contentVersion: 068DQ000000lK0iYAE

Expected output:

{ "documentName": "ESRA For cc cc", "documentItemToSigner": [ { "typeCd": "SIGNATURE", "subtypeCd": "CAPTURE", "eSignLiveExtension": { "extractInd": true }, "documentItemName": "Signature001" } ] }, { "documentName": "ODP Agreement", "documentItemToSigner": [ { "typeCd": "SIGNATURE", "subtypeCd": "CAPTURE", "eSignLiveExtension": { "extractInd": true }, "documentItemName": "Signature004" }, { "typeCd": "INPUT", "subtypeCd": "LABEL", "eSignLiveExtension": { "extractInd": true, "autoFieldBindingCd": "{approval.signed}" }, "documentItemName": "SignatureDate004" } ] }

Upvotes: 0

Views: 88

Answers (4)

veejay
veejay

Reputation: 115

 %dw 2.0
    output application/json
    var input = {
  "individualToEvent": [
    {
      "documentToSigner": [
        {
          "documentName": "ESRA For aa aa",
          "typeCd": "SIGNATURE",
          "subtypeCd": "CAPTURE",
          "eSignLiveExtension": {
            "extractInd": true
          },
          "documentItemName": "Signature001"
        },
        {
          "documentName": "ODP Agreement",
          "typeCd": "SIGNATURE",
          "subtypeCd": "CAPTURE",
          "eSignLiveExtension": {
            "extractInd": true
          },
          "documentItemName": "Signature002"
        },
        {
          "documentName": "ODP Agreement",
          "typeCd": "INPUT",
          "subtypeCd": "LABEL",
          "eSignLiveExtension": {
            "extractInd": true,
            "autoFieldBindingCd": "{approval.signed}"
          },
          "documentItemName": "SignatureDate002"
        }
      ]
    }
  ]
}

var output =  "documentToSigner": [
          {
            "documentName": "ESRA For cc cc",
            "documentItemToSigner": [
              {
                "typeCd": "SIGNATURE",
                "subtypeCd": "CAPTURE",
                "eSignLiveExtension": {
                  "extractInd": true
                },
                "documentItemName": "Signature001"
              }
            ]
          },
          {
            "documentName": "ODP Agreement",
            "documentItemToSigner": [
              {
                "typeCd": "SIGNATURE",
                "subtypeCd": "CAPTURE",
                "eSignLiveExtension": {
                  "extractInd": true
                },
                "documentItemName": "Signature004"
              },
              {
                "typeCd": "INPUT",
                "subtypeCd": "LABEL",
                "eSignLiveExtension": {
                  "extractInd": true,
                  "autoFieldBindingCd": "{approval.signed}"
                },
                "documentItemName": "SignatureDate004"
              }
            ]
          }
        ]
---
input.documentToSigner 
  // Group items by documentName
  groupBy ((item) -> item.documentName) 
  // Transform each group to have documentName and documentItemToSigner
  mapObject ((documentName, items) -> {
    documentName: documentName,
    documentItemToSigner: items map ((item) -> 
      item - 'documentName' // Remove documentName from each item
    )
  })

Upvotes: 0

Dane
Dane

Reputation: 1

if I understood your question correctly (in regards what is in the payload and what is in the input array) I was able to write this mapping, to be able to find matches between those two data sets:

%dw 2.0
output application/json

var myPayload = {
  "aAuDQ0000004mX60AI": [
    {
      "noOfDocumentItemToSigner": ["eSign_documentItemName1__c"],
      "contentVersion": "068DQ000000lK0iYAE",
      "documentName": "ESRA with Exhibit_A"
    },
    {
      "noOfDocumentItemToSigner": ["eSign_documentItemName1__c"],
      "contentVersion": "068DQ000000lK0OYAU",
      "documentName": "ODP Agreement"
    }
  ],
  "aAuDQ0000004mXG0AY": [
    {
      "noOfDocumentItemToSigner": ["eSign_documentItemName1__c"],
      "contentVersion": "068DQ000000lK0JYAU",
      "documentName": "ESRA For bb bb"
    },
    {
      "noOfDocumentItemToSigner": ["eSign_documentItemName1__c", "eSign_documentItemName2__c"],
      "contentVersion": "068DQ000000lK0OYAU",
      "documentName": "ODP Agreement"
    }
  ]
}

var myInputArray = [
    {
        "noOfDocumentItemToSigner": [
        "eSign_documentItemName1__c",
            "eSign_documentItemName2__c"
        ],
        "contentVersion": "068DQ000000lK0iYAE",
        "documentName": "ESRA with Exhibit_A"
    },
    {
        "noOfDocumentItemToSigner": [
        "eSign_documentItemName1__c"
        ],
        "contentVersion": "068DQ000000lK0OYAU",
        "documentName": "ODP Agreement"
    }
]

---
myPayload mapObject ((value, key, index) -> {
    (key): (value) map ((item, index) -> {
        noOfDocumentItemToSigner: item.noOfDocumentItemToSigner,
        contentVersion: item.contentVersion,
        documentName: item.documentName
    }) filter $.contentVersion == (myInputArray map ((item2, index2) -> item2.contentVersion))[index]
})

Upvotes: 0

cipherwizard
cipherwizard

Reputation: 21

If I understand your problem statement correctly, you just need to extract noOfDocumentItemToSigner based on contentVersion which is coming in array.

You can use selector functions in combination with map to extract the values.

%dw 2.0
output application/json
var listOfContentVersions=["068DQ000000lK0iYAE","068DQ000000lK0OYAU"]
---
listOfContentVersions map(v,i)-> {(v):payload[?($.contentVersion==v)][0].noOfDocumentItemToSigner map { 
    // mapping here
}}

Upvotes: 0

Harshank Bansal
Harshank Bansal

Reputation: 3262

You can get an array of all the available content versions either using "Descendants" selector like below

var requiredVersions = vars.inputJson..contentVersion

It is an easy way but I have experienced performance impact with the above method. If your input is large and the performance impact is significant, you can use a combination of valuesOf to get the array of all versions

var requiredVersions = flatten(valuesOf(vars.inputJson))

After this you can filter the required versions from payload

%dw 2.0
output application/json
var requiredVersions = flatten(valuesOf(vars.inputJson)).contentVersion
---
payload filter ((item, index) -> 
    requiredVersions contains item.contentVersion) 
map ((item, index) -> {
    // do the mapping here
})

Upvotes: 0

Related Questions