Reputation: 47
I am beginner for Mule. I have input payload as below, but I have to replace just one position of lines array the element: "number" with a custom value but the position of replace that value can be different in this context is 1 but can be 2 or another value, can some one help me how can we replace it.
input payload
{
"date": "2022-11-15T19:24:36.871Z",
"lines": [
{
"number": "BUS",
"received": "2022-11-15T19:30:17.955Z"
},
{
"number": "BUS",
"received": "2022-11-15T19:30:57.426Z"
},
{
"number": "BUS",
"received": "2022-11-15T19:31:49.042Z"
}
]
}
expected payload (position dynamic) in this example is 0
{
"date": "2022-11-15T19:24:36.871Z",
"lines": [
{
"number": "CUSTOM",
"received": "2022-11-15T19:30:17.955Z"
},
{
"number": "BUS",
"received": "2022-11-15T19:30:57.426Z"
},
{
"number": "BUS",
"received": "2022-11-15T19:31:49.042Z"
}
]
}
expected payload (position dynamic) in this example is 1
{
"date": "2022-11-15T19:24:36.871Z",
"lines": [
{
"number": "BUS",
"received": "2022-11-15T19:30:17.955Z"
},
{
"number": "CUSTOM",
"received": "2022-11-15T19:30:57.426Z"
},
{
"number": "BUS",
"received": "2022-11-15T19:31:49.042Z"
}
]
}
As you can see in this example, I need to search within the line array for position 0 in the number element to change its value, but it may be that it is another position with another value and it should be possible, the rest should remain as the original message entered.
Upvotes: 0
Views: 502
Reputation: 25664
You can use the update
operator, first with an index to find the right element in the list, then with the element and the key to update:
%dw 2.0
output application/json
var index=1
---
payload update {
case line at .lines[index] ->
line update {
case .number -> "CUSTOM"
}
}
Upvotes: 3