Forum Discussion

sravan's avatar
sravan
New Contributor II
3 years ago

Transform Array to New array

I have a requirement where I need to convert an array into new array . Below is the example .

INPUT:

{
“cover”: [
{
“needCode”: “abc”,
“needKindName”: “abc xyz”,
“autoBoostCoverAmount”: {
“amount”: 25,
“currencyCode”: “XAF”
},
“coverAmount”: {
“amount”: 0,
“currencyCode”: “XAF”
}
},
{
“needCode”: “ttt”,
“needKindName”: “abc ttt”,
“autoBoostCoverAmount”: {
“amount”: 25,
“currencyCode”: “XAF”
},
“coverAmount”: {
“amount”: 0,
“currencyCode”: “XAF”
}
},
{
“needCode”: “rrr”,
“needKindName”: “abc rrr”,
“autoBoostCoverAmount”: {
“amount”: 25,
“currencyCode”: “XAF”
},
“coverAmount”: {
“amount”: 0,
“currencyCode”: “XAF”
}
}
]
}

OUTPUT

{
“ppCover”: [
{
“needCode”: “abc”,
“needKindName”: “abc xyz”,
“rates”: [
{
“type”: “Cover Amount”,
“amount”: {
“value”: 0.00,
“currencyCode”: “xaf”
}
}
]
},
{
“needCode”: “ttt”,
“needKindName”: “abc ttt”,
“rates”: [
{
“type”: “Cover Amount”,
“amount”: {
“value”: 0.00,
“currencyCode”: “xaf”
}
}
]
},
{
“needCode”: “rrr”,
“needKindName”: “abc rrr”,
“rates”: [
{
“type”: “Cover Amount”,
“amount”: {
“value”: 0.00,
“currencyCode”: “xaf”
}
}
]
}
]
}

Step 1: Cover needs to be renamed as “ppCover”
Step2: Create a new array of objects called rates inside ppcover array .
Step3: field type inside the rates array is “coverAmount” as we have the coverAmount object in Input .if there is no coverObject in the input then it should be empty

please help how we can manage this using map function.

@bojanvelevski In case if you could help us.

4 Replies

  • Hello @sravan,

    In order to achieve the wanted transformations you can use two Mapper Snaps.

    The first one with Mapping Root set to $cover[*] and mapping the elements of the cover array.

    And the second Mapper to rename the cover array to ppCover.

    Also, here is a sample pipeline:
    sl-comm-transform-array_2023_05_31.slp (6.2 KB)

    Try this out, and let me know if this helps you.

    BR,
    Aleksandar.

    • Aleksandar_A's avatar
      Aleksandar_A
      Contributor III

      Or you can use the following map function in one Mapper Snap:

      $cover.map(x => {
      	"needCode":x.needCode,
      	"needKindName":x.needKindName,
      	"rates": x.hasPath("coverAmount") ? [{
      			"type":"Cover Amount",
      			"amount":{
      			"value":x.coverAmount.amount,
      			"currencyCode":x.coverAmount.currencyCode.toLowerCase()
      			}
      		}] : []
      })
      

      If you ask me, I would suggest going with the first approach if possible, since it is cleaner and easier to maintain.

      Try both ways and let me know.

  • sravan's avatar
    sravan
    New Contributor II

    Hi @AleksandarAngelevski

    Both Works.The first method is kind of hard coded one. We need to lookup on coveraount. it yes we need to proceed else create a empty array. Second one is working fine for me. I appreciate your help in this regards

    • sravan's avatar
      sravan
      New Contributor II

      actually this works only if we have covers. I got into another problem now. I am pasting INPUT and OUTPUT here. could you please on how we can transform this. I used json generator to transform but no luck.
      @AleksandarAngelevski

      INPUT:

      {
      “summary”: {
      “airtimeEnabledInsuranceProductCode”: {
      “cover”: [
      {
      “needCode”: “Illness”,
      “needKindName”: “Hospital Illness”,
      “autoBoostCoverAmount”: {
      “amount”: 25,
      “currencyCode”: “XAF”
      },
      “coverAmount”: {
      “amount”: 0,
      “currencyCode”: “XAF”
      }
      },
      {
      “needCode”: “Accident”,
      “needKindName”: “Hospital Accident”,
      “autoBoostCoverAmount”: {
      “amount”: 25,
      “currencyCode”: “XAF”
      },
      “coverAmount”: {
      “amount”: 0,
      “currencyCode”: “XAF”
      }
      },
      {
      “needCode”: “Life”,
      “needKindName”: “Life”,
      “autoBoostCoverAmount”: {
      “amount”: 25,
      “currencyCode”: “XAF”
      },
      “coverAmount”: {
      “amount”: 0,
      “currencyCode”: “XAF”
      }
      }
      ],
      “policyNumber”: “1234567”,
      “policyStatus”: “Not Taken Up”,
      “productCode”: “Airtime Enabled”,
      “coverAmountSegments”: [
      {
      “startDate”:“2022-03-20T00:00:00”,
      “endDate”:“2022-09-19T00:00:00”,
      “sumAssured”:{
      “amount”:10000.0,
      “currencyCode”:“XAF”
      }
      },
      {
      “startDate”:“2022-03-20T00:00:00”,
      “endDate”:“2022-09-19T00:00:00”,
      “sumAssured”:{
      “amount”:5000.0,
      “currencyCode”:“XAF”
      }
      }],
      “coverStatus”: {
      “loyalty”: “ACTIVE”,
      “autoBoost”: “ACTIVE”,
      “prepaid”: “ACTIVE”
      }
      },
      “bonusAmount”: {
      “amount”: 2.50,
      “currencyCode”: “GHS”
      },
      “bonusAmountRequiredToConvert”: {
      “amount”: -155.50,
      “currencyCode”: “XAF”
      },
      “consents”: [
      {
      “agreementReference”: “0006978665”,
      “content”: “Recharge With Care”,
      “reference”: “2150000059599412”,
      “response”: “NO-RESPONSE”,
      “type”: “Recharge With Care Policy Change”
      }
      ],
      “memberId”: “2320000000000”,
      “kycInfo”: {
      “kycStatus”: true,
      “kycConfirmation”: false
      },
      “language”: “English”
      }
      }

      OUTPUT:

      {
      “products”: [
      {
      “type”: “aYo Airtime Enabled”,
      “policyNumber”: “1234567”,
      “policyStatus”: “Not Taken Up”,
      “attributes”: [
      {
      “name”: “Game Level”,
      “value”: “1”
      }
      ],
      “postPaidCover”: [
      {
      “needCode”: “Illness”,
      “needKindName”: “Hospital Illness”,
      “rates”: [
      {
      “type”: “autoBoostCoverAmount”,
      “amount”: {
      “value”: 25,
      “currencyCode”: “XAF”
      }
      },
      {
      “type”: “Cover Amount”,
      “amount”: {
      “value”: 0.00,
      “currencyCode”: “XAF”
      }
      }
      ]
      },
      {
      “needCode”: “Accident”,
      “needKindName”: “Hospital Accident”,
      “rates”: [
      {
      “type”: “autoBoostCoverAmount”,
      “amount”: {
      “value”: 25,
      “currencyCode”: “XAF”
      }
      },
      {
      “type”: “Cover Amount”,
      “amount”: {
      “value”: 0.00,
      “currencyCode”: “XAF”
      }
      }
      ]
      },
      {
      “needCode”: “Life”,
      “needKindName”: “Life”,
      “rates”: [
      {
      “type”: “autoBoostCoverAmount”,
      “amount”: {
      “value”: 25,
      “currencyCode”: “XAF”
      }
      },
      {
      “type”: “Cover Amount”,
      “amount”: {
      “value”: 0.00,
      “currencyCode”: “XAF”
      }
      }
      ]
      }
      ],
      “prepaidCover”: [
      {
      “startDate”: “2022-03-20T00:00:00”,
      “endDate”: “2022-09-19T00:00:00”,
      “amount”: {
      “value”: 100000.0,
      “currencyCode”: “GHS”
      }
      }
      ]
      }
      ],
      “consents”: [
      {
      “agreementReference”: “0006978665”,
      “content”: “Recharge With Care”,
      “reference”: “2150000059599412”,
      “response”: “NO-RESPONSE”,
      “type”: “Recharge With Care Policy Change”
      }
      ],
      “bonus”: [
      {
      “type”: “bonusAmount”,
      “amount”: 2.50,
      “currencyCode”: “GHS”
      },
      {
      “type”: “requiredToConvert”,
      “amount”: -155.50,
      “currencyCode”: “GHS”
      }
      ],
      “memberId”: “2320000000000”,
      “kycInfo”: {
      “kycStatus”: true,
      “kycConfirmation”: false
      },
      “language”: “English”

      }