Forum Discussion

kkishore's avatar
kkishore
New Contributor III
4 years ago

How to achieve below one : merging documents

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

  • 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 ?

    • kkishore's avatar
      kkishore
      New Contributor III

      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.

  • @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
    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

    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.