โ01-25-2019 12:50 AM
Input json :-[
{
โe_noโ: โ1โ,
โemailโ: โabc@gmail.comโ,
โcountryโ: โIndiaโ,
โfirstnameโ: โAโ,
โlast nameโ: โBโ,
โdataโ: [
{
โe_noโ: โ12โ,
โemailโ: โabc4@gmail.comโ,
โcountryโ: โIndiaโ,
โfirstnameโ: โAโ,
โlast nameโ: โcโ
},
{
โe_noโ: โ13โ,
โemailโ: โabc3@gmail.comโ,
โcountryโ: โUSโ,
โfirstnameโ: โAโ,
โlast nameโ: โdโ
},
{
โe_noโ: โ14โ,
โemailโ: โabc2@gmail.comโ,
โcountryโ: โIndiaโ,
โfirstnameโ: โAโ,
โlast nameโ: โnโ
},
{
โe_noโ: โ15โ,
โemailโ: โabc1@gmail.comโ,
โcountryโ: โUKโ,
โfirstnameโ: โAโ,
โlast nameโ: โfโ
}
]
},
{
โe_noโ: โ4โ,
โemailโ: โa@gmail.comโ,
โcountryโ: โIndiaโ,
โfirstnameโ: โcโ,
โlast nameโ: โDโ
},
{
โe_noโ: โ5โ,
โemailโ: โb@gmail.comโ,
โcountryโ: โUSโ,
โfirstnameโ: โCโ,
โlast nameโ: โDโ
}
]
Output JSON required :-
[
{
โe_noโ: โ1โ,
โemailโ: โabc@gmail.comโ,
โcountryโ: โIndiaโ,
โfirstnameโ: โAโ,
โlast nameโ: โBโ,
โdataโ: [
{
โe_noโ: โ12โ,
โemailโ: โabc4@gmail.comโ,
โcountryโ: โIndiaโ,
โfirstnameโ: โAโ,
โlast nameโ: โcโ,
โageโ:โ12โ
},
{
โe_noโ: โ13โ,
โemailโ: โabc3@gmail.comโ,
โcountryโ: โUSโ,
โfirstnameโ: โAโ,
โlast nameโ: โdโ,
โageโ:โ13โ
},
{
โe_noโ: โ14โ,
โemailโ: โabc2@gmail.comโ,
โcountryโ: โIndiaโ,
โfirstnameโ: โAโ,
โlast nameโ: โnโ,
โageโ:โ14โ
},
{
โe_noโ: โ15โ,
โemailโ: โabc1@gmail.comโ,
โcountryโ: โUKโ,
โfirstnameโ: โAโ,
โlast nameโ: โfโ,
โageโ:โ15โ
}
]
},
{
โe_noโ: โ4โ,
โemailโ: โa@gmail.comโ,
โcountryโ: โIndiaโ,
โfirstnameโ: โcโ,
โlast nameโ: โDโ
},
{
โe_noโ: โ5โ,
โemailโ: โb@gmail.comโ,
โcountryโ: โUSโ,
โfirstnameโ: โCโ,
โlast nameโ: โDโ
}
]
wanted to add element in array with values of e_no.
โ01-25-2019 08:33 AM
Looks like a frustrated emoji on the end of that line ๐
If I understand you correctly, you want to add a new field called age
to each element in your input as well as any field called data
in an input document. To do this you can use the map
function on the data
array to apply a transformation to each element of the array.
Hereโs what it looks like in the expression language:
$.data.map(e => e.extend({'age': e.e_no}))
This says, for each element of $.data
extend it with a new field called age
that is just a mapping of e_no
. Putting this in the mapper will look like
Note that this will also append a data field to each element of the array. If it was missing, then the value will be null
. You could filter these out downstream if you like.
โ01-25-2019 10:01 AM
A colleague (HT @tstack) mentioned an alternative method thatโs a bit simpler. You could use a mapper with the Mapping Root set to $.data[*]
and the just map e_no
to age
in the mapper. Attaching an example pipeline to illustrate the idea.
eno-to-age_2019_01_25.slp (5.8 KB)
โ01-26-2019 07:59 AM
Hi @tlikarish
Thank you for providing solution.it worked.
โ09-20-2021 06:28 AM
What needs to be done if i want to add one more record in this array for example
{
โe_noโ: โ6โ,
โemailโ: โc@gmail.comโ,
โcountryโ: โUSโ,
โfirstnameโ: โCโ,
โlast nameโ: โDโ
}