【问题标题】:Modify existing payload array using Dataweave使用 Dataweave 修改现有的有效负载数组
【发布时间】:2019-07-01 09:06:03
【问题描述】:

请查看我传入的 mule 有效负载:(我使用的是 Mule 3.9)

 {
        "source": {
            "code": "CD-12"
        },
        "target": {
            "code": "CD-22"
        },
        "entities": [
            {
                "ID": "ABC",
                "sourceEnt": {
                    "entityId": "100A",
                    "entityName": "ID-1"
                },
                "targetEnt": {
                    "Account": {
                        "Key1": "Value1",
                        "Key2": "Value2"
                    },
                    "Address": [
                        {
                            "Name21": "Value21"
                        }
                    ],
                    "AccountAddress": [
                        {
                            "Key31": "Value31",
                            "Key32": "Key32"
                        }
                    ]
                }
            }
        ]
    }

我是 Mule dataweave 的新手。如何修改某个实体下的特定数组。例如,如果我需要在targetEnt Account下添加“Key3”和“Value3”? 我在下面粘贴我的示例代码。

%dw 1.0
%output application/json
---
{

    entities : payload.entities map 
    {
        ID: ID
        entity : $.targetEnt
    } //++ {"Key3":"Value3"} when $.targetEnt: "Account"
}

【问题讨论】:

    标签: json mule mule-component dataweave mule-esb


    【解决方案1】:

    我认为你需要做的是

    %dw 1.0
    %output application/json
    ---
    {
    
        entities : payload.entities map ((item, index) ->
        {
            ID: item.ID,
            entity : item.targetEnt mapObject ((value, key) -> { 
              (key): value ++ {"Key3":"Value3"} when  key ~= "Account" otherwise value
    
            })
        }) 
    }
    

    【讨论】:

    • 感谢您的回复。如果 targetEnt.Account 是一个有多个数组的对象,上面的方法会起作用吗?再次感谢
    • 是的,它会尝试一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    相关资源
    最近更新 更多