-1

I need to compare two arrays and replace values if id field is matched between these arrays. Below are the two sample arrays:

array1 = [
      {
        "Id": "test1",
        "Date": "2021-11-05T12:53:00.000Z",
        "name": "data"
      },
      {
        "Id": "test2",
        "Date": "2021-11-05T12:53:00.000Z",
        "name": "data"
      }
    ]
array2 = = [
      {
        "Id": "test1",
        "Date": "2021-11-05T12:53:00.000Z",
        "fname": "david",
        "lname": "john"

      },
      {
        "Id": "test4",
        "Date": "2021-11-05T12:53:00.000Z",
        "fname": "Chris",
        "lname": "Smith"
      }
    ]

In these two arrays if Id is same I need to replace "fname" from second array to "name" from first array. And if id is not same I'll print the "fname" from second array.

logic is similar like this:

if array1.id = array2.id then replace array2.fname with array1.name else keep array2.fname. In the above example since test1 is matched I should get below output:

[
          {
            "Id": "test1",
            "Date": "2021-11-05T12:53:00.000Z",
            "fname": "data",
            "lname": "john"

          },
          {
            "Id": "test4",
            "Date": "2021-11-05T12:53:00.000Z",
            "fname": "Chris",
            "lname": "Smith"
          }
        ]
Zak01
  • 7
  • 3

2 Answers2

1

You can use Update function to update name based on the condition where array2.Id == array1.Id Alternatively you can use if-else logic as well

%dw 2.0
import * from dw::util::Values
output application/json
var array1 = [
      {
        "Id": "test1",
        "Date": "2021-11-05T12:53:00.000Z",
        "name": "data"
      },
      {
        "Id": "test2",
        "Date": "2021-11-05T12:53:00.000Z",
        "name": "data"
      }
    ]
var array2 = [
      {
        "Id": "test1",
        "Date": "2021-11-05T12:53:00.000Z",
        "fname": "david",
        "lname": "john"
      },
      {
        "Id": "test4",
        "Date": "2021-11-05T12:53:00.000Z",
        "fname": "Chris",
        "lname": "Smith"
      }
    ]
---
(array1 flatMap(item,index)->(array2 filter (($.Id)==(item.Id))) map(
    ($ update "fname" with (item.name))
))++ array2 distinctBy $.Id

Output

[
  {
    "Id": "test1",
    "Date": "2021-11-05T12:53:00.000Z",
    "fname": "data",
    "lname": "john"
  },
  {
    "Id": "test4",
    "Date": "2021-11-05T12:53:00.000Z",
    "fname": "Chris",
    "lname": "Smith"
  }
]
Karthik
  • 2,181
  • 4
  • 10
  • 28
  • Hi @Karthik, what if we need to replace the value for fname field with some hard coded text. For example, if id is same in both arrays replace fname with "Matched" else "Not matched". – Zak01 Mar 20 '22 at 17:24
0

Another approach is to use groupBy and update operator.

%dw 2.0
output application/json
var array1 = [
      {
        "Id": "test1",
        "Date": "2021-11-05T12:53:00.000Z",
        "name": "data"
      },
      {
        "Id": "test2",
        "Date": "2021-11-05T12:53:00.000Z",
        "name": "data"
      }
    ]
var array2 = [
      {
        "Id": "test1",
        "Date": "2021-11-05T12:53:00.000Z",
        "fname": "david",
        "lname": "john"
      },
      {
        "Id": "test4",
        "Date": "2021-11-05T12:53:00.000Z",
        "fname": "Chris",
        "lname": "Smith"
      }
]

var array1byId = array1 groupBy ((item) -> item.Id)

---
array2 map ((item) -> do {
    var array1Item = array1byId [item.Id]
    //Requirement changed to set "Matched" if match found, else "Not Matched"
    //var name = if (isEmpty (array1Item)) item.fname else array1Item[0].name
    var name = if (isEmpty (array1Item)) "Not Matched" else "Matched"
    ---
    item update {
        case ."fname" -> name
    }
})
sudhish_s
  • 573
  • 2
  • 5
  • Thanks for the replay @sudhish_s. Actually there is a bit of a change in requirement. Now if Id is same in two arrays I need to replace the fname field with a hard codded text. The logic will be if array2.id = array1.id then replace name with "Matched" else "Not Matched". Can you please help in this. I can also post a new question with this new requirement. Thanks again! – Zak01 Mar 20 '22 at 17:19
  • @Zak01 Please update the question. Do you want update the name or fname with "Matched"/"Not Matched"? It would be helpful to update the desired output with what you are looking for. – sudhish_s Mar 21 '22 at 17:06
  • Assuming you want to keep the `fname` with the hardcoded text, I have updated the answer (commented out previous mapping from name/fname). let me know if a different output is required. – sudhish_s Mar 21 '22 at 17:28
  • Thanks @sudhish_s. I am going to post a new question as this is getting a bit confusing. I am going to mark this question complete. Thanks again for your help! – Zak01 Mar 25 '22 at 02:37