在 JSON 中追加数组元素

Appending array elements in a JSON

任何人都可以指导我如何在 dataweave 2.0 中附加 json 数组元素内的值。 输入JSON:

{
    "0": [
        {
            "text": "Line0-1"
        },
        {
            "text": "Line0-2"
        }
    ],
    "1": [
        {
            "text": "Line1-1"
        },
        {
            "text": "Line1-2"
        }
    ],
    "2": [
        {
            "text": "Line2-1"
        }
    ]
}

添加后应该是这样的:

((Line0-1 和 Line0-2) 或 (Line1-1 和 Line1-2) 或 Line2-1)

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

与其他答案类似。为了与输出相匹配,我添加了一个检查以仅在多个 text 元素的情况下将文本包装在括号中。此答案适用于任意数量的 text 元素和任意位置的单个文本元素。

%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 "
    )
) ++ ")"

输出

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