Reputation: 71
I have a JSON Array with two elements in it. I would like to combine them to one json. How can I do that in Mule?
Original Payload:
[{
"Response1": {
"lines": [{
"lineIdentifier": 9,
"result1": null
}]
}
}, {
"Response2": {
"lines": [{
"lineIdentifier": 10,
"result2": ".0225"
}]
}
}]
Expected Payload:
{
"Response1": {
"lines": [{
"lineIdentifier": 9,
"result1": null
}]
},
"Response2": {
"lines": [{
"lineIdentifier": 10,
"result2": ".0225"
}]
}
}
Upvotes: 1
Views: 854
Reputation: 4473
Sorry, but I believe you are on the way to wrong goal. You provided simple example which mislead to simple answers.
Let me paraphrase your question as it looks for me:
We have array [a,b,c,...x,y,z]
We need merge it to one object {a1:a, b2:b, c3:c,... z26:z }
You provided only 2 items but that's the idea. If you really want only these particular items then use any of other answers. But if I'm right and you want it in general then even it could be done in many ways consider cons for this solution:
So, in general, this is not good solution when it hardly depend on your data.
Upvotes: 1
Reputation: 2415
Easiest way to use reduce
operator so that you don't have to bother about number of objects. Please try following code
%dw 1.0
%output application/json
---
payload reduce ((value, accumulator = {}) -> accumulator ++ value)
HTH
Upvotes: 1
Reputation: 618
var arr1 = ['a', 'b', 'c'];
var arr2 = ['d', 'e', 'f'];
var arr3 = arr1.concat(arr2);
Upvotes: 0
Reputation: 324
Can be done using dataweave only
%dw 1.0
%output application/json
---
{
"Response1" : {
(payload.Response1.lines map ((item, indexOfItem) -> {
"lines": item
})
)
},
"Response2" : {
(payload.Response2.lines map ((item, indexOfItem) -> {
"lines": item
})
)
}
}
Upvotes: 0
Reputation: 341
It can be done using dataweave, here is the logic to achieve it.
%dw 1.0
%var input=[{
"Response1": {
"lines": [{
"lineIdentifier": 9,
"result1": null
}]
}
}, {
"Response2": {
"lines": [{
"lineIdentifier": 10,
"result2": ".0225"
}]
}
}]
%output application/json
---
{
"Response1":input[0].Response1,
"Response2": input[1].Response2
}
Upvotes: 2