Home > Software engineering >  Appending array elements in a JSON
Appending array elements in a JSON

Time:03-24

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)

CodePudding user response:

Try this

DW

%dw 2.0
output text/plain
---
"("    
(valuesOf(payload)
   map ("("  ($[0].text default "")    
     if($[1].text == null)
        ")"
     else 
        (" and "  $[1].text  ")")) joinBy " or ") 
    ")"

Output

((Line0-1 and Line0-2) or (Line1-1 and Line1-2) or (Line2-1))

CodePudding user response:

%dw 2.0
output application/json
---
 "("    ((payload mapObject {
    a:("("    ($..text  joinBy " and ")    ")")
}) pluck $ joinBy " or ")    ")"
  • Related