cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Transform Array to New array

sravan
New Contributor II

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 4

AleksandarAngel
Contributor III

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.

image

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

image

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.

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
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
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โ€

}