Search code examples
muledataweavemule-esb

Mule- Dataweave to insert new json field in the existing payload


Sample Json Request

{ "firstName": "George", "lastName": "Stephen" }

Sample Json Response

{ "id": "123", "firstName" : "George", "lastName" : "Stephen" }

I want to do insert id value into Response Json without doing one to one mapping in dataweave ( I already have the working solution which does one to one mapping in dataweave and 2) using groovy component).

My Original JSON Request is huge and lots of non-mandatory field that's why i am experimenting this way.


Solution

  • Simplest approach is to use ++ operator in dataweave like

    %dw 1.0
    %output application/json
    ---
    payload ++  {id : "123"}
    

    If you have to update child object you can use mapObject. This will iterate over each key.So based on key name you can use ++ to add field to child object.

    Hope this helps.