Introduction
In the last part DataWeave Using mapObject - Part I. I presented a way to add new attribute to the payload. In this post, I am going to demonstrate the dataweave code for change keys of the JSON payload.Input Data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | [ { "worker_id" : "AMAGWK00002673" , "position_id" : "35119973" , "requisition_id" : "R-32353" , "applicant_id" : "199505" , "staff_id" : "199504" }, { "worker_id" : "AMAGWK00002674" , "position_id" : "35119943" , "requisition_id" : "R-32353" , "applicant_id" : "199505" , "staff_id" : "199506" }, { "worker_id" : "AMAGWK00002675" , "position_id" : "35119975" , "requisition_id" : "R-32354" , "applicant_id" : "199507" , "staff_id" : "" } ] |
Desired Result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | [ { "ew_id" : "AMAGWK00002673" , "position_id" : "35119973" , "requisition_id" : "R-32353" , "applicant_id" : "199505" , "staff_id" : "199504" , "type" : "re-hire" }, { "ew_id" : "AMAGWK00002674" , "position_id" : "35119943" , "requisition_id" : "R-32353" , "applicant_id" : "199505" , "staff_id" : "199506" , "type" : "re-hire" }, { "ew_id" : "AMAGWK00002675" , "position_id" : "35119975" , "requisition_id" : "R-32354" , "applicant_id" : "199507" , "staff_id" : "" , "type" : "new-hire" } ] |
Solution
1 2 3 4 5 6 7 8 9 10 | %dw 1.0 %output application/json --- payload map ((worker) ->{ (worker mapObject (value, key) -> { (ew_id : value) when key as :string == 'worker_id' , ((key) : value) when (key as :string != 'worker_id' ) }), type : "new-hire" when worker.staff_id == null or worker.staff_id == '' otherwise 're-hire' }) |
Key learning Points
From the solution I provided, the key syntax is that if the key is worker_id, we will update the key with new key value which is ew_id.1 2 | (ew_id : value) when key as :string == 'worker_id' , ((key) : value) when (key as :string != 'worker_id' ) |
Very informative post for mulesoft developers.You can also visit goformule.com for mulesoft stuff.
ReplyDelete