Search code examples
jolt

How to check if an item of list exists in another list in jolt


In the given input request, check if the request.PartyAlternateId.item.AltIdType.idTyp is equal to existingMDMResponse.PartyAlternateId.item.AltIdType.idTyp, if so then add existingMDMResponse.PartyAlternateId.item.rowidObject as PartyAlternateId.item.key.rowid to the same item in the output. Repeat this matching to all items in the request.PartyAlternateId.item. And also add the existingMDMResponse.PartyAlternateId.item.rowidObject to the PartyAlternateId.$original.item.rowid in the output. request.id will go as PartyAlternateId.item.key.sourcekey for all the matched items.

Input

{
  "request": {
    "id":"1001",
    "PartyAlternateId": {
      "item": [
        {
          "AltIdType": {
            "idTyp": "0002",
            "idTypDesc": "UCM ID"
          },
          "AltIdVal": "B99857DB-5B3C-469a-A976-F504AA74660A"
        },
        {
          "AltIdType": {
            "idTyp": "0001",
            "idTypDesc": "Account Number OU_NUM"
          },
          "AltIdVal": "104267367"
        }
      ]
    }
  },
  "existingMDMResponse": {
    "MaskedFlg": false,
    "PartyAlternateId": {
      "link": [],
      "firstRecord": 1,
      "pageSize": 10,
      "searchToken": "multi",
      "item": [
        {
          "rowidObject": "1801667       ",
          "label": "Party Alternate Id",
          "AltIdVal": "B6C6C095-7924-490a-BFF8-325F43921F3A",
          "AltIdType": {
            "rowidObject": "20006         ",
            "label": "Lookup Alternate Id Type",
            "idTyp": "0006",
            "idTypDesc": "Ultimate Sales Account"
          }
        },
        {
          "rowidObject": "1801668       ",
          "label": "Party Alternate Id",
          "AltIdVal": "46C01DCD-64E3-47c5-98B3-E1E0F029EDD9",
          "AltIdType": {
            "rowidObject": "20007         ",
            "label": "Lookup Alternate Id Type",
            "idTyp": "0007",
            "idTypDesc": "Sales Account UCM ID"
          }
        },
        {
          "rowidObject": "1801669       ",
          "label": "Party Alternate Id",
          "AltIdVal": "91109",
          "AltIdType": {
            "rowidObject": "20008         ",
            "label": "Lookup Alternate Id Type",
            "idTyp": "0008",
            "idTypDesc": "Portal Organization ID"
          }
        },
        {
          "rowidObject": "12880622",
          "label": "Party Alternate Id",
          "AltIdVal": "5D4F54B8-5C26-4c61-A73D-6324AEC3951A",
          "EffStrtDt": "2022-02-04",
          "AltIdType": {
            "rowidObject": "20002         ",
            "label": "Lookup Alternate Id Type",
            "idTyp": "0002",
            "idTypDesc": "UCM ID"
          }
        },
        {
          "rowidObject": "12880628",
          "label": "Party Alternate Id",
          "AltIdVal": "158126",
          "AltIdType": {
            "rowidObject": "20001         ",
            "label": "Lookup Alternate Id Type",
            "idTyp": "0001",
            "idTypDesc": "Account Number"
          }
        },
        {
          "rowidObject": "87164         ",
          "label": "Party Alternate Id",
          "AltIdVal": "4683",
          "AltIdType": {
            "rowidObject": "20008         ",
            "label": "Lookup Alternate Id Type",
            "idTyp": "0008",
            "idTypDesc": "Portal Organization ID"
          }
        }
      ]
    }
  }
}

Expected Output

{
  "PartyAlternateId": {
    "item": [
      {
        "key": {
          "sourcekey":"1001",
          "rowid": "12880628"
        },
        "AltIdType": {
          "idTyp": "0001",
          "idTypDesc": "Account Number OU_NUM"
        },
        "AltIdVal": "104267367"
      },
      {
        "key": {
          "sourcekey":"1001",
          "rowid": "12880622"
        },
        "AltIdType": {
          "idTyp": "0002",
          "idTypDesc": "UCM ID"
        },
        "AltIdVal": "B99857DB-5B3C-469a-A976-F504AA74660A"
        
       }
     ],
    "$original": {
      "item": [
        {
          "rowid": "12880628"
            },
        {
          "rowid": "12880622"
             }
       ]
    }
  }
}

Solution

  • Try the following spec:

       [
    
      {
        //group items from PartyAlternateId & existingMDMResponse
        // under each idType value object where if there is match the same
        // idType value object will host both info and hence we can create
        // the required structure
        "operation": "shift",
        "spec": {
          "request": {
            "PartyAlternateId": {
              "item": {
                "*": {
                  "AltIdType": {
                    "*": "PartyAlternateId.item.@(1,idTyp).&",
                    "@(4,id)": "PartyAlternateId.item.@(1,idTyp).key.sourcekey"
                  }
                }
              }
            }
          },
          "existingMDMResponse": {
            "PartyAlternateId": {
              "item": {
                "*": {
                  "AltIdType": {
                    "@(1,rowidObject)": "PartyAlternateId.item.@(1,idTyp).key.rowid"
                  }
                }
              }
            }
          }
        }
      },
      {
        //transpose match items to new array
        "operation": "shift",
        "spec": {
          "PartyAlternateId": {
            "item": {
              "*": {
                "key": {
                  //if sourcekey found that has rowid then move to the new array
                  "sourcekey": {
                    "@(1,rowid)": {
                      "@(3)": "PartyAlternateId.item[]",
                      //move matched items rowid to the original array 
                      "@": "PartyAlternateId.\\$original.item[].rowid"
                    }
                  }
                }
              }
            }
          }
        }
      }
     ]