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

Creating one more field in list of objects by using the fields in same objects

GBekkanti
New Contributor III

Hi Team,
I have the below input data :
[
{
ID:123
โ€œNameโ€:โ€œTonyโ€,
โ€œAgeโ€:24
},
{
ID:124
โ€œNameโ€:โ€œSamโ€,
โ€œAgeโ€:36
},
{
ID:125
โ€œNameโ€:โ€œGlennโ€,
โ€œAgeโ€:58
}]

I need to add one more field โ€˜halfAgeโ€™ to the above and output data should be like below:

[
{
ID:123
โ€œNameโ€:โ€œTonyโ€,
โ€œAgeโ€:24,
โ€œhalfAgeโ€:12
},
{
ID:124
โ€œNameโ€:โ€œSamโ€,
โ€œAgeโ€:36
โ€œhalfAgeโ€:18
},
{
ID:125
โ€œNameโ€:โ€œGlennโ€,
โ€œAgeโ€:58,
โ€œhalfAgeโ€:29
}]

Can you please let me know the Expression language for this?

Thanks in Advance!

7 REPLIES 7

christwr
Contributor III

Use a mapper, pass existing fields through, and create a new field $halfAge = $Age / 2

image

GBekkanti
New Contributor III

Sorry for my wrong input structure. the below is the correct one. It is a list of objects rather than 3 single objects

{โ€˜itemsโ€™:[
{
โ€˜IDโ€™:123,
โ€˜Nameโ€™:โ€˜Tonyโ€™,
โ€˜Ageโ€™:24
},
{
โ€˜IDโ€™:124,
โ€˜Nameโ€™:โ€˜Samโ€™,
โ€˜Ageโ€™:36
},
{
โ€˜IDโ€™:125,
โ€˜Nameโ€™:โ€˜Glennโ€™,
โ€˜Ageโ€™:58
}]}

and the view in mapper is like below:
mapperView

Can you please process now?

Since youโ€™re manipulating an array, youโ€™ll want to look into the โ€œMapping Rootโ€ feature of the Mapper. By pointing the Mapping Root at your array, the Transformations will be applied to the elements of the array instead of the top-level document. Otherwise, the Mapper configuration is the same as suggested by @christwr:

image

GBekkanti
New Contributor III

Thanks for your reply @tstack and @christwr,

I have one more array in input like below

โ€˜genderArrayโ€™:[โ€˜Maleโ€™,โ€˜Femaleโ€™,โ€˜Maleโ€™]

Now, I need to add the corresponding values to the objects of itemsโ€™ array by creating new โ€˜Genderโ€™ field.

output should be like below:

{โ€˜itemsโ€™:[
{
โ€˜IDโ€™:123,
โ€˜Nameโ€™:โ€˜Tonyโ€™,
โ€˜Ageโ€™:24,
โ€˜Genderโ€™:โ€˜Maleโ€™
},
{
โ€˜IDโ€™:124,
โ€˜Nameโ€™:โ€˜Samโ€™,
โ€˜Ageโ€™:36,
โ€˜Genderโ€™:โ€˜Femaleโ€™
},
{
โ€˜IDโ€™:125,
โ€˜Nameโ€™:โ€˜Glennโ€™,
โ€˜Ageโ€™:58,
โ€˜Genderโ€™:โ€˜Maleโ€™
}]}

Can you please let me know the process?