Search code examples
jsonjolt

Adding elements to the array based on condition


I have 2 ids ultimateId and Id. If both id's are there in input then I need to add ultimateId related object to the output list. If i have only id but not ultimateId then I have to add id related object to the output list.

INPUT:

{
  "accountType": "CUSTOMER",
  "AccountInfo": {
    "ultimateId": "DF6ECC32",
    "id": "DJKWNA"
  },
  "AccountAddresses": [
    {
      "modId": "Al-Jazeera",
      "businessAddressEndDateTime": "2020-07-01T00:00:00.00Z"
    }
  ],
  "PartyDetails": {
    "domesticUltimateDunsPostalCode": "94115",
    "subIndustryCode": "1232"
  }
}

SPECS:

[
  {
    "operation": "shift",
    "spec": {
      "AccountInfo": {
        "id": {
          "@2,partyAltIdResellerUidSrcKey": "PartyAlternateId.item[#2].key.sourceKey",
          "@1,resellerUId": "PartyAlternateId.item[#2].AltIdVal",
          "@2,created": "PartyAlternateId.item[#2].EffStrtDt",
          "#ID": "PartyAlternateId.item[#2].AltIdType.idTypDesc",
          "#0004": "PartyAlternateId.item[#2].AltIdType.idTyp"
        },
        "ultimateId": {
          "@2,partyAltIdUltimateResellerUidSrcKey": "PartyAlternateId.item[#2].key.sourceKey",
          "@1,resellerUltimateSalesLegacySourceId": "PartyAlternateId.item[#2].AltIdVal",
          "@2,created": "PartyAlternateId.item[#2].EffStrtDt",
          "#Ultimate ID": "PartyAlternateId.item[#2].AltIdType.idTypDesc",
          "#0018": "PartyAlternateId.item[#2].AltIdType.idTyp"
        }
      }
    }
  }
]

output I am getting for above specs:

{
  "PartyAlternateId": {
    "item": [
      {
        "AltIdType": {
          "idTypDesc": "ID",
          "idTyp": "0004"
        }
      },
      {
        "AltIdType": {
          "idTypDesc": "Ultimate ID",
          "idTyp": "0018"
        }
      }
    ]
  }
}

Desired Output if input have both id and ultimateId:

{
  "PartyAlternateId": {
    "item": [
      {
        "AltIdType": {
          "idTypDesc": "Ultimate ID",
          "idTyp": "0018"
        }
      }
    ]
  }
}

Desired Output if input have only id but not ultimateId:

{
  "PartyAlternateId": {
    "item": [
      {
        "AltIdType": {
          "idTypDesc": "ID",
          "idTyp": "0004"
        }
      }
    ]
  }
}

Solution

  • You can add a modify transformation spec along with a lastElement function such as

    [
      {
        "operation": "shift",
        "spec": {
          "AccountInfo": {
            "id": {
              "@2,partyAltIdResellerUidSrcKey": "PartyAlternateId.item[#2].key.sourceKey",
              "@1,resellerUId": "PartyAlternateId.item[#2].AltIdVal",
              "@2,created": "PartyAlternateId.item[#2].EffStrtDt",
              "#ID": "PartyAlternateId.item[#2].AltIdType.idTypDesc",
              "#0004": "PartyAlternateId.item[#2].AltIdType.idTyp"
            },
            "ultimateId": {
              "@2,partyAltIdUltimateResellerUidSrcKey": "PartyAlternateId.item[#2].key.sourceKey",
              "@1,resellerUltimateSalesLegacySourceId": "PartyAlternateId.item[#2].AltIdVal",
              "@2,created": "PartyAlternateId.item[#2].EffStrtDt",
              "#Ultimate ID": "PartyAlternateId.item[#2].AltIdType.idTypDesc",
              "#0018": "PartyAlternateId.item[#2].AltIdType.idTyp"
            }
          }
        }
      },
      {//pick the last component of the "item" array  
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": "=lastElement(@(1,&))"
          }
        }
      },
      {//nest item object with array wrapper and convert it back to an array
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "item": "=toList"
          }
        }
      }
    ]