Mule 4 Dataweave:从脚本输出到 JSON 的数组

Mule 4 Dataweave: Array from script output to JSON

脚本模块执行后,我得到以下输出:

[
"0": [
"0": ["A","B","C"],
"1": ["D","E","F"]
],
"1": [
"0": ["A","B","C"],
"1": ["D","E","F"],
"2": ["G","H","I"]
],
"2": [
"0": ["A","B","C"],
"1": ["D","E","F"],
"2": ["G","H","I"]
]
]

而且我只想获取值,而不是键。所以预期的输出将是:

[
   [
      ["A","B","C"],
      ["D","E","F"]
   ],
   [
      ["A","B","C"],
      ["D","E","F"],
      ["G","H","I"]
   ],
   [
      ["A","B","C"],
      ["D","E","F"],
      ["G","H","I"]
   ]
]

我尝试使用 map 函数和 valuesOf 函数,但没有成功。

谢谢!

假设输入和第一个子项确实是对象,您可以在顶层使用 pluck() 提取对象数组,然后映射每个子项以再次使用 pluck()。

输入:

{
    "0": {
        "0": ["A","B","C"],
        "1": ["D","E","F"]
    },
    "1": {
        "0": ["A","B","C"],
        "1": ["D","E","F"],
        "2": ["G","H","I"]
    },
    "2": {
        "0": ["A","B","C"],
        "1": ["D","E","F"],
        "2": ["G","H","I"]
    }
}

脚本:

%dw 2.0
output application/json  
---
payload 
    pluck $ 
    flatMap 
        ($ pluck ((value, key, index) -> value))

输出:

[
  [
    [
      "A",
      "B",
      "C"
    ],
    [
      "D",
      "E",
      "F"
    ]
  ],
  [
    [
      "A",
      "B",
      "C"
    ],
    [
      "D",
      "E",
      "F"
    ],
    [
      "G",
      "H",
      "I"
    ]
  ],
  [
    [
      "A",
      "B",
      "C"
    ],
    [
      "D",
      "E",
      "F"
    ],
    [
      "G",
      "H",
      "I"
    ]
  ]
]

假设输出如下所示的另一个选项

[
  {
    "0": {
      "0": ["A","B","C"],
      "1": ["D","E","F"]
    }
  },
  {
    "1": {
      "0": ["A","B","C"],
      "1": ["D","E","F"],
      "2": ["G","H","I"]
    }
  },
  {
    "2": {
      "0": ["A","B","C"],
      "1": ["D","E","F"],
      "2": ["G","H","I"]
    }
  }
]

此脚本将其转换为您想要的输出

payload map (valuesOf($) flatMap valuesOf($))

输出

[
  [
    ["A","B","C"],
    ["D","E","F"]
  ],
  [
    ["A","B","C"],
    ["D","E","F"],
    ["G","H","I"]
  ],
  [
    ["A","B","C"],
    ["D","E","F"],
    ["G","H","I"]
  ]
]