San
San

Reputation: 359

How to display specific field to update in Mulesoft's dataweave

I want to get the specific field to update using dataweave/transform message by comparing two JSON object. Currently I'm getting the whole fields in the object.

CODE:

%dw 1.0
%output application/json
%var old = {
   "b":{
       "c":"dog",
       "d":"egg"
    }
}
%var new = {
   "b":{
       "c":"dog",
       "d":"eagle"
    }
 }
%function updateValue(newValue, newKey)
    null when old."$newKey" == newValue and old."$newKey" != null otherwise {
        newValuealue: newValue default null,
        oldValue: old."$newKey" default null
    }
%function compare(v)
    v match {
        :object -> $ mapObject ((v,k) -> {
            (k): updateValue(v,k)
        }),
        default -> updateValue(v)
    }
---
compare(new)

EXPECTED RESULT:

"b":[{
   "oldValue":{
      "d":"egg"
   },
   "newValue":{
      "d":"eagle"
  }]
}

Current build: (I don't want to include the "c":"dog" as it's not updated)

"b":[{
   "oldValue":{
      "c":"dog",
      "d":"egg"
   },
   "newValue":{
      "c":"dog"
      "d":"eagle"
  }]
}

Upvotes: 1

Views: 239

Answers (1)

user3078986
user3078986

Reputation:

For your specific example I can do as seen below:

%dw 1.0
%output application/json
%var old = {
   "b":{
       "c":"dog",
       "d":"egg"
    }
}
%var new = {
   "b":{
       "c":"dog",
       "d":"eagle"
    }
 }
%var both = old.b ++ new.b
---
b: [
    {oldvalue: both -- new.b},
    {newvalue: both -- old.b}
]

Is the field name b always the same? Do you expect to have multiple fields you want to test for difference? If so provide me with a complete example (to the extent possible) and I 'll see how to transform it.

Nonetheless the start of a solution (recursive or not) is what I have done above.

Upvotes: 2

Related Questions