2

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.

Infinity
  • 484
  • 2
  • 8
  • 21

2 Answers2

10

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.

AnupamBhusari
  • 2,395
  • 2
  • 14
  • 22
4

For anyone needing this and has an array as the payload, you can do this:

%dw 2.0
output application/json
---
payload map ((item, index) -> item ++ primary: true)

Replace the "primary: true" with whatever key-value pair you want.

DW Playground Screenshot Example

https://developer.mulesoft.com/learn/dataweave/ is great for playing around with dataweave code.

shoutist
  • 41
  • 2
  • loved that DW Playground; thank u for sharing it, was my first time to know that there is such an easy way to test dataweave. – Bishoy Hanna Dec 30 '22 at 01:04