Reputation: 645
I need to add new attributes (simple and nested) to an existing JSON payload using Dataweave (3.0). I'm posting the sample payload below:
{
"entities": [
{
"ID": "ABC",
"sourceEnt": {
"entityId": "100A",
"entity": {
"Code": "AB",
"Idf1": "1pwe",
"Idf2": null,
"OrgAddr": [
{
"OrgAddrIdf1": "1pwe1",
"Rank": 1,
"Label": "One",
"MainAddr": {
"AddrLine1": "abc",
"PoBox": 123,
"DistrictCode": null
}
},
{
"OrgAddrIdf1": "1pwe2",
"Rank": 2,
"Label": "Two",
"MainAddr": {
"AddrLine1": "xyz",
"PoBox": 456,
"DistrictCode": null
}
}
]
}
}
}
]
}
In the above payload, I need to add a new attribute ("StateCode": "null") to OrgAddr.MainAddr and also a new attribute called "Flag": "Yes", after OrgAddr. I can add the new "Flag" attribute at the end, but how can I modify a nested attribute (OrgAddr). Please note that I need to add the simple and nested attributes together.
Upvotes: 0
Views: 1637
Reputation: 5059
Answer for 2.3 version of DataWeave
payload update {
case entity at.entities[0].sourceEnt.entity -> do {
entity update {
case addresses at .OrgAddr -> do {
addresses map ((addr, index) -> addr update {
case .MainAddr.StateCode! -> null
})
}
case .Flag! -> "Yes"
}
}
}
Upvotes: 3
Reputation: 21
You can try this :
%dw 1.0
%output application/json
%var pay=flatten payload.entities.sourceEnt.entity.OrgAddr
---
{(pay)} mapObject {
($$):$ when ($$ as :string) != "MainAddr" otherwise {"StateCode": "null"} ++ $
}
Upvotes: 2
Reputation: 5059
very intersting use case. I was able to solve it by creating two helper functions that allows me to update the fields.
%dw 2.0
output application/json
/**
* Updates the value of the specified field If the field doesn't exits it will be inserted at the bottom. Use this function with `with`. The with will receive the old value
*/
fun update(objectValue: Object, fieldName: String) =
(newValueProvider: (oldValue: Any, index: Number) -> Any) -> do {
if(objectValue[fieldName]?)
objectValue mapObject ((value, key, index) ->
if(key ~= fieldName)
{(key): newValueProvider(value, index)}
else
{(key): value}
)
else
objectValue ++ {(fieldName): newValueProvider(null, 0)}
}
/**
* Updates the value at the specified index. If the index is bigger than the size it will be appended. Use this function with `with`. The with will receive the old value
*/
fun update(arrayValue: Array, indexToUpdate: Number) =
(newValueProvider: (oldValue: Any, index: Number) -> Any) -> do {
if(arrayValue[indexToUpdate]?)
arrayValue map ((value, index) ->
if(index == indexToUpdate)
newValueProvider(value, index)
else
value
)
else
arrayValue << newValueProvider(null, 0)
}
---
payload update "entities" with (
$ update 0 with (
$ update "sourceEnt" with (
$ update "entity" with (
$ update "OrgAddr" with (
$ map ((item, index) -> item update "MainAddr" with ($ ++ {"StateCode": null}) )
) ++ {
"Flag" : "yes"
}
)
)
)
)
The two update function help me traverse the object and update the parts of the tree structure that need to be updated or modified.
Upvotes: 3