Search code examples
transformtransformationjolt

Jolt conditional mapping of array of elements


Input.json

{
  "accountType": "ADMIN",
  "id": "aaab-ccc-ddd-eeeAAA",
  "relationship": "DIRECT",
  "accountInfo": {
    "partyTypeCode": "PARTNER",
    "source": "Facebook",
    "internalAccount": true,
    "category": "Father"
  },
  "accountAddress": [
    {
      "addressId": "0016u00000bsqfrAddId",
      "integrationId": "0016u00000bsqfrInt",
      "addressType": "office",
      "shipToAddress": "Y",
      "soldToAddress": "N"
    },
    {
      "addressId": "12316u00000bsqfrAddId",
      "integrationId": "12316u00000bsqfrInt",
      "addressType": "home",
      "shipToAddress": "N",
      "soldToAddress": "Y"
    }
  ]
}

output.json

{
  "PostalAddress": {
    "item": [
      {
        "key": {
          "primaryKey": "0016u00000bsqfrAddId~HDADDRESS~Admin"
        },
        "PrimaryAddress": {
          "addrTyp": "Y",
          "addrDes": "PRIMARY"
        },
        "SecondaryAddress": {
          "addrTyp": "N",
          "addrDes": "OPTIONAL"
        }
      }
    ]
  }
}

Criteria for the above output

  1. When source is "Facebook" primarykey value should be addressId+"~HDADDRESS~"+accountType(should be camelcase)
  2. when source is "Google" primarykey value should be integrationId+accountType(should be camelcase)
  3. Map the shipToAddress of input parameter to PrimaryAddress.addrTyp as it is and hardcode the value of addrDes as "PRIMARY"
  4. Map the soldToAddress of input parameter to SecondaryAddress.addrTyp as it is and hardcode the value of addrDes as "OPTIONAL"

Solution

  • You can use the following spec

        [
            { //transfrom to lowercase
                "operation": "modify-overwrite-beta",
                "spec": {
                "accountType": "=toLower"
                }
            },
            { //split all the characters
                "operation": "modify-overwrite-beta",
                "spec": {
                "accountType": "=split('',@(1,&))"
                }
            },
            { //convert 1st character to uppercase
                "operation": "modify-overwrite-beta",
                "spec": {
                "accountType": {
                    "[0]": "=toUpper"
                }
                }
            },
            { //convert array back to string
                "operation": "modify-overwrite-beta",
                "spec": {
                "accountType": "=join('',@(1,&))"
                }
            }, { //concat ids
                "operation": "modify-overwrite-beta",
                "spec": {
                "accountAddress": {
                    "*": {
                    "Fprim": "=concat(@(1,addressId),'~HDADDRESS~',@(3,accountType))",
                    "Gprim": "=concat(@(1,integrationId),@(3,accountType))"
                    }
                }
                }
            },
            {
                "operation": "shift",
                "spec": {
                "accountInfo": {
                    "source": {
                    "Facebook": { //if source is Facebook
                        "@4,accountAddress": {
                        "*": {
                            "Fprim": "PostalAdress.items[&1].key.primaryKey"
                        }
                        }
                    },
                    "Google": { //if source is Google
                        "@4,accountAddress": {
                        "*": {
                            "Gprim": "PostalAdress.items[&1].key.primaryKey"
                        }
                        }
                    }
                    }
                },
                "accountAddress": {
                    "*": {
                    "shipToAddress": "PostalAdress.items[&1].PrimaryAddress.addrTyp",
                    "#PRIMARY": "PostalAdress.items[&1].PrimaryAddress.addrDes",
                    "soldToAddress": "PostalAdress.items[&1].SecondaryAddress.addrTyp",
                    "#OPTIONAL": "PostalAdress.items[&1].SecondaryAddress.addrDes"
                    }
                }
                }
            }
        ]
    

    enter image description here