0

I have situation to merge global array with another custom array or if the name is the same use custom values.

Global:

{
  "connections": [
   {
    "name": "Test SFTP",
    "type": "SFTP",
    "user": "sftpuser",
    "password": "password",
    "server": "127.0.0.1",
    "port": 22,
   },
   {
    "name": "Test FTP",
    "type": "FTP",
    "user": "ftpuser",
    "password": "password",
    "server": "127.0.0.1",
    "port": 21,
   }
 ]
}

Custom:

{
  "connections": [
   {
    "name": "Test SFTP",
    "user": "sftpuser1",
    "password": "password1",
    "server": "127.0.0.2",
   },

   {
    "name": "Test FTPS",
    "type": "FTPS",
    "user": "ftpsuser",
    "password": "password",
    "server": "127.0.0.1",
    "port": 990,
   }
 ]
}

Expected:

{
  "connections": [
   {
    "name": "Test SFTP",
    "type": "SFTP",
    "user": "sftpuser1",
    "password": "password1",
    "server": "127.0.0.2",
    "port": 22,
   },
   {
    "name": "Test FTP",
    "type": "FTP",
    "user": "ftpuser",
    "password": "password",
    "server": "127.0.0.1",
    "port": 21,
   },
   {
    "name": "Test FTPS",
    "type": "FTPS",
    "user": "ftpsuser",
    "password": "password",
    "server": "127.0.0.1",
    "port": 990,
   }
 ]
}

Global will always have all fields but custom can have name + just one field to override global.

Later on I will validate if the json is ok but for now i just need to merge and overwrite.

Thanks, Ivan

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34

1 Answers1

1

Below script will help you.

 %dw 2.0
output application/json
import mergeWith from dw::core::Objects
import * from dw::core::Arrays
var global = {
  "connections": [
   {
    "name": "Test SFTP",
    "type": "SFTP",
    "user": "sftpuser",
    "password": "password",
    "server": "127.0.0.1",
    "port": 22,
   },
   {
    "name": "Test FTP",
    "type": "FTP",
    "user": "ftpuser",
    "password": "password",
    "server": "127.0.0.1",
    "port": 21,
   }
 ]
}
var custom  = {
  "connections": [
   {
    "name": "Test SFTP",
    "user": "sftpuser1",
    "password": "password1",
    "server": "127.0.0.2",
   },

   {
    "name": "Test FTPS",
    "type": "FTPS",
    "user": "ftpsuser",
    "password": "password",
    "server": "127.0.0.1",
    "port": 990,
   }
 ]
}
---
outerJoin(global.connections, custom.connections, (g) -> g.name, (c) -> c.name) map  ($.l  mergeWith $.r ) 
  • 1
    You can directly use `dw::core::Objects::mergeWith()` instead of ++ for merging the object. the distinct by seems like a dirty workaround for avoiding duplicate keys. `($.l default {}) mergeWith ($.r default {})` like this. You can avoid the distinctBy logic altogether – Harshank Bansal Nov 02 '22 at 10:23
  • And what if inside each object it has another array which also need to be updated? – user20350110 Nov 02 '22 at 12:38
  • You can use your own custom function (which handles arrays too) for merging instead of the `dw::core::Objects::mergeWith()`. You can use the function from [this answer]( https://stackoverflow.com/questions/73886401/how-to-compare-and-merge-two-json-objects-using-dataweave-2-0/73891449#73891449). If it is still unclear, you can accept the answer here and create a new question as updating the question will invalidate this answer which is valid for the original problem – Harshank Bansal Nov 03 '22 at 04:27