Search code examples
muledataweavemulesoftmule4

How to Compare two JSON and group them together to get the output


Need to group by ID from the input1.

If the Id in Input1 exist in Input2 then we need to group by ID and concatenate the value separated by colon (;) and the method would be PATCH.

If the Id in Input2 exists and the same Id does not exist in Input1 we need to group the Id in Input2 and and concatenate the value separated by colon (;) and the method would be POST.

If Id in Input1 exist and the same Id does not exist in Input2, then we need to group the Id in Input1 and and concatenate the value separated by colon (;) and the method would be DELETE.

If in the input1 Id and Value matches exactly same as Input2 then we need to create one more extra method with PATCH with current timestamp.

I have tried different option but I am missing something

Input1:

{
  "prod": [
    {
      "Id": "123456",
      "value": "ABC"
    },
    {
      "Id": "123456",
      "value": "DEF"
    },
    {
      "Id": "56789",
      "value": "ABC"
    },
    {
      "Id": "56789",
      "value": "DEF"
    },
    {
      "Id": "987654",
      "value": "DEF"
    }
  ]
}

Input2:

{
  "ProdInfo": {
    "Prod": [
      {
        "Id": "123456",
        "value1": "LMN"
      },
      {
        "Id": "123456",
        "value1": "OPQ"
      },
      {
        "Id": "56789",
        "value1": "ABC"
      },
      {
        "Id": "56789",
        "value1": "DEF"
      },
      {
        "Id": "654321",
        "value1": "OPQ"
      }
    ]
  }
}

Output:

{
  "output": [
    {
      "Request": [
        {
          "method": "PATCH",
          "body": {
            "ID": "123456",
            "Value": "ABC;DEF;LMN;OPQ"
          }
        },
        {
          "method": "PATCH",
          "body": {
            "ID": "56789",
            "Value": "ABC;DEF",
            "CurrentTime": "now()"
          }
        },
        {
          "method": "POST",
          "body": {
            "ID": "654321",
            "Value": "OPQ"
          }
        },
        {
          "method": "DELETE",
          "body": {
            "ID": "987654",
            "Value": "DEF"
          }
        }
      ]
    }
  ]
}

I tried different option but something I am missing.


Solution

  • I modified my previous answer to include the 'delete' case. The example provided doesn't cover it but I assumed there could be more than one value for a 'delete' id and added an example to input1.

    I just collected the ids of the elements that don't match to the other input, map those keys to a new object, joining the values, then just append the result to the array of the previous answer.

    Script:

    %dw 2.0
    output application/json
    var input1={
      "prod": [
        {
          "Id": "123456",
          "value": "ABC"
        },
        {
          "Id": "123456",
          "value": "DEF"
        },
        {
          "Id": "56789",
          "value": "ABC"
        },
        {
          "Id": "56789",
          "value": "DEF"
        },
        {
          "Id": "987654",
          "value": "DEF"
        },
        {
          "Id": "987654",
          "value": "GHI"
        }
    
      ]
    }.prod groupBy ($.Id)
    
    var deleteIds=namesOf(input1) filter !( 
        payload.ProdInfo.Prod.*Id contains $)
    
    var deleteRecord= deleteIds map 
        { 
            method: "DELETE", 
            body: {
                ID: $,
                Value: input1[$].*value joinBy  ";"
            } 
        }
    
    fun equalArrays(v1: Array, v2: Array)=(v1 dw::core::Arrays::every(v2 contains $)) and (v2 dw::core::Arrays::every( v1 contains $))
    ---
    {
        "output": [
            {
                Request: 
                    payload.ProdInfo.Prod 
                        groupBy ($.Id)
                        pluck ((value, key, index) -> {
                            method: if (namesOf(input1) contains (key as String)) "PATCH" else "POST",
                            body: 
                                do {
                                    var v1=input1[key as String].*value default []
                                    var v2=value.*value1  default []
                                    ---
                                    {
                                        ID: key,
                                        Value: if (equalArrays(v1,v2))
                                                    v1 joinBy  ";"
                                                else (v1 ++ v2) joinBy  ";",
                                        (CurrentTime: "now()") if (equalArrays(v1,v2))
                                    }
                                }
                        })
                        ++ deleteRecord
                    
            }
        ]
    } 
    

    Input:

    {
      "ProdInfo": {
        "Prod": [
          {
            "Id": "123456",
            "value1": "LMN"
          },
          {
            "Id": "123456",
            "value1": "OPQ"
          },
          {
            "Id": "56789",
            "value1": "ABC"
          },
          {
            "Id": "56789",
            "value1": "DEF"
          },
          {
            "Id": "654321",
            "value1": "OPQ"
          }
        ]
      }
    }
    

    Output:

    {
      "output": [
        {
          "Request": [
            {
              "method": "PATCH",
              "body": {
                "ID": "123456",
                "Value": "ABC;DEF;LMN;OPQ"
              }
            },
            {
              "method": "PATCH",
              "body": {
                "ID": "56789",
                "Value": "ABC;DEF",
                "CurrentTime": "now()"
              }
            },
            {
              "method": "POST",
              "body": {
                "ID": "654321",
                "Value": "OPQ"
              }
            },
            {
              "method": "DELETE",
              "body": {
                "ID": "987654",
                "Value": "DEF;GHI"
              }
            }
          ]
        }
      ]
    }