Arrays 使用Dataweave将多个字符串数组映射到单个数组中

Arrays 使用Dataweave将多个字符串数组映射到单个数组中,arrays,json,dataweave,anypoint-studio,mulesoft,Arrays,Json,Dataweave,Anypoint Studio,Mulesoft,我有一个带有单个索引的数组,该数组有一个JSON对象,该对象包含三个不同的字符串数组,我必须根据每个索引将其映射为单个数组。例如,从每个数组到单个JSON对象的所有第一个索引等等 在Dataweave转换中可能吗 输入 期望输出: [ { "Key1": "123", "Key2": "Test Brand 1", "Key3": "

我有一个带有单个索引的数组,该数组有一个JSON对象,该对象包含三个不同的字符串数组,我必须根据每个索引将其映射为单个数组。例如,从每个数组到单个JSON对象的所有第一个索引等等

在Dataweave转换中可能吗

输入

期望输出:

[
    {
        "Key1": "123",
        "Key2": "Test Brand 1",
        "Key3": "Via Roma 1"
    },
    {
        "Key1": "456",
        "Key2": "Test Brand 2",
        "Key3": "Via Milano 1"
    },
    {
        "Key1": "789",
        "Key2": "Test Brand 3",
        "Key3": "Via Napoli 1"
    }
]

您可以尝试以下DataWeave表达式,假设每个数组始终包含相同数量的项:

%dw 2.0
output application/json
---
flatten(payload map (item, index1) -> 
    item.id map (item2, index2) -> {
        "Key1": item2,
        "Key2": item.name[index2],
        "Key3": item.address[index2]
    })
输出(更改了地址名称以确保转换按预期进行):


您可以尝试下面的代码,我们不需要重复(循环)两次,也不需要展平

%dw 2.0
output application/json
var id = payload[0].id
---
id map (item2, index2) -> {
        "Key1": item2,
        "Key2": payload[0].name[index2],
        "Key3": payload[0].address[index2]
    }
它将给出相同的结果

[
  {
    "Key1": "123",
    "Key2": "Test Brand 1",
    "Key3": "Via Roma 1"
  },
  {
    "Key1": "456",
    "Key2": "Test Brand 2",
    "Key3": "Via Milano 1"
  },
  {
    "Key1": "789",
    "Key2": "Test Brand 3",
    "Key3": "Via Napoli 1"
  }
]

谢谢

我对此进行了一次调整,使其具有动态性,以便对阵列中的任意数量的键都有效。它变得有点混乱,因为您无法访问reduce中的索引。享受吧

%dw 2.0
output application/json
import indexOf from dw::core::Arrays
var data = [{
    "id": [
          "123",
          "456",
          "789"
        ],
    "name": [
          "Test Brand 1",
          "Test Brand 2",
          "Test Brand 3"
        ],
    "address": [
          "Via Roma 1",
          "Via Milano 1",
          "Via Napoli 1"
        ]
}]
--- 
(data flatMap valuesOf($)) map ((valuesArray) -> valuesArray reduce ((value, acc={}) -> acc ++ {
    ("key$(indexOf(valuesArray,value) + 1)"): value
}))
输出:

[
  {
    "key1": "123",
    "key2": "456",
    "key3": "789"
  },
  {
    "key1": "Test Brand 1",
    "key2": "Test Brand 2",
    "key3": "Test Brand 3"
  },
  {
    "key1": "Via Roma 1",
    "key2": "Via Milano 1",
    "key3": "Via Napoli 1"
  }
]

数组确实包含相同数量的值,很抱歉没有指定它。很好,非常感谢:)很高兴它成功了!请不要忘记将此答案标记为已接受,因为它可以帮助可能有相同问题的其他人。非常感谢。
%dw 2.0
output application/json
import indexOf from dw::core::Arrays
var data = [{
    "id": [
          "123",
          "456",
          "789"
        ],
    "name": [
          "Test Brand 1",
          "Test Brand 2",
          "Test Brand 3"
        ],
    "address": [
          "Via Roma 1",
          "Via Milano 1",
          "Via Napoli 1"
        ]
}]
--- 
(data flatMap valuesOf($)) map ((valuesArray) -> valuesArray reduce ((value, acc={}) -> acc ++ {
    ("key$(indexOf(valuesArray,value) + 1)"): value
}))
[
  {
    "key1": "123",
    "key2": "456",
    "key3": "789"
  },
  {
    "key1": "Test Brand 1",
    "key2": "Test Brand 2",
    "key3": "Test Brand 3"
  },
  {
    "key1": "Via Roma 1",
    "key2": "Via Milano 1",
    "key3": "Via Napoli 1"
  }
]