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

Transforming into JSON Array

Tanmay_Sarkar
New Contributor III

Hello All,

I have a requirement where I have to merge two same keys within a JSON array to form a JSON array of themselves. The below provided examples will clear the requirement:

Input JSON:

[{
โ€œheaderโ€: {
โ€œbatch_idโ€: โ€œPVTS_20200919_866184โ€,
โ€œcompany_codeโ€: 200,
โ€œregionโ€: โ€œLSAโ€,
โ€œlineโ€: [{
โ€œprojectnumberโ€: โ€œ1000165โ€,
โ€œio_numberโ€: โ€œ200-3754โ€,
โ€œamountโ€: โ€œ4207.20โ€,
โ€œidโ€: 1
}, {
โ€œprojectnumberโ€: โ€œ1000165โ€,
โ€œio_numberโ€: โ€œ200-345233โ€,
โ€œamountโ€: โ€œ1682.88โ€,
โ€œidโ€: 2
}]
}
}, {
โ€œheaderโ€: {
โ€œbatch_idโ€: โ€œPVTS_20200919_866184โ€,
โ€œcompany_codeโ€: 334,
โ€œregionโ€: โ€œS4Fโ€,
โ€œlineโ€: [{
โ€œprojectnumberโ€: โ€œ1000173โ€,
โ€œio_numberโ€: โ€œ334-674294โ€,
โ€œamountโ€: โ€œ4522.74โ€,
โ€œidโ€: 1
}]
}
}]

Required out put is to form a JSON with header key being the root array and everything else comes under it.

Required Output Example:

{
โ€œheaderโ€: [{
โ€œbatch_idโ€: โ€œPVTS_20200919_866184โ€,
โ€œcompany_codeโ€: 200,
โ€œregionโ€: โ€œLSAโ€,
โ€œlineโ€: [{
โ€œprojectnumberโ€: โ€œ1000165โ€,
โ€œio_numberโ€: โ€œ200-3754โ€,
โ€œamountโ€: โ€œ4207.20โ€,
โ€œidโ€: 1
}, {
โ€œprojectnumberโ€: โ€œ1000165โ€,
โ€œio_numberโ€: โ€œ200-345233โ€,
โ€œamountโ€: โ€œ1682.88โ€,
โ€œidโ€: 2
}]
},
{
โ€œbatch_idโ€: โ€œPVTS_20200919_866184โ€,
โ€œcompany_codeโ€: 334,
โ€œregionโ€: โ€œS4Fโ€,
โ€œlineโ€: [{
โ€œprojectnumberโ€: โ€œ1000173โ€,
โ€œio_numberโ€: โ€œ334-674294โ€,
โ€œamountโ€: โ€œ4522.74โ€,
โ€œidโ€: 1
}]
}
]
}

Iโ€™ve tried few things, but it doesnโ€™t seem to help. Can someone please provide any insights on how to achieve this?

1 ACCEPTED SOLUTION

alchemiz
Contributor III

Hi Tanmay,

You can use Gate snap to setup the streaming documents into array, then you can manipulate the array from there.

image

View solution in original post

2 REPLIES 2

alchemiz
Contributor III

Hi Tanmay,

You can use Gate snap to setup the streaming documents into array, then you can manipulate the array from there.

image

Thank you for this @alchemiz . This did the job.
I tried another way too with the help of a mapper and group by N snap.
image