我想通过比较两个JSON对象来获得要使用dataweave/transform消息更新的特定字段。目前我正在获取对象中的所有字段。
代码:
%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"
}]
}
对于您的具体示例,我可以如下所示:
%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}
]
字段名称b
是否始终相同?您希望有多个字段要测试差异吗?如果是这样的话,给我一个完整的例子(尽可能(,我会看看如何转换它
尽管如此,解决方案的开始(无论是否递归(就是我上面所做的。