'Appending array elements in a JSON
Can anyone guide me on how to append the values of inside a json array element in dataweave 2.0. InputJSON:
{
"0": [
{
"text": "Line0-1"
},
{
"text": "Line0-2"
}
],
"1": [
{
"text": "Line1-1"
},
{
"text": "Line1-2"
}
],
"2": [
{
"text": "Line2-1"
}
]
}
After appending it should be something like this:
((Line0-1 and Line0-2) or (Line1-1 and Line1-2) or Line2-1)
Solution 1:[1]
Similar to the other answers. To match with the output, I have added a check to wrap the texts in parenthesis only incase of multiple text elements. This answer will work with any number of text elements and single text element in any position.
%dw 2.0
output application/java
---
"(" ++ (
(
(
valuesOf (payload) map (
do {
var texts = $..text
var len = sizeOf (texts)
---
if (len == 1) texts[0] else ("(" ++ (texts joinBy " and ") ++ ")")
}
)
) joinBy " or "
)
) ++ ")"
Output
((Line0-1 and Line0-2) or (Line1-1 and Line1-2) or Line2-1)
Solution 2:[2]
%dw 2.0
output application/json
---
"(" ++ ((payload mapObject {
a:("(" ++ ($..text joinBy " and ") ++ ")")
}) pluck $ joinBy " or ") ++ ")"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Salim Khan |
