Triumph Spitfire
Triumph Spitfire

Reputation: 645

Modify existing payload array using Dataweave

Please see my incoming mule payload: (I'm on 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"
                        }
                    ]
                }
            }
        ]
    }

I am new to Mule dataweave. How do I modify a specific array under a certain entity. For example, if I need to add "Key3" and "Value3" under targetEnt Account? I am pasting my sample code below.

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

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

Upvotes: 0

Views: 347

Answers (1)

machaval
machaval

Reputation: 5059

I think that what you need is to do

%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

        })
    }) 
}

Upvotes: 2

Related Questions