Aggregate two json payloads into one based on filt

2019-08-20 15:46发布

问题:

i am trying to aggregate two json payloads into one. below json is stored in payload,

{
   "clients":[
      {
         "status":"IN",
         "clientSoftDeleteIndicator":"N",
         "roles":[
            {
               "clientRoleCodeDesc":"PRIMARY OWNER",
               "clientRoleCode":"PO",
               "roleEffDate":null,
               "clientRoleStatusCode":"IN",
               "clientRoleStatusCodeDesc":"INACTIVE",
               "roleSoftDeleteIndicator":"N",
               "contractID":"49006649"
            },
            {
               "clientRoleCodeDesc":"PRIMARY INSURED",
               "clientRoleCode":"PI",
               "roleEffDate":"2014-11-05",
               "clientRoleStatusCode":"IN",
               "clientRoleStatusCodeDesc":"INACTIVE",
               "roleSoftDeleteIndicator":"N",
               "contractID":"49006649"
            },
             {
               "clientRoleCodeDesc":"PRIMARY INSURED",
               "clientRoleCode":"PI",
               "roleEffDate":"2014-11-05",
               "clientRoleStatusCode":"IN",
               "clientRoleStatusCodeDesc":"INACTIVE",
               "roleSoftDeleteIndicator":"N",
               "contractID":"49006648"
            }
         ],
         "clientID":72341935,
         "statusDate":"2014-11-05",
         "party":{
            "tin4":"0346",
            "addresses":[
               {
                  "addrSoftDeleteIndicator":"N",
                  "city":"HOLLYWOOD",
                  "addressType":null,
                  "zip":"104620000",
                  "country":"U",
                  "line3":null,
                  "line2":null,
                  "line1":"5 MODEL OFFICE",
                  "county":null,
                  "state":"CA",
                  "validIndicator":null,
                  "preferredIndicator":null
               }
            ],
            "firstName":"SALLONARO",
            "middleName":null,
            "lastName":"PARETO",
            "personSoftDeleteIndicator":"N",
            "birthDate":"1987-11-10",
            "sufName":null,
            "preName":"MR",
            "tin":"103380346",
            "taxIdSoftDeleteIndicator":"N",
            "gender":"M",
            "phones":[

            ],
            "martialStatus":null,
            "emails":[

            ],
            "tinType":"U"
         },
         "statusDesc":"INACTIVE"
      }
   ]
}

and below json is stored in flowVariable contractVariable

{
   "contracts":[
      {
         "policyEffDate":null,
         "productCode":"97",
         "policyStatusDate":"2014-11-05",
         "cntrctSoftDeleteIndicator":"N",
         "contractID":"49006649",
         "adminSysCode":"FCT",
         "dataSourceCode":null,
         "policyStatus":"B3",
         "lobCode":null,
         "contractIssuerCode":null
      },
      {
         "policyEffDate":null,
         "productCode":"98",
         "policyStatusDate":"2015-11-05",
         "cntrctSoftDeleteIndicator":"N",
         "contractID":"49006648",
         "adminSysCode":"FCT",
         "dataSourceCode":null,
         "policyStatus":"B4",
         "lobCode":null,
         "contractIssuerCode":null
      }
   ]
}

i want to copy the attributes from flowVariable, into payload when contractId matches.

below is my code snippet but all i am getting is empty json

%dw 1.0
%output application/json
---
payload.clients.roles map ((role,index) ->
     {
    contracts: ((flowVars.contractVariable.contracts default []) filter (role.contractID == $.contractID)) 
                                            map ((contract ,indexOf)  -> {
        policyEffDate: contract.policyEffDate,
        productCode: contract.productCode,
        policyStatusDate: contract.policyStatusDate,
        cntrctSoftDeleteIndicator: contract.cntrctSoftDeleteIndicator,
        contractID: contract.contractID,
        adminSysCode: contract.adminSysCode,
        dataSourceCode: contract.dataSourceCode,
        policyStatus: contract.policyStatus,
        lobCode: contract.lobCode,
        contractIssuerCode: contract.contractIssuerCode,
        clientRoleCodeDesc: role.clientRoleCodeDesc,
        clientRoleCode: role.clientRoleCode,
        roleEffDate: role.roleEffDate,
        clientRoleStatusCode: role.clientRoleStatusCode,
        clientRoleStatusCodeDesc: role.clientRoleStatusCodeDesc,
        roleSoftDeleteIndicator: role.roleSoftDeleteIndicator,
        contractID: role.contractID
    })
})  

and the output i am getting is

[
  {
    "contracts": [

    ]
  }
]

Can please someone help me on what is going wrong, i tried multiple ways but couldn't make it work.

I also went through Aggregate two payloads in Mule ESB which is similar to my requirement but it didn't work as well.

any help is highly appreciated.