如何显示特定字段以在Mulesoft的dataweave中进行更新

问题描述 投票:1回答:1

我想通过比较两个JSON对象来使用dataweave / transform消息获取要更新的特定字段。目前,我正在获取对象中的整个字段。

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)

预期结果:

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

当前版本:(我不想包含“ c”:“ dog”,因为它没有更新)

"b":[{
   "oldValue":{
      "c":"dog",
      "d":"egg"
   },
   "newValue":{
      "c":"dog"
      "d":"eagle"
  }]
}
anypoint-studio dataweave mulesoft
1个回答
0
投票

对于您的特定示例,我可以如下所示进行操作:

%dw 1.0
%output application/dw
%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}
]

字段名称b是否始终相同?您是否希望有多个要测试差异的字段?如果是这样,请向我提供一个完整的示例(可能的话),我将看到如何进行转换。

尽管如此,解决方案的开始(无论是否递归)都是我在上面所做的。

© www.soinside.com 2019 - 2024. All rights reserved.