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.