-1

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)

Mythri
  • 21
  • 8

2 Answers2

1
%dw 2.0
output application/json
---
 "(" ++ ((payload mapObject {
    a:("(" ++ ($..text  joinBy " and ") ++ ")")
}) pluck $ joinBy " or ") ++ ")"
Salim Khan
  • 4,233
  • 11
  • 15
  • Salim your solution actually helped a lot, thank you.. - but accepting sudish answer - due to the parenthesis – Mythri Mar 25 '22 at 05:52
0

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)
sudhish_s
  • 573
  • 2
  • 5