Json Mule Dataweave-添加嵌套和简单属性
我需要使用Dataweave 3.0向现有JSON负载添加新的简单和嵌套属性。我将在下面发布示例有效负载:Json Mule Dataweave-添加嵌套和简单属性,json,mule,dataweave,mule-esb,mulesoft,Json,Mule,Dataweave,Mule Esb,Mulesoft,我需要使用Dataweave 3.0向现有JSON负载添加新的简单和嵌套属性。我将在下面发布示例有效负载: { "entities": [ { "ID": "ABC", "sourceEnt": { "entityId": "100A", "entity": { "Code": "AB", "Idf1": "1pwe", "Idf2": null,
{
"entities": [
{
"ID": "ABC",
"sourceEnt": {
"entityId": "100A",
"entity": {
"Code": "AB",
"Idf1": "1pwe",
"Idf2": null,
"OrgAddr": [
{
"OrgAddrIdf1": "1pwe1",
"Rank": 1,
"Label": "One",
"MainAddr": {
"AddrLine1": "abc",
"PoBox": 123,
"DistrictCode": null
}
},
{
"OrgAddrIdf1": "1pwe2",
"Rank": 2,
"Label": "Two",
"MainAddr": {
"AddrLine1": "xyz",
"PoBox": 456,
"DistrictCode": null
}
}
]
}
}
}
]
}
在上面的有效负载中,我需要向OrgAddr.MainAddr添加一个新属性StateCode:null,并在OrgAddr之后添加一个名为Flag:Yes的新属性。我可以在末尾添加新的Flag属性,但是如何修改嵌套属性OrgAddr呢。
请注意,我需要将简单属性和嵌套属性添加到一起。非常有趣的用例。我可以通过创建两个帮助函数来解决这个问题,这两个函数允许我更新字段
%dw 2.0
output application/json
/**
* Updates the value of the specified field If the field doesn't exits it will be inserted at the bottom. Use this function with `with`. The with will receive the old value
*/
fun update(objectValue: Object, fieldName: String) =
(newValueProvider: (oldValue: Any, index: Number) -> Any) -> do {
if(objectValue[fieldName]?)
objectValue mapObject ((value, key, index) ->
if(key ~= fieldName)
{(key): newValueProvider(value, index)}
else
{(key): value}
)
else
objectValue ++ {(fieldName): newValueProvider(null, 0)}
}
/**
* Updates the value at the specified index. If the index is bigger than the size it will be appended. Use this function with `with`. The with will receive the old value
*/
fun update(arrayValue: Array, indexToUpdate: Number) =
(newValueProvider: (oldValue: Any, index: Number) -> Any) -> do {
if(arrayValue[indexToUpdate]?)
arrayValue map ((value, index) ->
if(index == indexToUpdate)
newValueProvider(value, index)
else
value
)
else
arrayValue << newValueProvider(null, 0)
}
---
payload update "entities" with (
$ update 0 with (
$ update "sourceEnt" with (
$ update "entity" with (
$ update "OrgAddr" with (
$ map ((item, index) -> item update "MainAddr" with ($ ++ {"StateCode": null}) )
) ++ {
"Flag" : "yes"
}
)
)
)
)
“两个更新”功能可帮助我遍历对象并更新树结构中需要更新或修改的部分。您可以尝试以下方法:
%dw 1.0
%output application/json
%var pay=flatten payload.entities.sourceEnt.entity.OrgAddr
---
{(pay)} mapObject {
($$):$ when ($$ as :string) != "MainAddr" otherwise {"StateCode": "null"} ++ $
}
DataWeave 2.3版本的答案
payload update {
case entity at.entities[0].sourceEnt.entity -> do {
entity update {
case addresses at .OrgAddr -> do {
addresses map ((addr, index) -> addr update {
case .MainAddr.StateCode! -> null
})
}
case .Flag! -> "Yes"
}
}
}
谢谢你的回答。我使用的是Mule 3.9和%dw 1.0。这样行吗?无论如何,我都会尝试一下。如果您能发布与dw 2.3 update operator中发布的更新函数相对应的%dw 1.0函数show do这些更新函数,那就太好了。它是作为一个操作符还是一个函数来实现的,如图所示?我还对显示的语法感到困惑。这可以用于任何DW函数吗?2.3中的语法将在选择器表达式上提供类型推断和工具帮助。有效负载更新{case entity at.entities[0].sourceEnt.entity->do{entity update{case address at.OrgAddr->do{addresses map addr,index->addr update{case.MainAddr.StateCode!->null}}case.Flag!-->Yes} } }