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

How to achieve below one : merging documents

kkishore
New Contributor III

Hi All,

How to achieve this in snaplogic . I tried using different snaps like joiner,union,group by , but none is giving desired output.

Input:

[
{
โ€œfilenameโ€: โ€œa1.datโ€,

},

"filename": "a2.dat",

},
{
โ€œcountโ€: 2,
โ€œtypeโ€: โ€œpdfโ€
}
]

expected output is:

[
{
โ€œfilenameโ€: โ€œa1.datโ€,
โ€œcountโ€: 2,
โ€œtypeโ€: โ€œpdfโ€

},

"filename": "a2.dat",
 "count": 2,
"type": "pdf"

}

]

4 REPLIES 4

j_angelevski
Contributor III

Hi @kkishore,

This can be done with the Group by N snap, but first I need to know if we can always expect the
{ "count": 2, "type": "pdf" } object to be the last element in the input ?

Hi @j.angelevski,

It is not same, it changes like { โ€œcountโ€:54, โ€œtypeโ€: โ€œexcel sheetโ€} or { โ€œcountโ€:72, โ€œtypeโ€: โ€œwordโ€} .
depending on the source it varies.

j_angelevski
Contributor III

@kkishore, assuming that the last object will be in the format that you provided, you can implement the following solution.

  1. Group by N - to group the input data in a single array
    image
  2. Mapper
    Use the following expression, with this you can add the 'count' object to each input document.
$group.filter(val => val.get("count") == null).map(val => val.extend($group[$group.length - 1]))

Target path: $array
image

Result:

{
    "array": [
      {
        "filename": "a1.dat",
        "count": 2,
        "type": "pdf"
      },
      {
        "filename": "a2.dat",
        "count": 2,
        "type": "pdf"
      }
    ]
  }

After this, you can use a JSON Splitter if you want to split the array.

Hi @j.angelevski ,

Thanks for the response and it is working as expected.